MartinCl2.Text.Json.Serialization 1.0.0

dotnet add package MartinCl2.Text.Json.Serialization --version 1.0.0                
NuGet\Install-Package MartinCl2.Text.Json.Serialization -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="MartinCl2.Text.Json.Serialization" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MartinCl2.Text.Json.Serialization --version 1.0.0                
#r "nuget: MartinCl2.Text.Json.Serialization, 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 MartinCl2.Text.Json.Serialization as a Cake Addin
#addin nuget:?package=MartinCl2.Text.Json.Serialization&version=1.0.0

// Install MartinCl2.Text.Json.Serialization as a Cake Tool
#tool nuget:?package=MartinCl2.Text.Json.Serialization&version=1.0.0                

JSON Serializer JIT Compiler for .NET Core 3.0

Emit CIL/MSIL code at runtime for serializing JSON object, utilizing the System.Test.Json namespace provided since .NET Core 3.0.

Example usage

static string CompileAndSerialize<T>(T obj)
{
    JsonJitSerializer<T> serializer = JsonJitSerializer<T>.Compile(new JsonSerializerOptions()
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    });

    return serializer.Serialize(obj);
}

static async Task CompileAndSerializeAsync<T>(Stream stream, T obj)
{
    JsonJitSerializer<T> serializer = JsonJitSerializer<T>.Compile(new JsonSerializerOptions()
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    });

    using (Utf8JsonWriter writer = new Utf8JsonWriter(stream))
    {
        await serializer.SerializeAsync(writer, obj);
    }
}

High level design

The JIT compiler generates a dynamic class inside a dynamic assembly when JsonJitSerializer.Compile<T> is called. This dynamic class is a value type and implements ISerializerImplementation<T>. That interface defines two methods: Serialize and SerializeChunk. Serialize serializes an object in one shot. SerializeChunk is re-enterable which can be called with the same parameter until it returns false, so it can be used for asynchronized serialization. Both methods will be implemented by generated IL codes. Other than that, the dynamic class contains static fields used as constants, such as converters, names and serialization options.

The dynamic class contains a list of Object fields used as serialization stack. The type Object is actually used as void* (Note: value type will be boxed). However there is no runtime casting involved. Therefore the runtime memory consumption is linear to the depth of the payload object. One exception is IDictionary<string, T>, because a local variable is needed to store a KeyValuePair<string, T> for every dictionary. Since the dynamic class is a value type, the serialization stack will be allocated on the stack at runtime.

Supported feature

  • Just-in-time compiled JSON serialization with System.Text.Json.JsonSerializerOptions.
  • Custom JsonConverter attribute.
  • Custom JsonPropertyName attribute.
  • Both synchronized and asynchronized (re-enterable) serialization.
  • struct serialization.
  • Correctly deal with ref getter.
  • IEnumerable<T> and IDictionary<string, T> serialization.
  • Value type enumerator optimization.
  • O(depth of structure + number of dictionary) runtime memory consumption.

Benchmark

See: https://github.com/Martin1994/JsonJitSerializer

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 netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.0

    • No dependencies.

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 565 9/13/2019