Skip to content

Repository files navigation

Aoxe.Serialization


Provide an easy way to use serializations. It is also the serializer provider for all Aoxe technology stacks like configuration, cache, queue, rpc, etc.

1. Why use Aoxe.Serialization?

There are many different serializers in the world, they have different features and limitations. This project allows you to use difference serializers in the same way. Also it set nullable and default value for all serializers.

Serializers can be divided into two categories (binary and text):

  • Binary serializers
    • MemoryPack
    • MessagePack
    • MsgPack.Cli
    • protobuf-net
    • ZeroFormatter
  • Text serializers
    • Json
      • Jil
      • NetJson
      • Newtonsoft.Json
      • SpanJson
      • System.Text.Json
      • Utf8Json
    • Xml
      • XmlSerializer
      • DataContractSerializer
    • Yaml
      • SharpYaml
      • YamlDotNet
    • Ini
      • ini-parser-netstandard
    • Toml
      • Tomlet
      • Tomlyn

Though some serializers does not support stream or bytes, the Aoxe serializers will supply the lack. And the text serializers will support text on this base.

2. Functions

  • Helper: Unified code style and provide lack of features. The default enconding is UTF-8.
    • stream
      • sync
        • MemoryStream ToStream<TValue>(TValue? value)
        • MemoryStream ToStream(Type type, object? value)
        • void Pack<TValue>(TValue? value, Stream? stream)
        • void Pack(Type type, object? value, Stream? stream)
        • TValue? FromStream<TValue>(Stream? stream)
        • object? FromStream(Type type, Stream? stream)
      • async
        • Task PackAsync<TValue>(TValue? value, Stream? stream)
        • Task PackAsync(Type type, object? value, Stream? stream)
        • Task<TValue?> FromStreamAsync<TValue>(Stream? stream)
        • Task<object?> FromStreamAsync(Type type, Stream? stream)
    • bytes
      • byte[] ToBytes<TValue>(TValue? value)
      • byte[] ToBytes(Type type, object? value)
      • TValue? FromBytes<TValue>(byte[]? bytes)
      • object? FromBytes(Type type, byte[]? bytes)
    • text
      • json
        • string ToJson<TValue>(TValue? value)
        • string ToJson(Type type, object? value)
        • TValue? FromJson<TValue>(string? json)
        • object? FromJson(Type type, string? json)
      • xml
        • string ToXml<TValue>(TValue? value)
        • string ToXml(Type type, object? value)
        • TValue? FromXml<TValue>(string? xml)
        • object? FromXml(Type type, string? xml)
      • yaml
        • string ToYaml<TValue>(TValue? value)
        • string ToYaml(Type type, object? value)
        • TValue? FromYaml<TValue>(string? xml)
        • object? FromYaml(Type type, string? xml)
      • Ini
        • string ToIni<TValue>(TValue? value)
        • string ToIni(Type type, object? value)
        • TValue? FromIni<TValue>(string? xml)
        • object? FromIni(Type type, string? xml)
      • Toml
        • string ToToml<TValue>(TValue? value)
        • string ToToml(Type type, object? value)
        • TValue? FromToml<TValue>(string? xml)
        • object? FromToml(Type type, string? xml)
  • Extensions: Supply Extension methods base by Helper. Also it supports generic type and non-generic type.
    • Bytes
      • FromBytes
    • Object
      • PackTo
      • ToBytes
      • ToJson/ToXml/ToYaml
      • ToStream
    • Stream
      • FromStream
      • PackBy
    • String
      • FromJson/FromXml/FromYaml
  • Serializer: Implement Aoxe.Serialization.Abstractions, The Aoxe technology stacks use this library to serialize and deserialize.
    • Stream
      • stream ToStream<TValue>(TValue? value)
      • TValue? FromStream<TValue>(Stream? stream)
      • stream ToStream(Type type, object? value)
      • object? FromStream(Type type, Stream? stream)
    • Bytes
      • byte[] ToBytes<TValue>(TValue? value)
      • TValue? FromBytes<TValue>(byte[]? bytes)
      • byte[] ToBytes(Type type, object? value)
      • object? FromBytes(Type type, byte[]? bytes)
    • Text
      • string ToText<TValue>(TValue? value)
      • TValue? FromText<TValue>(string? text)
      • string ToText(Type type, object? value)
      • object? FromText(Type type, string? text)

