BigBytes.JsonParticle 1.0.0

dotnet add package BigBytes.JsonParticle --version 1.0.0
NuGet\Install-Package BigBytes.JsonParticle -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BigBytes.JsonParticle" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BigBytes.JsonParticle --version 1.0.0
#r "nuget: BigBytes.JsonParticle, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BigBytes.JsonParticle as a Cake Addin
#addin nuget:?package=BigBytes.JsonParticle&version=1.0.0

// Install BigBytes.JsonParticle as a Cake Tool
#tool nuget:?package=BigBytes.JsonParticle&version=1.0.0

Json Particle

C# Library for extending object capabilities with easy serialization and deserialization using JSON format.

Provides simple object mapper that allows to convert objects between different class types using JSON serialization.

Allows deserialization from TOML format.

Depends on brilliant Newtonsoft.Json package.

Example

Json<T>

Derrive from BigBytes.JsonParticle.Json<T> to add static methods Serialize and Deserialize to the class.

public class Record : BigBytes.JsonParticle.Json<Record>
{
    public class _Book
    {
        public string Title;
        public int Year;
    }

    public class _Author
    {
        public string Name;
    }

    public _Book Book;
    public _Author Author;
}
var record = new Record()
{
    Book = new Record._Book()
    {
        Title = "UML Distilled",
        Year = 2003,
    },
    Author = new Record._Author()
    {
        Name = "Martin Fowler",
    },
};
var json = Record.Serialize(record);
Console.WriteLine(json);
{
  "book": {
    "title": "UML Distilled",
    "year": 2003
  },
  "author": {
    "name": "Martin Fowler"
  }
}

Reverse conversion is also possible.

var json = @"
{
  ""book"": {
    ""title"": ""UML Distilled"",
    ""year"": 2003
  },
  ""author"": {
    ""name"": ""Martin Fowler""
  }
}
";

var record = Record.Deserialize(json);
            
Console.WriteLine(record.Book.Title);
Console.WriteLine(record.Book.Year);
Console.WriteLine(record.Author.Name);
UML Distilled
2003
Martin Fowler

More advanced example with TOML input.

[book]
title = "UML Distilled"
year = 2003

[author]
name = "Martin Fowler"
var toml = @"
[book]
title = ""UML Distilled""
year = 2003

[author]
name = ""Martin Fowler""
";
var json = new Converter.TomlToJson().Convert(toml);
var record = Mock.Record.Deserialize(json);
Debug.WriteLine(record.Book.Title);  // expect "UML Distilled"
Debug.WriteLine(record.Book.Year);   // expect "2003"
Debug.WriteLine(record.Author.Name); // expect "Martin Fowler"

Last but not least example with ungreedy<sup>1</sup> mapping.

public class Person : Json<Person>
{
    public string Name;
    public string City;
    public int Age;
}
public class Customer : Json<Customer>
{
    public string Name;
    public string City;
    public string Office;
}
Person person;
Customer customer;

person = new Person()
{
    Name = "Andy",
    Age = 21,
    City = "Warsaw",
};

customer = new Mapper<Customer>()
    .Map(person);

Console.WriteLine(customer.Name);              // Outputs: "Andy"
Console.WriteLine(customer.City);              // Outputs: "Warsaw"
Console.WriteLine(customer.Office ?? "NULL");  // Outputs: "NULL"

person = new Mapper<Person>()
    .Map(customer);

Console.WriteLine(person.Name);  // Outputs: "Andy"
Console.WriteLine(person.City);  // Outputs: "Warsaw"
Console.WriteLine(person.Age);   // Outputs: "0"

Voilà.

{
  "name": "Andy",
  "city": "Warsaw",
  "age": 21
}

If you get problems with mapping check if both classes contain Serialize and Deserialize methods. That's the minimum requirement for Mapper<> to work. You shouldn't get any problems when classes are using inheritance from Json<>.

Functions

TomlSectionNameToCSharpName

Converts TOML section name to its C# counterpart.

var toml = "list.one.12";
var csharp = BigBytes.JsonParticle.Utility.TomlSectionNameToCSharpName(toml);
System.Diagnostics.Debug.WriteLine(csharp); // expect "ListOne_12"

CSharpNameToJsonName

Converts C# name to its JSON counterpart.

var csharp = "MyObject";
var json = BigBytes.JsonParticle.Utility.CSharpNameToJsonName(csharp);
System.Diagnostics.Debug.WriteLine(json); // expect "myObject"

Project

Main library project directory contains two project files.

BigBytes.JsonParticle.Legacy.csproj

Notes

1) Ungreedy convertion allows to rewrite values only for fields that are present in both objects without throwing an exception

Author

Filip Golewski

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 217 5/29/2022

How much reality can you take?