Skip to content

trampster/JsonSrcGen

Repository files navigation

alt text JsonSrcGen

Json library that uses .NET 5 c# Source Generators

Note: Requires the .NET 5 to run.

Supported Types

Classes Class serializers can be generated by defining a Json attribute on the class

[Json]
public class MyType
{
    [JsonName("my_name")]
    public string MyProperty {get;set}

    [JsonIgnore]
    public string IgnoredProperty {get;set;}
}

var converter = new JsonConverter();

ReadOnlySpan<char> json = convert.ToJson(new MyType(){MyProperty = "Some value"});

var myType = new MyType();
convert.FromJson("{\"MyProperty\:\"Some value\"}", myType);

Properties with the following types are supported:

Integer Types Others
int float?
int? double
uint double?
uint? boolean
ushort boolean?
ushort? string
short DateTime
short? DateTime?
byte DateTimeOffset
byte? DateTimeOffset?
long Guid
long? Arrays
float List

Arrays

Arrays are generated by defining a JsonArray attribute at the assembly level.

[assembly: JsonArray(typeof(bool))]

List

Lists are generated by defining a JsonList attribute at the assembly level.

[assembly: JsonList(typeof(bool))]

Dictionary

Dictionaries are generated by defining a JsonList attribute at the assembly level. Only Dictionaries with string keys are supported currently.

[assembly: JsonDictionary(typeof(string), typeof(int))]

Values

Simple json values are generated by defining a JsonValue attribute at the assembly level.

[assembly: JsonValue(typeof(int))]

Custom Converters

You can customize the conversion for specific types by providing your own converter. You must implement ICustomConverter and put the CustomConverter attribute on your class.

[CustomConverter(typeof(int))]
public class CustomCaseStringConverter : ICustomConverter<int>
{
    public void ToJson(IJsonBuilder builder, int value)
    {
        // Write your json to the builder here
    }

    public ReadOnlySpan<char> FromJson(ReadOnlySpan<char> json, ref int value)
    {
        // Read the Json from the json span here
    }
}

Leave out null properties

You can instruct JsonSrcGen to skip serializing null property values by adding the following attribute:

[JsonIgnoreNull]
public class MyJsonType
{
    int? MyProperty {get;set;}
}

Set Property to default if missing

By 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

public class MyJsonType
{
    [JsonOptional]
    string MyProperty{get;set;}
}

UTF8 Support

JsonSrcGen supports UTF8 via ReadOnlySpan.

To Json looks is the same as string ToJson byte with Utf8 at the end of the name

ReadOnlySpan<byte> json = convert.ToJsonUtf8(new MyType(){MyProperty = "Some value"});

From Json is even easier as the method name is the same as string FromJson but takes a ReadOnlySpan instead of ReadOnlySpan

ReadOnlySpan<byte> utf8Json = Encoding.Utf8.GetBytes("{\"MyProperty\:\"Some value\"}");
convert.FromJson(utf8Json, myType);

Nuget Packages

JsonSrcGen is available as a nuget package: https://www.nuget.org/packages/JsonSrcGen/

About

Json library that uses .NET 5 Source Generators

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages