Eocron.Serialization
1.2.1
dotnet add package Eocron.Serialization --version 1.2.1
NuGet\Install-Package Eocron.Serialization -Version 1.2.1
<PackageReference Include="Eocron.Serialization" Version="1.2.1" />
paket add Eocron.Serialization --version 1.2.1
#r "nuget: Eocron.Serialization, 1.2.1"
// Install Eocron.Serialization as a Cake Addin
#addin nuget:?package=Eocron.Serialization&version=1.2.1
// Install Eocron.Serialization as a Cake Tool
#tool nuget:?package=Eocron.Serialization&version=1.2.1
Eocron.Serialization
This library is inteded to unify C# wide known frameworks for serialization under one interface. The list include:
XmlDocument
/XDocument
serialization usingXmlSerializer
/XmlObjectSerializer
- Yaml serialization
- Json serialization
- Protobuf serialization
Common interface they share is ISerializationConverter
and all string
/byte
/stream
extensions are based on this interface.
Main path of defining converter is to make it as singleton and use everywhere, so its best to avoid creating those per-call (in most cases it should not affect performance).
Example:
public static readonly ISerializationConverter XDocument =
new XmlSerializationConverter<XDocument>(
new XmlAdapter<XDocument>(
new XmlSerializerAdapter(x => new XmlSerializer(x)),
new XDocumentAdapter()));
Or:
public static readonly ISerializationConverter XmlDataContract =
new XmlSerializationConverter<XmlDocument>(
new XmlAdapter<XmlDocument>(
new XmlObjectSerializerAdapter(x => new DataContractSerializer(x)),
new XmlDocumentAdapter()));
Or:
public static readonly ISerializationConverter Json = new JsonSerializationConverter(
new JsonSerializerSettings()
{
Formatting = Formatting.Indented
});
Usage:
var xml = XmlDataContract.SerializeToString(myObj);
XmlDataContract.SerializeTo(myObj, stream);
var bytes = XmlDataContract.SerializeToBytes(myObj);
XML
Because of many problems related to XML in C# such as:
- Multiple versions of documents like
XmlDocument
,XDocument
- Multiple versions of serializers like
XmlSerializer
,XmlObjectSerializer
,DataContractSerializer
which work with both types of documents - Various markups like
XmlRoot
/DataContract
/ISerializable
- Constant problems of serializing basic types like
Dictionary
/TimeSpan
which in one version throw error, in other it will just silently empty your fields (Say HI! toTimeSpan
being empty in .net472). - Chaotic changes from Microsoft to blow up your tests (rearrangment of namespaces, adding encoding attribute, etc) and sometimes criple back-compatability (Say HI! to BOM in .net6)
I decided to unify all this architectural garbage into couple of adapters, so it is easier to configure entire serialization process, such as initial settings on namespaces/readers/writers/serializers and XSLT transformations on document, so you choose your own pill to swallow. This should give 100% percent coverage on schema formats and compatability with bugs-as-feature, but can sometimes lower performance tweak flexibility. But who cares? XML is slow/old and you know it. Use Json where it is possible.
Benchmark
BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19044.2130/21H2/November2021Update)
AMD Ryzen 5 3600, 1 CPU, 12 logical and 6 physical cores
[Host] : .NET Framework 4.8 (4.8.4515.0), X64 RyuJIT VectorSize=256
Job-VOETLF : .NET Framework 4.8 (4.8.4515.0), X64 LegacyJIT VectorSize=256 DEBUG
BuildConfiguration=Debug
| Method | Mean | Error | StdDev | Median |
|------------------------ |-----------:|----------:|----------:|-----------:|
| ProtobufDeserialize | 3.912 us | 0.0512 us | 0.0479 us | 3.920 us |
| ProtobufSerialize | 3.090 us | 0.0617 us | 0.1380 us | 3.025 us |
| JsonDeserialize | 14.750 us | 0.2365 us | 0.3075 us | 14.596 us |
| JsonSerialize | 8.996 us | 0.1325 us | 0.1239 us | 9.012 us |
| DataContractDeserialize | 41.471 us | 0.6344 us | 0.5624 us | 41.183 us |
| DataContractSerialize | 36.643 us | 0.7258 us | 0.8067 us | 36.399 us |
| XDocumentDeserialize | 34.189 us | 0.3386 us | 0.3168 us | 34.432 us |
| XDocumentSerialize | 20.747 us | 0.4102 us | 0.5477 us | 20.675 us |
| XmlDocumentDeserialize | 38.562 us | 0.7628 us | 0.9647 us | 38.449 us |
| XmlDocumentSerialize | 29.083 us | 0.5791 us | 0.8844 us | 28.936 us |
| YamlDeserialize | 114.085 us | 1.7690 us | 1.4772 us | 114.266 us |
| YamlSerialize | 165.450 us | 3.2628 us | 7.4968 us | 162.044 us |
Product | Versions 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. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 9.0.1)
- protobuf-net (>= 3.1.22)
- YamlDotNet (>= 12.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.