3. Getting Started

Use nuget to install the package which you want.

PM> Install-Package Aoxe.DataContractSerializer
PM> Install-Package Aoxe.Jil
PM> Install-Package Aoxe.MemoryPack
PM> Install-Package Aoxe.MessagePack
PM> Install-Package Aoxe.MsgPack
PM> Install-Package Aoxe.NewtonsoftJson
PM> Install-Package Aoxe.NetJson
PM> Install-Package Aoxe.Utf8Json
PM> Install-Package Aoxe.Protobuf
PM> Install-Package Aoxe.SharpYaml
PM> Install-Package Aoxe.SpanJson
PM> Install-Package Aoxe.SystemTextJson
PM> Install-Package Aoxe.Xml
PM> Install-Package Aoxe.YamlDotNet
PM> Install-Package Aoxe.ZeroFormatter

You can then use the serializer in extension method way

vartestModel=newTestModel();// bytesvarbytes=testModel.ToBytes();vardeserializedModelFromBytes=bytes.FromBytes<TestModel>();// streamvarstream=testModel.ToStream();vardeserializedModelFromStream=stream.FromStream<TestModel>();// you can pack to a streamvarstream1=newMemoryStream();testModel.PackTo(stream1);varresult1=stream1.FromStream<TestModel>();// or use a stream pack byvarstream2=newMemoryStream();stream2.PackBy(testModel);varresult2=stream2.FromStream<TestModel>();// also the stream functions have async versionvarstream=testModel.ToStream();vardeserializedModelFromStream=awaitstream.FromStreamAsync<TestModel>();varstream1=newMemoryStream();awaittestModel.PackToAsync(stream1);varresult1=awaitstream1.FromStreamAsync<TestModel>();varstream2=newMemoryStream();awaitstream2.PackByAsync(testModel);varresult2=awaitstream2.FromStreamAsync<TestModel>();

4. Packages introductions

4.1. Aoxe.DataContractSerializer

Base by System.Runtime.Serialization.DataContractSerializer, serializes and deserializes an instance of a type into an XML stream or document using a supplied data contract.

4.2. Aoxe.Jil

A fast JSON (de)serializer, built on Sigil with a number of somewhat crazy optimization tricks.

4.3. Aoxe.MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.

4.4. Aoxe.MessagePack

The extremely fast MessagePack serializer for C#. It is 10x faster than MsgPack-Cli and outperforms other C# serializers. MessagePack for C# also ships with built-in support for LZ4 compression - an extremely fast compression algorithm. Performance is important, particularly in applications like games, distributed computing, microservices, or data caches.

4.5. Aoxe.MsgPack

MessagePack implementation for Common Language Infrastructure / msgpack.org[C#]

4.6. Aoxe.NetJson

Faster than Any Binary?

4.7. Aoxe.NewtonsoftJson

Json.NET is a popular high-performance JSON framework for .NET https://www.newtonsoft.com/json

4.8. Aoxe.Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).

4.9. Aoxe.Protobuf

protobuf-net is a contract based serializer for .NET code, that happens to write data in the "protocol buffers" serialization format engineered by Google. The API, however, is very different to Google's, and follows typical .NET patterns (it is broadly comparable, in usage, to XmlSerializer, DataContractSerializer, etc). It should work for most .NET languages that write standard types and can use attributes.

4.10. Aoxe.SharpYaml

SharpYaml is a .NET library that provides a YAML parser and serialization engine for .NET objects, compatible with CoreCLR.

4.11. Aoxe.SpanJson

SpanJson is a JSON serializer for .NET Core 6.0+.

4.12. Aoxe.SystemTextJson

The System.Text.Json namespace provides high-performance, low-allocating, and standards-compliant capabilities to process JavaScript Object Notation (JSON), which includes serializing objects to JSON text and deserializing JSON text to objects, with UTF-8 support built-in. It also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document object model (DOM) for random access of the JSON elements within a structured view of the data.

4.13. Aoxe.Xml

Serializes and deserializes objects into and from XML documents. The XmlSerializer enables you to control how objects are encoded into XML.

4.14. Aoxe.YamlDotNet

YamlDotNet is a YAML library for netstandard and other .NET runtimes.

