All Projects → trampster → JsonSrcGen

trampster / JsonSrcGen

Licence: MIT license
Json library that uses .NET 5 Source Generators

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to JsonSrcGen

Immutype
Immutability is easy!
Stars: ✭ 26 (-81.43%)
Mutual labels:  csharp-sourcegenerator
dotvariant
A type-safe and space-efficient sum type for C# (comparable to discriminated unions in C or C++)
Stars: ✭ 52 (-62.86%)
Mutual labels:  csharp-sourcegenerator
ThisAssembly
Exposes project and assembly level information as constants in the ThisAssembly class using source generators powered by Roslyn.
Stars: ✭ 209 (+49.29%)
Mutual labels:  csharp-sourcegenerator
JsonByExampleGenerator
Generate classes based on example json files in your project. Uses a C# 9 source generator.
Stars: ✭ 55 (-60.71%)
Mutual labels:  csharp-sourcegenerator
Vogen
A semi-opinionated library which is a source generator and a code analyser. It Source generates Value Objects
Stars: ✭ 240 (+71.43%)
Mutual labels:  csharp-sourcegenerator
spreadcheetah
SpreadCheetah is a high-performance .NET library for generating spreadsheet (Microsoft Excel XLSX) files.
Stars: ✭ 107 (-23.57%)
Mutual labels:  csharp-sourcegenerator
AutoInterface
C# interface-to-member source generator
Stars: ✭ 47 (-66.43%)
Mutual labels:  csharp-sourcegenerator
DpdtInject
Highly efficient compile-time general purpose DI container based on C# source generators.
Stars: ✭ 25 (-82.14%)
Mutual labels:  csharp-sourcegenerator
avatar
A modern compile-time generated interception/proxy library
Stars: ✭ 80 (-42.86%)
Mutual labels:  csharp-sourcegenerator
StringLiteralGenerator
C# Source Generator for UTF-8 binary literal
Stars: ✭ 109 (-22.14%)
Mutual labels:  csharp-sourcegenerator
GitBuildInfo.SourceGenerator
Source Generator for dumping the git branch information, commit hash, and if the working tree is dirty or clean on projects that install this and applies them as an assembly level attribute.
Stars: ✭ 15 (-89.29%)
Mutual labels:  csharp-sourcegenerator
TypedSignalR.Client
C# Source Generator to Create Strongly Typed SignalR Client.
Stars: ✭ 16 (-88.57%)
Mutual labels:  csharp-sourcegenerator
StructPacker
Low-level, lightweight and performance-focused serializer for C# struct types that uses Source Generators technology.
Stars: ✭ 42 (-70%)
Mutual labels:  csharp-sourcegenerator
GraphQL.Tools
GraphQL.Tools is a GraphQL to C# compiler (code-generator) which turns your GraphQL schema into a set of C# classes, interfaces, and enums.
Stars: ✭ 49 (-65%)
Mutual labels:  csharp-sourcegenerator
Plastic
This project provides encapsulation of things like Domain, Application Rules, Business Rules or Business Logic in Application.
Stars: ✭ 30 (-78.57%)
Mutual labels:  csharp-sourcegenerator
Generator.Equals
A source code generator for automatically implementing IEquatable<T> using only attributes.
Stars: ✭ 49 (-65%)
Mutual labels:  csharp-sourcegenerator
GodotOnReady
A C# Source Generator that adds convenient onready-like features to your C# scripts in Godot Mono without any reflection.
Stars: ✭ 73 (-47.86%)
Mutual labels:  csharp-sourcegenerator
EmbeddingResourceCSharp
Embed resource files more C# programmer friendly!
Stars: ✭ 22 (-84.29%)
Mutual labels:  csharp-sourcegenerator
WinFormsComInterop
ComWrappers required to run NativeAOT and WinForms
Stars: ✭ 54 (-61.43%)
Mutual labels:  csharp-sourcegenerator

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/

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].