SpanJson 3.2.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package SpanJson --version 3.2.3                
NuGet\Install-Package SpanJson -Version 3.2.3                
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="SpanJson" Version="3.2.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SpanJson --version 3.2.3                
#r "nuget: SpanJson, 3.2.3"                
#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 SpanJson as a Cake Addin
#addin nuget:?package=SpanJson&version=3.2.3

// Install SpanJson as a Cake Tool
#tool nuget:?package=SpanJson&version=3.2.3                

SpanJson

NuGet

See https://github.com/Tornhoof/SpanJson/wiki/Performance for Benchmarks

What is supported

  • Serialization and Deserialization into/from byte arrays, strings, TextWriter/TextReader and streams
  • Serialization and Deserialization of Arrays, Lists, Complex types of the following Base Class Library types:

sbyte, Int16, Int32, Int64, byte, UInt16, UInt32, UInt64, Single, Double, decimal, bool, char, DateTime, DateTimeOffset, TimeSpan, Guid, string, Version, Uri, Tuple<,>,ValueTuple<,>, KeyValuePair<,>

  • Public Properties and Fields are considered for serialization/deserialization

  • DateTime{Offset} is in ISO8601 mode with profile https://www.w3.org/TR/NOTE-datetime

  • Dynamics

  • Enums (string and integer, for integer see section Custom Resolver), incl. Flags

  • Anonymous types

  • Dictionary, ConcurrentDictionary with string/int/enum as key, the enum is formatted as a string.

  • Serialization/Deserialization of most IEnumerable<T> types (Stack and ConcurrentStack are not supported)

  • Support for [DataMember(Name="MemberName")] to set field name

  • Support for [IgnoreDataMember] to ignore a specific member

  • Support for ShouldSerializeXXX pattern to decide at runtime if a member should be serialized

  • Support for [EnumMember] to specify the string value of the enum value

  • Support for Immutable Collections, full Serialization/Deserialization for ImmutableList, ImmutableArray, ImmutableDictionary, ImmutableSortedDictionary. ImmutableStack is not supported.

  • Support for read-only collections, ReadOnlyCollection, ReadOnlyDictionary, they are deserialized into a writeable type (i.e. List or Dictionary), then the read-only version is created via an appropriate constructor overload.

  • Support for tuples currently excludes the last type with 8 arguments (TRest)

  • Support for annotating a constructor with [JsonConstructor] to use that one instead of assigning members during deserialization

  • Support for custom serializers with [JsonCustomSerializer] to use that one instead of the normal formatter, see examples below

  • Support for annotating a IDictionary<string,object> with [JsonExtensionData]. Serialization will write all values from the dictionary as additional attributes. Deserialization will deserialize all unknown attributes into it. This does not work together with the Dynamic Language Runtime (DLR) support or the [JsonConstructor] attribute. See Example below. The Dictionary will also honor the Case Setting (i.e. CamelCase) and null behaviour for the dictionary keys.

  • Pretty printing JSON

  • Minify JSON

  • Different 'Resolvers' to control general behaviour:

    • Exclude Nulls with Camel Case: ExcludeNullsCamelCaseResolver
    • Exclude Nulls with Original Case (default): ExcludeNullsOriginalCaseResolver
    • Include Nulls with Camel Case: IncludeNullsCamelCaseResolver
    • Include Nulls with Original Case: IncludeNullsOriginalCaseResolver
  • Custom Resolvers to control behaviour much more detailed.

How to use it

Synchronous API:

var result = JsonSerializer.Generic.Utf16.Serialize(input);
var result = JsonSerializer.NonGeneric.Utf16.Serialize(input);
var result = JsonSerializer.Generic.Utf16.Deserialize<Input>(input);
var result = JsonSerializer.NonGeneric.Utf16.Deserialize(input, typeof(Input));

var result = JsonSerializer.Generic.Utf8.Serialize(input);
var result = JsonSerializer.NonGeneric.Utf8.Serialize(input);
var result = JsonSerializer.Generic.Utf8.Deserialize<Input>(input);
var result = JsonSerializer.NonGeneric.Utf8.Deserialize(input, typeof(Input));

// The following methods return an ArraySegment from the ArrayPool, you NEED to return it yourself after working with it.
var result = JsonSerializer.Generic.Utf16.SerializeToArrayPool(input);
var result = JsonSerializer.NonGeneric.Utf16.SerializeToArrayPool(input);
var result = JsonSerializer.Generic.Utf8.SerializeToArrayPool(input);
var result = JsonSerializer.NonGeneric.Utf8.SerializeToArrayPool(input);

