Friday, July 23, 2021

Introducing JsonSrcGen 1.1

 

 Introducing JsonSrcGen 1.1


JsonSrcGen 1.1 has been released. This is the first release that includes UTF8 support.

Why UTF8?
UTF8 support is a big deal for a Json serializer. In c# strings are UTF16, but the vast majority of real JSON usage is UTF8, files, rest API's all use UTF8. This means a JSON serializer that uses .net strings has to do a double conversion, first from UTF8 to a .Net string (UTF16) then from the .net string to the json object. If a JSON serializer can work with UTF16 directly then a large amount of time can be saved.

Features

  • UTF8 Support
  • Decimal type support
  • Struct and read only struct support

Code Examples

// To Json UTF8
ReadOnlySpan<byte> json = convert.ToJsonUtf8(new MyType(){MyProperty = "Some value"});

// From Json UTF8
ReadOnlySpan<byte> utf8Json = Encoding.Utf8.GetBytes("{\"MyProperty\:\"Some value\"}");
convert.FromJson(utf8Json, myType);

Benchmarks

The code for the following benchmarks can be found in github. The following class is used for these benchmarks:

public class JsonTestClass
{
    public string FirstName{get;set;}
    public string LastName{get;set;}
    public int Age{get;set;}
    public bool Registered {get;set;}
}




How to get it

Please checkout the JsonSrcGen project on github: https://github.com/trampster/JsonSrcGen

JsonSrcGen is available as a nuget package:

Thanks to...

A special thanks goes out to the people who have contributed to JsonSrcGen for this release.
@trampster - Daniel Hughes
@hugobritobh - Hugo de Brito V. R. Alves
@sirh3e - Marvin Huber
@Youssef1313 - Youssef Victor