GithubHelp home page GithubHelp logo

ironcev-forks / jsonsrcgen Goto Github PK

View Code? Open in Web Editor NEW

This project forked from trampster/jsonsrcgen

0.0 1.0 0.0 331 KB

Json library that uses .NET 5 Source Generators

License: MIT License

C# 100.00%

jsonsrcgen's Introduction

JsonSrcGen

Json library that uses .NET 5 c# Source Generators

Note: Requires the .Net 5 Preview to run. Mono platforms do not have support for the .Net 5 Source Generators yet.

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? float?
uint double
uint? double?
ushort boolean
ushort? boolean?
short string
short? DateTime
byte DateTime?
byte? Guid
long Arrays
long? 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
    }
}

Nuget Packages

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

jsonsrcgen's People

Contributors

trampster avatar youssef1313 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.