Asynchronous API:

ValueTask result = JsonSerializer.Generic.Utf16.SerializeAsync(input, textWriter, cancellationToken);
ValueTask result = JsonSerializer.NonGeneric.Utf16.SerializeAsync(input, textWriter, cancellationToken);
ValueTask<Input> result = JsonSerializer.Generic.Utf16.DeserializeAsync<Input>(textReader,cancellationToken);
ValueTask<object> result = JsonSerializer.NonGeneric.Utf16.DeserializeAsync(textReader,typeof(Input),cancellationToken);
ValueTask result = JsonSerializer.Generic.Utf8.SerializeAsync(input, stream, cancellationToken);
ValueTask result = JsonSerializer.NonGeneric.Utf8.SerializeAsync(input, stream, cancellationToken);
ValueTask<Input> result = JsonSerializer.Generic.Utf8.DeserializeAsync<Input>(input, stream, cancellationToken);
ValueTask<object> result = JsonSerializer.NonGeneric.Utf8.DeserializeAsync(input, stream, typeof(Input) cancellationToken);

To use other resolvers use the appropriate overloads,e.g.:

var serialized = JsonSerializer.NonGeneric.Utf16.Serialize<Input, IncludeNullsOriginalCaseResolver<char>>(includeNull);

Pretty Printing:

var pretty = JsonSerializer.PrettyPrinter.Print(serialized); // this works by reading the JSON and writing it out again with spaces and line breaks

Minify:
var minified = JsonSerializer.Minifier.Minify(serialized); // this works by reading the JSON and writing it out again without spaces and line breaks

Full example:

using System;
using SpanJson;

namespace Test
{
    public class Program
    {
        private static void Main(string[] args)
        {

            var input = new Input { Text = "Hello World" };

            var serialized = JsonSerializer.Generic.Utf16.Serialize(input);

            var deserialized = JsonSerializer.Generic.Utf16.Deserialize<Input>(serialized);
        }
    }

    public class Input
    {
        public string Text { get; set; }
    }
}
using System;
using SpanJson;

namespace Test
{
	// This JsonConstructorAttribute assumes that the constructor parameter names are the same as the member names (case insensitive comparison, order is not important)
	public class DefaultDO
	{
		[JsonConstructor]
		public DefaultDO(string key, int value)
		{
			Key = key;
			Value = value;
		}

		public string Key { get; }
		public int Value { get; }
	}

	// This JsonConstructorAttribute allows overwriting the matching names of the constructor parameter names to allow for different member names vs. constructor parameter names, order is important here
	public readonly struct NamedDO
	{
		[JsonConstructor(nameof(Key), nameof(Value))]
		public NamedDO(string first, int second)
		{
			Key = first;
			Value = second;
		}


		public string Key { get; }
		public int Value { get; }
	}
}
// Type with a custom serializer to (de)serialize the long value into/from string
public class TestDTO
{
    [JsonCustomSerializer(typeof(LongAsStringFormatter), "Hello World")]
    public long Value { get; set; }
}

// Serializes the Long into a string
public sealed class LongAsStringFormatter : ICustomJsonFormatter<long>
{
    public static readonly LongAsStringFormatter Default = new LongAsStringFormatter();
	
	public object Arguments {get;set;} // the Argument from the attribute will be assigned

    public void Serialize(ref JsonWriter<char> writer, long value)
    {
        StringUtf16Formatter.Default.Serialize(ref writer, value.ToString(CultureInfo.InvariantCulture));
    }

    public long Deserialize(ref JsonReader<char> reader)
    {
        var value = StringUtf16Formatter.Default.Deserialize(ref reader);
        if (long.TryParse(value, out long longValue))
        {
            return longValue;
        }

        throw new InvalidOperationException("Invalid value.");
    }

    public void Serialize(ref JsonWriter<byte> writer, long value)
    {
        StringUtf8Formatter.Default.Serialize(ref writer, value.ToString(CultureInfo.InvariantCulture));
    }

    public long Deserialize(ref JsonReader<byte> reader)
    {
        var value = StringUtf8Formatter.Default.Deserialize(ref reader);
        if (long.TryParse(value, out long longValue))
        {
            return longValue;
        }

        throw new InvalidOperationException("Invalid value.");
    }
}
// It's possible to annotate custom types a custom formatter to always use the custom formatter 
[JsonCustomSerializer(typeof(TwcsCustomSerializer))]
public class TypeWithCustomSerializer : IEquatable<TypeWithCustomSerializer>
{
    public long Value { get; set; }
}