YamlDotNet provides low level parsing and emitting of YAML as well as a high level object model similar to XmlDocument. A serialization library is also included that allows to read and write objects from and to YAML streams.

4.15. Aoxe.ZeroFormatter

Fastest C# Serializer and Infinitely Fast Deserializer for .NET, .NET Core and Unity.

5. Benchmark

BenchmarkDotNet v0.13.9+228a464e8be6c580ad9408e98f18813f6407fb5a, Windows 11 (10.0.22621.1105/22H2/2022Update/SunValley2)
AMD Ryzen 7 6800H with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK 8.0.100-rc.2.23502.2
[Host] : .NET 8.0.0 (8.0.23.47906), X64 RyuJIT AVX2
DefaultJob : .NET 8.0.0 (8.0.23.47906), X64 RyuJIT AVX2

IterationCount=1000 RunStrategy=Monitoring

5.1. Stream

5.1.1. To Stream

MethodMeanErrorStdDevMedianMinMaxGen0Gen1Allocated
DataContractToStream614.77 ns11.623 ns21.831 ns607.04 ns579.48 ns657.88 ns0.23460.00101968 B
JilToStream160.89 ns3.197 ns7.902 ns161.74 ns149.28 ns177.27 ns0.11180.0002936 B
MemoryPackToStream106.06 ns2.157 ns4.596 ns104.56 ns100.21 ns114.06 ns0.0783-656 B
MessagePackToStream180.11 ns3.409 ns3.022 ns180.67 ns173.95 ns184.10 ns0.0410-344 B
MsgPackToStream138.97 ns2.759 ns6.973 ns136.65 ns130.31 ns160.17 ns0.0658-552 B
NetJsonToStream162.19 ns3.093 ns3.177 ns162.87 ns152.39 ns166.34 ns0.0668-560 B
NewtonsoftJsonToStream411.07 ns8.218 ns19.531 ns402.60 ns384.70 ns449.70 ns0.21790.00051824 B
ProtobufToStream171.38 ns3.157 ns2.636 ns171.49 ns165.16 ns175.44 ns0.0410-344 B
SharpYamlToStream23,422.26 ns305.706 ns271.001 ns23,397.31 ns22,879.41 ns23,955.76 ns2.0752-18340 B
SpanJsonToStream107.76 ns2.113 ns2.821 ns108.34 ns100.68 ns110.88 ns0.0488-408 B
SystemTextJsonToStream177.22 ns3.562 ns6.690 ns177.42 ns167.45 ns195.76 ns0.0591-496 B
Utf8JsonToStream76.77 ns1.091 ns1.021 ns76.69 ns75.29 ns78.64 ns0.0411-344 B
XmlToStream5,413.13 ns101.784 ns95.209 ns5,405.25 ns5,239.99 ns5,609.19 ns1.28170.030510769 B
YamlDotNetToStream8,239.70 ns156.836 ns146.705 ns8,273.23 ns7,923.32 ns8,450.49 ns1.4038-11825 B
ZeroFormatterToStream120.37 ns2.323 ns2.582 ns120.54 ns115.67 ns124.31 ns0.17880.00071496 B

5.1.2. From Stream

MethodMeanErrorStdDevMedianMinMaxGen0Gen1Allocated
DataContractFromStream1,772.29 ns29.243 ns27.354 ns1,775.58 ns1,733.71 ns1,826.85 ns0.99180.02108296 B
JilFromStream127.57 ns2.498 ns3.739 ns126.57 ns122.86 ns135.39 ns0.0372-312 B
MemoryPackFromStream66.26 ns1.001 ns0.936 ns66.15 ns64.29 ns67.65 ns0.0134-112 B
MessagePackFromStream108.56 ns1.896 ns1.773 ns108.02 ns105.79 ns111.64 ns0.0076-64 B
MsgPackFromStream195.43 ns3.689 ns3.270 ns194.37 ns190.80 ns203.67 ns0.0534-448 B
NetJsonFromStream222.71 ns2.850 ns2.666 ns222.29 ns218.28 ns227.30 ns0.0448-376 B
NewtonsoftJsonFromStream606.27 ns11.604 ns10.855 ns604.17 ns584.02 ns627.60 ns0.34050.00292848 B
ProtobufFromStream128.10 ns1.953 ns1.731 ns127.54 ns125.87 ns131.29 ns0.0076-64 B
SharpYamlFromStream885.33 ns17.613 ns39.755 ns871.24 ns831.36 ns974.66 ns0.76580.00486408 B
SpanJsonFromStream88.68 ns1.332 ns1.246 ns88.65 ns86.38 ns90.44 ns0.0153-128 B
SystemTextJsonFromStream279.96 ns3.207 ns2.999 ns279.86 ns274.24 ns286.14 ns0.0076-64 B
Utf8JsonFromStream112.44 ns1.566 ns1.465 ns112.91 ns109.70 ns114.35 ns0.0076-64 B
XmlFromStream6,901.06 ns114.626 ns101.613 ns6,884.34 ns6,710.18 ns7,071.92 ns1.0071-8588 B
YamlDotNetFromStream5,890.89 ns117.598 ns110.001 ns5,910.51 ns5,692.23 ns6,048.19 ns1.41910.030511936 B
ZeroFormatterFromStream49.29 ns0.966 ns0.949 ns49.51 ns46.62 ns50.40 ns0.0296-248 B

