{"id":13629253,"url":"https://github.com/aviationexam/json-converter-source-generator","last_synced_at":"2026-01-06T01:09:44.303Z","repository":{"id":212643628,"uuid":"696463074","full_name":"aviationexam/json-converter-source-generator","owner":"aviationexam","description":"Source generated polymorphic JSON converters","archived":false,"fork":false,"pushed_at":"2025-04-09T12:57:01.000Z","size":400,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T13:44:25.250Z","etag":null,"topics":["csharp-sourcegenerator","dotnet","enum-serialization","json","polymorphic-types","system-text-json"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aviationexam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-25T19:44:29.000Z","updated_at":"2025-04-09T12:57:03.000Z","dependencies_parsed_at":"2024-06-26T14:34:52.915Z","dependency_job_id":"f510ffab-9379-4dad-8489-94b932e88d71","html_url":"https://github.com/aviationexam/json-converter-source-generator","commit_stats":null,"previous_names":["aviationexam/json-converter-source-generator"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aviationexam%2Fjson-converter-source-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aviationexam%2Fjson-converter-source-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aviationexam%2Fjson-converter-source-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aviationexam%2Fjson-converter-source-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aviationexam","download_url":"https://codeload.github.com/aviationexam/json-converter-source-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249316070,"owners_count":21249888,"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-sourcegenerator","dotnet","enum-serialization","json","polymorphic-types","system-text-json"],"created_at":"2024-08-01T22:01:05.814Z","updated_at":"2026-01-06T01:09:44.272Z","avatar_url":"https://github.com/aviationexam.png","language":"C#","funding_links":[],"categories":["Content","Source Generators"],"sub_categories":["82. [jsonConverterSourceGenerator](https://ignatandrei.github.io/RSCG_Examples/v2/docs/jsonConverterSourceGenerator) , in the [Serializer](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#serializer) category","Serialization"],"readme":"[![Build Status](https://github.com/aviationexam/json-converter-source-generator/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/aviationexam/json-converter-source-generator/actions/workflows/build.yml)\n[![NuGet](https://img.shields.io/nuget/v/Aviationexam.GeneratedJsonConverters.SourceGenerator.svg?style=flat-square\u0026label=nuget)](https://www.nuget.org/packages/Aviationexam.GeneratedJsonConverters.SourceGenerator/)\n[![MyGet](https://img.shields.io/myget/json-converter-source-generator/vpre/Aviationexam.GeneratedJsonConverters.SourceGenerator?label=MyGet)](https://www.myget.org/feed/json-converter-source-generator/package/nuget/Aviationexam.GeneratedJsonConverters.SourceGenerator)\n[![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Faviationexam%2Fjson-converter-source-generator%2Fshield%2FAviationexam.GeneratedJsonConverters.SourceGenerator%2Flatest\u0026label=Aviationexam.GeneratedJsonConverters.SourceGenerator)](https://f.feedz.io/aviationexam/json-converter-source-generator/packages/Aviationexam.GeneratedJsonConverters.SourceGenerator/latest/download)\n\n# Aviationexam.GeneratedJsonConverters.SourceGenerator\n\nMotivation for this library are polymorphic contracts with discriminator property not present as first property.\n\ni.e. this JSON\n```json\n{\n    \"baseProperty\": 1,\n    \"$type\": 2,\n    \"anotherLeafProperty\": 2\n}\n```\nis deserialized correctly into `AnotherLeafContract` using this library.\n\nAnd string based enum serialization.\n\n## Install\n```xml\n\u003cItemGroup\u003e\n    \u003cPackageReference Include=\"Aviationexam.GeneratedJsonConverters.SourceGenerator\" Version=\"0.1.0\" PrivateAssets=\"all\" /\u003e\n\u003c/ItemGroup\u003e\n```\n\n## How to use library\n\n```xml\n\u003cPropertyGroup\u003e\n    \u003c!-- DefaultJsonSerializerContext configuration is required to generate UseEnumConverters method --\u003e\n    \u003cAVI_EJC_DefaultJsonSerializerContext_ClassAccessibility\u003epublic\u003c/AVI_EJC_DefaultJsonSerializerContext_ClassAccessibility\u003e\n    \u003cAVI_EJC_DefaultJsonSerializerContext_Namespace\u003eNamespaceOf.My.Json.Serializer.Context\u003c/AVI_EJC_DefaultJsonSerializerContext_Namespace\u003e\n    \u003cAVI_EJC_DefaultJsonSerializerContext_ClassName\u003eMyJsonSerializerContext\u003c/AVI_EJC_DefaultJsonSerializerContext_ClassName\u003e\n\n    \u003c!-- Allowed options BackingType, FirstEnumName. Default value FirstEnumName --\u003e\n    \u003cAVI_EJC_DefaultEnumSerializationStrategy\u003eBackingType\u003c/AVI_EJC_DefaultEnumSerializationStrategy\u003e\n\n    \u003c!-- Allowed options UseBackingType, UseEnumName, or UseBackingType|UseEnumName (DeserializationStrategy is Flags enum). Default value UseEnumName --\u003e\n    \u003cAVI_EJC_DefaultEnumDeserializationStrategy\u003eUseBackingType|UseEnumName\u003c/AVI_EJC_DefaultEnumDeserializationStrategy\u003e\n\u003c/PropertyGroup\u003e\n```\n\n```cs\n// file=contracts.cs\nusing Aviationexam.GeneratedJsonConverters.Attributes;\n\n[JsonPolymorphic] // notice, that attributes are from `Aviationexam.GeneratedJsonConverters.Attributes` namespace, not `System.Text.Json.Serialization`\n[JsonDerivedType(typeof(LeafContract), typeDiscriminator: nameof(LeafContract))]\n[JsonDerivedType(typeof(AnotherLeafContract), typeDiscriminator: 2)]\n[JsonDerivedType\u003cGenericLeafContract\u003e(typeDiscriminator: nameof(GenericLeafContract))]\npublic abstract class BaseContract\n{\n    public int BaseProperty { get; set; }\n}\npublic sealed class LeafContract : BaseContract\n{\n    public int LeafProperty { get; set; }\n}\npublic sealed class AnotherLeafContract : BaseContract\n{\n    public int AnotherLeafProperty { get; set; }\n}\npublic sealed class GenericLeafContract : BaseContract\n{\n    public int Property { get; set; }\n}\n\n[EnumJsonConverter] // this use project defined configuration\npublic enum EMyEnum\n{\n    [EnumMember(Value = \"C\")]\n    A,\n    [EnumMember(Value = \"D\")]\n    B,\n}\n\n[EnumJsonConverter(\n    SerializationStrategy = EnumSerializationStrategy.FirstEnumName,\n    DeserializationStrategy = EnumDeserializationStrategy.UseEnumName\n)]\npublic enum EMyEnumWithExplicitConfiguration\n{\n    [EnumMember(Value = \"C\")]\n    A,\n    [EnumMember(Value = \"D\")]\n    B,\n}\n\n[DisableEnumJsonConverter]\npublic enum EMyIgnoredEnum\n{\n    C,\n    D,\n}\n\n// file=MyJsonSerializerContext.cs\nusing System.Text.Json.Serialization;\n\n[JsonSerializable(typeof(BaseContract))] // this line is neccesary, generator searches for JsonSerializableAttribute with argument type decorated by JsonPolymorphicAttribute\n[JsonSerializable(typeof(LeafContract))] // notice, it's necessary to specify leaf types\n[JsonSerializable(typeof(AnotherLeafContract))]\n[JsonSerializable(typeof(GenericLeafContract))]\n\n[JsonSerializable(typeof(EMyEnum))] // only necessary for not referenced enums from other contracts\n[JsonSerializable(typeof(EMyEnumWithExplicitConfiguration))]\npublic partial class MyJsonSerializerContext : JsonSerializerContext\n{\n    static MyJsonSerializerContext()\n    {\n        // register generated converters to options\n        UsePolymorphicConverters(s_defaultOptions.Converters);\n        UseEnumConverters(s_defaultOptions.Converters);\n\n        Default = new MyJsonSerializerContext(new System.Text.Json.JsonSerializerOptions(s_defaultOptions));\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faviationexam%2Fjson-converter-source-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faviationexam%2Fjson-converter-source-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faviationexam%2Fjson-converter-source-generator/lists"}