// Instead of copying the implementation of for serialize/deserialize for utf8/utf16
// it is possible to use the writer/reader methods which support both, there is no or only a very minor performance difference
public sealed class TwcsCustomSerializer : ICustomJsonFormatter<TypeWithCustomSerializer>
{
    public static readonly TwcsCustomSerializer Default = new TwcsCustomSerializer();

    public object Arguments { get; set; }

    private void SerializeInternal<TSymbol>(ref JsonWriter<TSymbol> writer, TypeWithCustomSerializer value) where TSymbol : struct
    {
        if (value == null)
        {
            writer.WriteNull();
            return;
        }

        writer.WriteBeginObject();

        writer.WriteName(nameof(TypeWithCustomSerializer.Value));

        writer.WriteInt64(value.Value);

        writer.WriteEndObject();
    }

    public void Serialize(ref JsonWriter<byte> writer, TypeWithCustomSerializer value)
    {
        SerializeInternal(ref writer, value);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private TypeWithCustomSerializer DeserializeInternal<TSymbol>(ref JsonReader<TSymbol> reader) where TSymbol : struct
    {
        if (reader.ReadIsNull())
        {
            return null;
        }

        reader.ReadBeginObjectOrThrow();
        var result = new TypeWithCustomSerializer {Value = reader.ReadInt64()};
        reader.ReadEndObjectOrThrow();
        return result;
    }

    public TypeWithCustomSerializer Deserialize(ref JsonReader<byte> reader)
    {
        return DeserializeInternal(ref reader);
    }

    public void Serialize(ref JsonWriter<char> writer, TypeWithCustomSerializer value)
    {
        SerializeInternal(ref writer, value);
    }

    public TypeWithCustomSerializer Deserialize(ref JsonReader<char> reader)
    {
        return DeserializeInternal(ref reader);
    }
}
// Below class will serialize Key and Value and any additional key-value-pair from the dictionary
public class ExtensionTest
{
    public string Key;
    public string Value;

    [JsonExtensionData]
    public IDictionary<string, object> AdditionalValues { get; set; }
}

ASP.NET Core 3.1 Formatter

You can enable SpanJson as the default JSON formatter in ASP.NET Core 3.1 by using the Nuget package SpanJson.AspNetCore.Formatter. To enable it, add one of the following extension methods to the AddMvc() call in ConfigureServices

  • AddSpanJson for a resolver with ASP.NET Core 2.1 defaults: IncludeNull, CamelCase, Integer Enums
  • AddSpanJsonCustom for a custom resolver (one of the default resolvers or custom)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddSpanJson();
}

AddSpanJson is the closest in behaviour compared to the default JSON.NET formatter, this used the AspNetCoreDefaultResolver type. Note: This clears the Formatter list, if you have other formatters, e.g. JSON Patch or XML, you need to re-add them.

Custom Resolver

As each option is a concrete class it is infeasible to supply concrete classes for each possible option combination. To support a custom combination implement your own custom formatter resolvers

public sealed class CustomResolver<TSymbol> : ResolverBase<TSymbol, CustomResolver<TSymbol>> where TSymbol : struct
{
    public CustomResolver() : base(new SpanJsonOptions
    {
        NullOption = NullOptions.ExcludeNulls,
        NamingConvention = NamingConventions.CamelCase,
        EnumOption = EnumOptions.Integer
    })
    {
    }
}

and pass this type just the same as e.g. ExcludeNullsCamelCaseResolver

TODO

  • Improve async deserialization/serialization: Find a way to do it streaming instead of buffering.
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (19)

Showing the top 5 NuGet packages that depend on SpanJson:

Package Downloads
SpanJson.AspNetCore.Formatter

SpanJson ASP.NET Core Formatter is the ASP.NET Core formatter package for SpanJson for ASP.NET Core 3.0+

Miki.Serialization.SpanJson

Package Description

Bundgaard.JsonRpcLib

C# DotNetCore 2.1+ Client/Server Json RPC library Using Span<T>, Memory<T> and IO pipelines

Eventso.Subscription.SpanJson

Eventso subscription JSON message deserializer using SpanJson library

BitbankDotNet

.NET wrapper for Bitbank.cc API.