5.2. Bytes

5.2.1. To Bytes

MethodMeanErrorStdDevMinMaxMedianGen0Gen1Allocated
DataContractToBytes664.27 ns19.490 ns57.161 ns578.95 ns792.24 ns655.74 ns0.2794-2344 B
JilToBytes145.38 ns2.890 ns6.583 ns132.34 ns157.76 ns146.29 ns0.0706-592 B
MemoryPackToBytes56.20 ns0.595 ns0.557 ns55.34 ns57.20 ns56.07 ns0.0057-48 B
MessagePackToBytes72.32 ns1.422 ns1.397 ns70.18 ns74.41 ns72.55 ns0.0048-40 B
MsgPackToBytes157.00 ns2.972 ns3.650 ns148.44 ns163.26 ns158.10 ns0.0706-592 B
NetJsonToBytes124.72 ns2.469 ns2.744 ns120.02 ns129.67 ns124.45 ns0.0257-216 B
NewtonsoftJsonToBytes360.81 ns7.188 ns8.827 ns339.12 ns373.32 ns362.96 ns0.17790.00051488 B
ProtobufToBytes189.07 ns3.571 ns3.969 ns180.03 ns196.40 ns188.49 ns0.0458-384 B
SharpYamlToBytes24,277.75 ns453.809 ns621.178 ns23,489.34 ns25,807.42 ns24,228.69 ns2.1973-18396 B
SpanJsonToBytes77.93 ns1.030 ns0.913 ns76.41 ns79.42 ns77.94 ns0.0076-64 B
SystemTextJsonToBytes150.26 ns2.170 ns2.030 ns147.87 ns154.07 ns149.49 ns0.0076-64 B
Utf8JsonToBytes55.73 ns0.655 ns0.613 ns54.98 ns56.85 ns55.54 ns0.0076-64 B
XmlToBytes5,519.67 ns109.294 ns116.943 ns5,252.93 ns5,737.02 ns5,515.89 ns1.31230.030511017 B
YamlDotNetToBytes8,220.72 ns158.050 ns155.226 ns7,916.78 ns8,479.30 ns8,213.22 ns1.40380.015311761 B
ZeroFormatterToBytes74.74 ns1.067 ns0.998 ns72.73 ns76.22 ns74.90 ns0.13770.00051152 B

5.2.2. From Bytes

