RSGC-JSON to Class- part 4

 

 

name JsonByExampleGenerator
nuget

https://www.nuget.org/packages/JsonByExampleGenerator/

link https://github.com/hermanussen/JsonByExampleGenerator/
author Robin Hermanussen

This will generate C# classes from json files.
 

The code that you start with is


    {

    "FirstName": "Andrei",

    "LastName": "Ignat",

    "Blog": "http://msprogrammer.serviciipeweb.ro/"


The code that you will use is



    var p1 = new Person();

    p1.Blog = "http://msprogrammer.serviciipeweb.ro/";

    var config = new ConfigurationBuilder()

      .AddJsonFile("persons.json")

      .Build();

    

    var p = config.Get<Person>();

    var p2 = Person.FromConfig(config);

 

The code that is generated is


    [DataContract(Name = "Person", Namespace = "JsonToClass.Json.Persons")]

    public partial class Person

    {

    [DataMember(Name = "FirstName", EmitDefaultValue = false, Order = 0)]

    public string FirstName { get; set; }

    [DataMember(Name = "LastName", EmitDefaultValue = false, Order = 1)]

    public string LastName { get; set; }

    [DataMember(Name = "Blog", EmitDefaultValue = false, Order = 2)]

    public string Blog { get; set; }

    

    public static Person FromConfig([System.Diagnostics.CodeAnalysis.NotNull] IConfiguration config)

    {

    return config.Get<Person>();

    }

    }

Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/JsonToClass

All RSCG

NrBlog Post
1RSCG–part 1
2RSCG- AppVersion–part 2
3http://msprogrammer.serviciipeweb.ro/2021/02/17/rsgc-enum-part-3/
4RSGC-JSON to Class- part 4
5RSGC-Constructor – Deconstructor – part 5
6RSGC – DTO Mapper – part 6
7RSGC – Skinny Controllers- part 7
8RSGC-Builder Design Pattern – part 8
9RSGC- MetadataFromObject – part 9
10RSGC- Dynamic Mock – part 10
11RSCG- Method Decorator – part 11
12RSCG – Curry – Partial function – part 12
13RSCG- part 13 – IFormattable
14RSCG- part 14 – DP_Decorator
15RSCG- part 15 – Expression Generator
16RSCG- part 16 – Many Others
17RSCG- the book
18RSCG–Template Rendering- part 17
19CI Version
20HttpClientGenerator
21Query from database
22AutoRegister
23TinyTypes
24Static2Interface
25AppSettings
26Properties
27
Roslyn Source Code Generators