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

1
2
3
4
5
6
7
{
 
"FirstName": "Andrei",
 
"LastName": "Ignat",
 

The code that you will use is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
var p1 = new Person();
 
 
var config = new ConfigurationBuilder()
 
  .AddJsonFile("persons.json")
 
  .Build();
 
 
 
var p = config.Get<Person>();
 
var p2 = Person.FromConfig(config);

 

The code that is generated is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[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

Roslyn Source Code Generators