MethodMeanErrorStdDevMinMaxMedianGen0Gen1Allocated
DataContractFromBytes1,808.81 ns32.524 ns55.228 ns1,750.61 ns1,965.96 ns1,802.17 ns0.99950.01918360 B
JilFromBytes114.20 ns1.681 ns1.573 ns111.63 ns117.09 ns114.19 ns0.0296-248 B
MemoryPackFromBytes47.96 ns0.955 ns1.275 ns45.40 ns50.22 ns48.10 ns0.0076-64 B
MessagePackFromBytes85.46 ns1.440 ns1.347 ns82.62 ns87.78 ns85.59 ns0.0076-64 B
MsgPackFromBytes219.12 ns3.389 ns3.004 ns214.23 ns225.49 ns219.11 ns0.0610-512 B
NetJsonFromBytes233.90 ns4.083 ns4.538 ns224.89 ns241.42 ns233.89 ns0.0372-312 B
NewtonsoftJsonFromBytes584.92 ns12.593 ns36.735 ns533.79 ns686.05 ns579.09 ns0.33190.00292784 B
ProtobufFromBytes233.63 ns3.133 ns2.931 ns228.92 ns238.48 ns233.65 ns0.0181-152 B
SharpYamlFromBytes26,055.20 ns281.045 ns262.890 ns25,670.34 ns26,535.89 ns26,069.27 ns2.38040.061019939 B
SpanJsonFromBytes64.41 ns1.189 ns1.112 ns62.95 ns66.21 ns64.28 ns0.0076-64 B
SystemTextJsonFromBytes230.67 ns3.039 ns2.694 ns226.27 ns235.44 ns229.81 ns0.0076-64 B
Utf8JsonFromBytes104.34 ns1.095 ns1.024 ns102.44 ns105.83 ns104.17 ns0.0076-64 B
XmlFromBytes7,196.03 ns106.114 ns99.259 ns7,041.42 ns7,363.96 ns7,214.21 ns1.0071-8651 B
YamlDotNetFromBytes6,123.06 ns87.373 ns81.729 ns5,945.31 ns6,250.51 ns6,133.12 ns1.41910.030511880 B
ZeroFormatterFromBytes40.41 ns0.818 ns1.707 ns38.27 ns45.30 ns39.96 ns0.0296-248 B

5.3. Text

5.3.1. Json

5.3.1.1. To Json
MethodMeanErrorStdDev
JilToText115.07 ns2.259 ns4.460 ns
NetJsonToText100.53 ns1.289 ns1.205 ns
NewtonsoftJsonToText332.22 ns6.472 ns8.640 ns
SpanJsonToText102.23 ns1.610 ns1.506 ns
SystemTextJsonToText168.14 ns1.972 ns1.845 ns
Utf8JsonToText66.74 ns1.138 ns1.009 ns
5.3.1.2. From Json
MethodMeanErrorStdDev
JilFromText91.71 ns1.585 ns1.405 ns
NetJsonFromText174.18 ns2.711 ns2.536 ns
NewtonsoftJsonFromText520.15 ns10.420 ns25.949 ns
SpanJsonFromText85.90 ns1.704 ns1.673 ns
SystemTextJsonFromText263.97 ns3.804 ns3.559 ns
Utf8JsonFromText129.68 ns2.410 ns2.475 ns

5.3.2. Xml

5.3.2.1. To Xml
MethodMeanErrorStdDev
DataContractToText708.0 ns14.03 ns16.16 ns
XmlToText5,529.7 ns62.59 ns52.26 ns
5.3.2.2. From Xml
MethodMeanErrorStdDevMedian
DataContractFromText1.921 μs0.0383 μs0.0872 μs1.889 μs
XmlFromText7.218 μs0.0887 μs0.0740 μs7.222 μs

5.3.3. Yaml

5.3.3.1. To Yaml
MethodMeanErrorStdDev
SharpYamlToText22.560 μs0.2544 μs0.2125 μs
YamlDotNetToText7.994 μs0.1544 μs0.1839 μs
5.3.3.2. From Yaml
MethodMeanErrorStdDev
SharpYamlFromText24.065 μs0.4524 μs0.4646 μs
YamlDotNetFromText5.769 μs0.1137 μs0.1594 μs

5.3.4. Ini

5.3.4.1. To Ini
MethodMeanErrorStdDev
IniParserToText8.570 μs0.1496 μs0.1469 μs
5.3.4.2. From Ini
MethodMeanErrorStdDev
IniParserFromText10.88 μs0.216 μs0.281 μs

5.3.5. Toml

5.3.5.1. To Toml
MethodMeanErrorStdDev
TomlynToText6.152 μs0.0780 μs0.0691 μs
5.3.5.2. From Toml
MethodMeanErrorStdDev
TomlynFromText8.785 μs0.1127 μs0.1054 μs

Thank`s for JetBrains for the great support in providing assistance and user-friendly environment for my open source projects.

JetBrains

About

Provide an easy way to use Binary/DataContractSerializer/Jil/MessagePack/MsgPack/NewtonsoftJson/Utf8Json/Protobuf/SystemTextJson/Xml/ZeroFormatter

Resources

Stars

18 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages