https://github.com/hexarc-software/hexarc-serialization
Advanced converters for the System.Text.Json serializer
https://github.com/hexarc-software/hexarc-serialization
csharp deserialization discriminated-unions dotnet json libary library nuget serialization tagged-unions tuples
Last synced: 10 months ago
JSON representation
Advanced converters for the System.Text.Json serializer
- Host: GitHub
- URL: https://github.com/hexarc-software/hexarc-serialization
- Owner: hexarc-software
- Created: 2021-02-10T13:49:52.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-04-23T15:10:14.000Z (over 2 years ago)
- Last Synced: 2025-02-14T20:04:19.781Z (11 months ago)
- Topics: csharp, deserialization, discriminated-unions, dotnet, json, libary, library, nuget, serialization, tagged-unions, tuples
- Language: C#
- Homepage: https://github.com/hexarc-software/hexarc-serialization
- Size: 43 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/funding.yml
Awesome Lists containing this project
README
# Serialization for advanced .NET types
[](http://badges.mit-license.org)
[](https://fondify.app/to/9qjvN4GZf78M2JsLUxXUNQWnDo96s12Zzzkzoo8cFpqw)
The Hexarc Serialization project provides additional converters for the `System.Text.Json` serializer.
## Packages
|Package| Platform |Version|Downloads|
|-------|-----------|-------|---------|
|`Hexarch.Serialization.Union`| .NET 7.0+ |[](https://www.nuget.org/packages/Hexarc.Serialization.Union)|[](https://www.nuget.org/packages/Hexarc.Serialization.Union)|
|`Hexarch.Serialization.Tuple`| .NET 7.0+ |[](https://www.nuget.org/packages/Hexarc.Serialization.Tuple)|[](https://www.nuget.org/packages/Hexarc.Serialization.Tuple)|
## Hexarc.Serialization.Union
The `Hexarc.Serialization.Union` package helps to serialize .NET/C# classes hierarchy as a tagged union (also known as a discriminated union).
### A tagged union example
```c#
[UnionTag(nameof(Kind))]
[UnionCase(typeof(Circle), nameof(Circle))]
[UnionCase(typeof(Square), nameof(Square))]
public abstract class Shape
{
public abstract String Kind { get; }
}
public sealed class Circle : Shape
{
public override String Kind { get; } = nameof(Circle);
public required Double Radius { get; set; }
}
public sealed class Square : Shape
{
public override String Kind { get; } = nameof(Square);
public required Double Side { get; set; }
}
```
In the example above the `UnionTag` attribute marks the union tag and the `UnionCase` attribute
marks a known subtype (or a case class) of the `Shape` class.
### Serialization and deserialization of the tagged union
```c#
var settings = new JsonSerializerOptions { Converters = { new UnionConverterFactory() } };
var square = new Square { Side = 15.0 };
Console.WriteLine(JsonSerializer.Serialize(square, settings));
var shape = JsonSerializer.Deserialize(@"{ ""Kind"": ""Circle"", ""Radius"": 10.0 }", settings);
Console.Write((shape as Circle)!.Radius);
```
Some technical details about the tagged union converter implementation can be found in [this article](https://shadeglare.medium.com/mimic-discriminating-union-types-in-c-with-serialization-via-system-text-json-3da67ef58dc0).
## Hexarc.Serialization.Tuple
The `Hexarc.Serialization.Tuple` package helps to serialize .NET/C# value tuple types.
```c#
var settings = new JsonSerializerOptions { Converters = { new TupleConverterFactory() } };
var point = (10, 20);
Console.WriteLine(JsonSerializer.Serialize(point, settings));
var (x, y) = JsonSerializer.Deserialize<(Int32, Int32)>(@"[10, 20]", settings);
Console.Write($"Point coords: {x}, {y}");
```
## Acknowledgments
Built with JetBrains tools for [Open Source](https://jb.gg/OpenSourceSupport) projects.

## License
MIT © [Hexarc Software and its contributors](https://github.com/hexarc-software)