{"id":13629620,"url":"https://github.com/trampster/JsonSrcGen","last_synced_at":"2025-04-17T09:35:15.066Z","repository":{"id":55558935,"uuid":"289436314","full_name":"trampster/JsonSrcGen","owner":"trampster","description":"Json library that uses .NET 5 Source Generators","archived":false,"fork":false,"pushed_at":"2021-07-31T09:23:17.000Z","size":726,"stargazers_count":147,"open_issues_count":1,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-04T22:09:04.545Z","etag":null,"topics":["csharp-sourcegenerator"],"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/trampster.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["trampster"]}},"created_at":"2020-08-22T06:50:51.000Z","updated_at":"2024-09-21T06:15:16.000Z","dependencies_parsed_at":"2022-08-15T03:10:48.663Z","dependency_job_id":null,"html_url":"https://github.com/trampster/JsonSrcGen","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trampster%2FJsonSrcGen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trampster%2FJsonSrcGen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trampster%2FJsonSrcGen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trampster%2FJsonSrcGen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trampster","download_url":"https://codeload.github.com/trampster/JsonSrcGen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223751306,"owners_count":17196610,"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"],"created_at":"2024-08-01T22:01:14.994Z","updated_at":"2024-11-08T20:31:27.472Z","avatar_url":"https://github.com/trampster.png","language":"C#","funding_links":["https://github.com/sponsors/trampster"],"categories":["Source Generators","Do not want to test 112 ( old ISourceGenerator )"],"sub_categories":["Serialization","1. [ThisAssembly](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly) , in the [EnhancementProject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#enhancementproject) category"],"readme":"# ![alt text](https://github.com/trampster/JsonSrcGen/blob/master/JsonSrcGen/icon.png \"JsonSrcGen Logo\") JsonSrcGen\nJson library that uses .NET 5 c# Source Generators\n\nNote: Requires the .NET 5 to run.\n\n# Supported Types\n\n**Classes**\nClass serializers can be generated by defining a Json attribute on the class\n```csharp\n[Json]\npublic class MyType\n{\n    [JsonName(\"my_name\")]\n    public string MyProperty {get;set}\n\n    [JsonIgnore]\n    public string IgnoredProperty {get;set;}\n}\n\nvar converter = new JsonConverter();\n\nReadOnlySpan\u003cchar\u003e json = convert.ToJson(new MyType(){MyProperty = \"Some value\"});\n\nvar myType = new MyType();\nconvert.FromJson(\"{\\\"MyProperty\\:\\\"Some value\\\"}\", myType);\n```\nProperties with the following types are supported:\n\nInteger Types | Others\n------|--------\nint | float?\nint? | double\nuint | double?\nuint? | boolean\nushort | boolean?\nushort? | string \nshort | DateTime\nshort? | DateTime?\nbyte | DateTimeOffset\nbyte? | DateTimeOffset?\nlong | Guid\nlong? | Arrays\nfloat | List\u003cT\u003e\n    \n**Arrays**\n\nArrays are generated by defining a JsonArray attribute at the assembly level.\n\n```csharp\n[assembly: JsonArray(typeof(bool))]\n```\n\n**List**\n\nLists are generated by defining a JsonList attribute at the assembly level.\n\n```csharp\n[assembly: JsonList(typeof(bool))]\n```\n\n**Dictionary**\n\nDictionaries are generated by defining a JsonList attribute at the assembly level. Only Dictionaries with string keys are supported currently.\n\n```csharp\n[assembly: JsonDictionary(typeof(string), typeof(int))]\n```\n\n**Values**\n\nSimple json values are generated by defining a JsonValue attribute at the assembly level.\n\n```csharp\n[assembly: JsonValue(typeof(int))]\n```\n\n**Custom Converters**\n\nYou can customize the conversion for specific types by providing your own converter. You must implement ICustomConverter\u003cT\u003e and\nput the CustomConverter attribute on your class.\n\n```csharp\n[CustomConverter(typeof(int))]\npublic class CustomCaseStringConverter : ICustomConverter\u003cint\u003e\n{\n    public void ToJson(IJsonBuilder builder, int value)\n    {\n        // Write your json to the builder here\n    }\n\n    public ReadOnlySpan\u003cchar\u003e FromJson(ReadOnlySpan\u003cchar\u003e json, ref int value)\n    {\n        // Read the Json from the json span here\n    }\n}\n```\n\n**Leave out null properties**\n\nYou can instruct JsonSrcGen to skip serializing null property values by adding the following attribute:\n\n```csharp\n[JsonIgnoreNull]\npublic class MyJsonType\n{\n    int? MyProperty {get;set;}\n}\n```\n\n**Set Property to default if missing**\n\nBy default JsonSrcGen doesn't set properties to there default value if they are missing in the JSON. If you always give FromJson a new instance this isn't a problem. However if you reused objects (which is a big performance boost) then the property wont get set unless present in the Json. If you want JsonSrcGen to set missing properties to default then you can specify this using the JsonOptionalAttribute\n\n```csharp\npublic class MyJsonType\n{\n    [JsonOptional]\n    string MyProperty{get;set;}\n}\n```\n\n**UTF8 Support**\n\nJsonSrcGen supports UTF8 via ReadOnlySpan\u003cbyte\u003e.\n\nTo Json looks is the same as string ToJson byte with Utf8 at the end of the name\n\n```csharp\nReadOnlySpan\u003cbyte\u003e json = convert.ToJsonUtf8(new MyType(){MyProperty = \"Some value\"});\n```\n\nFrom Json is even easier as the method name is the same as string FromJson but takes a ReadOnlySpan\u003cbyte\u003e instead of ReadOnlySpan\u003cchar\u003e\n\n```\nReadOnlySpan\u003cbyte\u003e utf8Json = Encoding.Utf8.GetBytes(\"{\\\"MyProperty\\:\\\"Some value\\\"}\");\nconvert.FromJson(utf8Json, myType);\n```\n\n**Nuget Packages**\n\nJsonSrcGen is available as a nuget package:\nhttps://www.nuget.org/packages/JsonSrcGen/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrampster%2FJsonSrcGen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrampster%2FJsonSrcGen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrampster%2FJsonSrcGen/lists"}