GitHub repositories (10)

Showing the top 5 popular GitHub repositories that depend on SpanJson:

Repository Stars
dotnet/orleans
Cloud Native application framework for .NET
MessagePack-CSharp/MessagePack-CSharp
Extremely Fast MessagePack Serializer for C#(.NET, .NET Core, Unity, Xamarin). / msgpack.org[C#]
dotnet/corefxlab
This repo is for experimentation and exploring new ideas that may or may not make it into the main corefx repo.
beetlex-io/FastHttpApi
a lightweight and high-performance http/websocket service component in the dotnet core platform that supports TLS.
EgorBo/SimdJsonSharp
C# bindings for lemire/simdjson (and full C# port)
Version Downloads Last updated
4.2.1 5,673 10/9/2024
4.2.0 29,111 7/29/2024
4.1.0 16,873 5/23/2024
4.0.1 364,405 7/13/2023
4.0.0 272,244 11/15/2022
3.3.1 109,558 8/19/2022
3.3.0 613 8/19/2022
3.2.4 5,966 7/28/2022
3.2.3 1,031 7/24/2022
3.2.2 131,193 4/15/2022
3.2.1 665 4/14/2022
3.2.0 199,983 12/14/2021
3.1.0 277,117 11/19/2020
3.1.0-rc1 352 11/16/2020
3.0.1 57,277 11/23/2019
3.0.0 5,166 9/23/2019
3.0.0-alpha-02 461 9/23/2019
3.0.0-alpha-01 364 9/7/2019
2.0.14 12,838 5/31/2019
2.0.13 815 5/30/2019
2.0.12 1,041 5/30/2019
2.0.11 864 5/21/2019
2.0.10 4,544 5/13/2019
2.0.9 5,159 4/11/2019
2.0.8 1,885 4/4/2019
2.0.7 19,021 3/6/2019
2.0.6 1,314 2/26/2019
2.0.5 799 2/25/2019
2.0.4 5,406 1/24/2019
2.0.3 1,024 1/24/2019
2.0.2 1,352 1/17/2019
2.0.1 13,681 1/2/2019
2.0.0 1,817 12/15/2018
2.0.0-rc6 613 12/9/2018
2.0.0-rc5 619 12/8/2018
2.0.0-rc4 677 12/8/2018
2.0.0-rc3 719 11/29/2018
2.0.0-rc2 651 11/27/2018
2.0.0-rc1 755 11/27/2018
1.3.3 982 11/26/2018
1.3.2 886 11/19/2018
1.3.1 909 11/9/2018
1.3.0 125,268 10/9/2018
1.2.5 1,286 9/21/2018
1.2.4 5,141 7/31/2018
1.2.3 910 7/23/2018
1.2.2 1,136 7/22/2018
1.2.1 983 7/18/2018
1.2.0 2,368 7/16/2018
1.1.0 1,112 7/14/2018
1.0.7 14,666 7/3/2018
1.0.6 974 7/3/2018
1.0.5 856 7/3/2018
1.0.4 861 7/2/2018
1.0.3 1,005 7/1/2018
1.0.2 990 7/1/2018
1.0.1 2,008 7/1/2018
1.0.0.2-womjlssg 951 7/1/2018
1.0.0.1 1,301 7/1/2018
1.0.0 1,377 7/1/2018
0.4.0 1,055 6/24/2018
0.3.0.7 1,035 6/23/2018
0.3.0.5 1,018 6/22/2018
0.3.0.4 1,091 6/20/2018
0.3.0.3 975 6/19/2018
0.3.0.2 1,001 6/19/2018
0.3.0.1 924 6/18/2018
0.3.0 982 6/17/2018
0.2.0.2 1,000 6/16/2018
0.2.0.1 900 6/15/2018
0.2.0 1,056 6/15/2018
0.1.0.5 1,060 6/5/2018
0.1.0.3 1,452 5/30/2018
0.1.0-rc2-9 816 5/23/2018
0.1.0-rc2-7 778 5/20/2018
0.1.0-rc2-5 2,385 5/13/2018
0.1.0-rc2-3 832 5/12/2018
0.1.0-rc2-1 837 5/12/2018
0.1.0-rc1-6 918 5/11/2018
0.1.0-rc1-5 1,036 5/10/2018
0.1.0-rc1-4 804 5/10/2018
0.1.0-rc1-3 798 5/8/2018
0.1.0-rc1-2 803 5/7/2018

Bugfixes