Giter Club home page Giter Club logo

hexarc-serialization's Introduction

Serialization for advanced .NET types

License Donate Solana

The Hexarc Serialization project provides additional converters for the System.Text.Json serializer.

Packages

Package Platform Version Downloads
Hexarch.Serialization.Union .NET 7.0+ NuGet Downloads
Hexarch.Serialization.Tuple .NET 7.0+ NuGet Downloads

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

[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

var settings = new JsonSerializerOptions { Converters = { new UnionConverterFactory() } };

var square = new Square { Side = 15.0 };
Console.WriteLine(JsonSerializer.Serialize(square, settings));

var shape = JsonSerializer.Deserialize<Shape>(@"{ ""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.

Hexarc.Serialization.Tuple

The Hexarc.Serialization.Tuple package helps to serialize .NET/C# value tuple types.

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 projects.

JetBrains Logo (Main) logo

License

MIT © Hexarc Software and its contributors

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.