{"id":26328470,"url":"https://github.com/hexarc-software/hexarc-serialization","last_synced_at":"2025-03-15T21:17:02.445Z","repository":{"id":65427612,"uuid":"337738115","full_name":"hexarc-software/hexarc-serialization","owner":"hexarc-software","description":"Advanced converters for the System.Text.Json serializer","archived":false,"fork":false,"pushed_at":"2023-04-23T15:10:14.000Z","size":44,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-14T20:04:19.781Z","etag":null,"topics":["csharp","deserialization","discriminated-unions","dotnet","json","libary","library","nuget","serialization","tagged-unions","tuples"],"latest_commit_sha":null,"homepage":"https://github.com/hexarc-software/hexarc-serialization","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hexarc-software.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"custom":["fondify.app/to/9qjvN4GZf78M2JsLUxXUNQWnDo96s12Zzzkzoo8cFpqw"]}},"created_at":"2021-02-10T13:49:52.000Z","updated_at":"2023-01-20T06:03:58.000Z","dependencies_parsed_at":"2023-01-30T22:00:53.881Z","dependency_job_id":null,"html_url":"https://github.com/hexarc-software/hexarc-serialization","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexarc-software%2Fhexarc-serialization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexarc-software%2Fhexarc-serialization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexarc-software%2Fhexarc-serialization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexarc-software%2Fhexarc-serialization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hexarc-software","download_url":"https://codeload.github.com/hexarc-software/hexarc-serialization/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243775887,"owners_count":20346281,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","deserialization","discriminated-unions","dotnet","json","libary","library","nuget","serialization","tagged-unions","tuples"],"created_at":"2025-03-15T21:17:01.756Z","updated_at":"2025-03-15T21:17:02.344Z","avatar_url":"https://github.com/hexarc-software.png","language":"C#","funding_links":["fondify.app/to/9qjvN4GZf78M2JsLUxXUNQWnDo96s12Zzzkzoo8cFpqw"],"categories":[],"sub_categories":[],"readme":"# Serialization for advanced .NET types \n[![License](http://img.shields.io/:license-mit-blue.svg)](http://badges.mit-license.org)\n[![Donate Solana](https://img.shields.io/static/v1?label=Fondify.app\u0026message=Donate%20%24SOL\u0026color=blueviolet)](https://fondify.app/to/9qjvN4GZf78M2JsLUxXUNQWnDo96s12Zzzkzoo8cFpqw)\n\nThe Hexarc Serialization project provides additional converters for the `System.Text.Json` serializer.\n\n## Packages\n\n|Package| Platform  |Version|Downloads|\n|-------|-----------|-------|---------|\n|`Hexarch.Serialization.Union`| .NET 7.0+ |[![NuGet](https://img.shields.io/nuget/v/Hexarc.Serialization.Union.svg)](https://www.nuget.org/packages/Hexarc.Serialization.Union)|[![Downloads](http://img.shields.io/nuget/dt/Hexarc.Serialization.Union.svg)](https://www.nuget.org/packages/Hexarc.Serialization.Union)|\n|`Hexarch.Serialization.Tuple`| .NET 7.0+ |[![NuGet](https://img.shields.io/nuget/v/Hexarc.Serialization.Tuple.svg)](https://www.nuget.org/packages/Hexarc.Serialization.Tuple)|[![Downloads](http://img.shields.io/nuget/dt/Hexarc.Serialization.Tuple.svg)](https://www.nuget.org/packages/Hexarc.Serialization.Tuple)|\n\n## Hexarc.Serialization.Union\n\nThe `Hexarc.Serialization.Union` package helps to serialize .NET/C# classes hierarchy as a tagged union (also known as a discriminated union).\n\n### A tagged union example\n```c#\n[UnionTag(nameof(Kind))]\n[UnionCase(typeof(Circle), nameof(Circle))]\n[UnionCase(typeof(Square), nameof(Square))]\npublic abstract class Shape\n{\n    public abstract String Kind { get; }\n}\n\npublic sealed class Circle : Shape\n{\n    public override String Kind { get; } = nameof(Circle);\n    public required Double Radius { get; set; } \n}\n\npublic sealed class Square : Shape\n{\n    public override String Kind { get; } = nameof(Square);\n    public required Double Side { get; set; }\n}\n```\n\nIn the example above the `UnionTag` attribute marks the union tag and the `UnionCase` attribute \nmarks a known subtype (or a case class) of the `Shape` class.\n\n### Serialization and deserialization of the tagged union\n```c#\nvar settings = new JsonSerializerOptions { Converters = { new UnionConverterFactory() } };\n\nvar square = new Square { Side = 15.0 };\nConsole.WriteLine(JsonSerializer.Serialize(square, settings));\n\nvar shape = JsonSerializer.Deserialize\u003cShape\u003e(@\"{ \"\"Kind\"\": \"\"Circle\"\", \"\"Radius\"\": 10.0 }\", settings);\nConsole.Write((shape as Circle)!.Radius);\n```\n\nSome 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).\n\n## Hexarc.Serialization.Tuple\n\nThe `Hexarc.Serialization.Tuple` package helps to serialize .NET/C# value tuple types.\n\n```c#\nvar settings = new JsonSerializerOptions { Converters = { new TupleConverterFactory() } };\n\nvar point = (10, 20);\nConsole.WriteLine(JsonSerializer.Serialize(point, settings));\n\nvar (x, y) = JsonSerializer.Deserialize\u003c(Int32, Int32)\u003e(@\"[10, 20]\", settings);\nConsole.Write($\"Point coords: {x}, {y}\");\n```\n\n## Acknowledgments\nBuilt with JetBrains tools for [Open Source](https://jb.gg/OpenSourceSupport) projects.\n\n![JetBrains Logo (Main) logo](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)\n\n## License\nMIT © [Hexarc Software and its contributors](https://github.com/hexarc-software)\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhexarc-software%2Fhexarc-serialization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhexarc-software%2Fhexarc-serialization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhexarc-software%2Fhexarc-serialization/lists"}