Skip to main content

RSCG_JSON2Class by Andrei Ignat

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: RSCG_JSON2Class

This package transforms any json into a class

Author: Andrei Ignat

NuGet: https://www.nuget.org/packages/RSCG_JSON2Class/

You can find more details at https://github.com/ignatandrei/RSCG_JSON2Class

Source : https://github.com/ignatandrei/RSCG_JSON2Class

Original Readme

note

RSCG_JSON2Class

Transform any json ( including appsettings ) into a class

Usage

Install the nuget package into your project

<ItemGroup>
<PackageReference Include="RSCG_JSON2Class" Version="2024.2.29.807" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

Verify that the json file is set analyzer ( and , if you want to deserialize, copy to the output directory)

<ItemGroup>
<None Remove="testData.json" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="testData.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AdditionalFiles>
</ItemGroup>

Access from C# code

using System.Text.Json;
var testData = JsonSerializer.Deserialize<JSON2ClassConsole.SettingsJson.testData>(System.IO.File.ReadAllText("testData.json"));
ArgumentNullException.ThrowIfNull(testData);
Console.WriteLine(testData.Logging.LogLevel.Default);
Console.WriteLine(testData.DictData.Number_2);

About

note

transform any json into a class

How to use

Example ( source csproj, source files )

This is the CSharp Project that references RSCG_JSON2Class

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="testData.json" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="testData.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AdditionalFiles>
</ItemGroup>


<ItemGroup>
<PackageReference Include="RSCG_JSON2Class" Version="2024.2.29.807" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>


</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace JSON2ClassConsole.SettingsJson
{

//[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("RSCG_JSON2Class", "2024.2.29.807")]
public partial class LogLevel
{
public object GetFromPropertyName(string propName, bool returnNull =false){

propName=propName?.ToUpper();

switch(propName){

case "DEFAULT":
return this.Default ;

case "MICROSOFT":
return this.Microsoft ;

case "MICROSOFTHOSTINGLIFETIME":
return this.MicrosoftHostingLifetime ;

default:
if(returnNull)
return null;

throw new ArgumentException("cannot found from LogLevel prop "+propName);

}


}

public IEnumerable<string> Properties(){

yield return "Default" ;

yield return "Microsoft" ;

yield return "MicrosoftHostingLifetime" ;

yield break;
}

[System.Text.Json.Serialization.JsonPropertyName("Default")]
public string Default { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("Microsoft")]
public string Microsoft { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("MicrosoftHostingLifetime")]
public string MicrosoftHostingLifetime { get; set; }


}

//[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("RSCG_JSON2Class", "2024.2.29.807")]
public partial class Logging
{
public object GetFromPropertyName(string propName, bool returnNull =false){

propName=propName?.ToUpper();

switch(propName){

case "LOGLEVEL":
return this.LogLevel ;

default:
if(returnNull)
return null;

throw new ArgumentException("cannot found from Logging prop "+propName);

}


}

public IEnumerable<string> Properties(){

yield return "LogLevel" ;

yield break;
}

[System.Text.Json.Serialization.JsonPropertyName("LogLevel")]
public LogLevel LogLevel { get; set; }


}

//[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("RSCG_JSON2Class", "2024.2.29.807")]
public partial class PropChars
{
public object GetFromPropertyName(string propName, bool returnNull =false){

propName=propName?.ToUpper();

switch(propName){

case "A":
return this.A ;

default:
if(returnNull)
return null;

throw new ArgumentException("cannot found from PropChars prop "+propName);

}


}

public IEnumerable<string> Properties(){

yield return "A" ;

yield break;
}

[System.Text.Json.Serialization.JsonPropertyName("a")]
public string A { get; set; }


}

//[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("RSCG_JSON2Class", "2024.2.29.807")]
public partial class NoProp
{
public object GetFromPropertyName(string propName, bool returnNull =false){

propName=propName?.ToUpper();

switch(propName){

default:
if(returnNull)
return null;

throw new ArgumentException("cannot found from NoProp prop "+propName);

}


}

public IEnumerable<string> Properties(){

yield break;
}


}

//[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("RSCG_JSON2Class", "2024.2.29.807")]
public partial class Test
{
public object GetFromPropertyName(string propName, bool returnNull =false){

propName=propName?.ToUpper();

switch(propName){

case "NUMBER_1234A":
return this.Number_1234a ;

default:
if(returnNull)
return null;

throw new ArgumentException("cannot found from Test prop "+propName);

}


}

public IEnumerable<string> Properties(){

yield return "Number_1234a" ;

yield break;
}

[System.Text.Json.Serialization.JsonPropertyName("1234a")]
public IList<string> Number_1234a { get; set; }


}

//[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("RSCG_JSON2Class", "2024.2.29.807")]
public partial class Number_55
{
public object GetFromPropertyName(string propName, bool returnNull =false){

propName=propName?.ToUpper();

switch(propName){

case "A":
return this.A ;

case "NUMBER_2":
return this.Number_2 ;

default:
if(returnNull)
return null;

throw new ArgumentException("cannot found from Number_55 prop "+propName);

}


}

public IEnumerable<string> Properties(){

yield return "A" ;

yield return "Number_2" ;

yield break;
}

[System.Text.Json.Serialization.JsonPropertyName("a")]
public string A { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("2")]
public string Number_2 { get; set; }


}

//[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("RSCG_JSON2Class", "2024.2.29.807")]
public partial class DictData
{
public object GetFromPropertyName(string propName, bool returnNull =false){

propName=propName?.ToUpper();

switch(propName){

case "TEST":
return this.Test ;

case "A":
return this.A ;

case "NUMBER_2":
return this.Number_2 ;

case "MYNUMBER":
return this.MyNumber ;

case "NUMBER_55":
return this.Number_55 ;

default:
if(returnNull)
return null;

throw new ArgumentException("cannot found from DictData prop "+propName);

}


}

public IEnumerable<string> Properties(){

yield return "Test" ;

yield return "A" ;

yield return "Number_2" ;

yield return "MyNumber" ;

yield return "Number_55" ;

yield break;
}

[System.Text.Json.Serialization.JsonPropertyName("Test")]
public Test Test { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("A")]
public string A { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("2")]
public string Number_2 { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("MyNumber")]
public int MyNumber { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("55")]
public Number_55 Number_55 { get; set; }


}

//[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("RSCG_JSON2Class", "2024.2.29.807")]
public partial class testData
{
public object GetFromPropertyName(string propName, bool returnNull =false){

propName=propName?.ToUpper();

switch(propName){

case "LOGGING":
return this.Logging ;

case "PROPCHARS":
return this.PropChars ;

case "NOPROP":
return this.NoProp ;

case "ALLOWEDHOSTS":
return this.AllowedHosts ;

case "MYTEST":
return this.MyTest ;

case "DICTDATA":
return this.DictData ;

default:
if(returnNull)
return null;

throw new ArgumentException("cannot found from testData prop "+propName);

}


}

public IEnumerable<string> Properties(){

yield return "Logging" ;

yield return "PropChars" ;

yield return "NoProp" ;

yield return "AllowedHosts" ;

yield return "MyTest" ;

yield return "DictData" ;

yield break;
}

[System.Text.Json.Serialization.JsonPropertyName("Logging")]
public Logging Logging { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("PropChars")]
public PropChars PropChars { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("NoProp")]
public NoProp NoProp { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("AllowedHosts")]
public string AllowedHosts { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("MyTest")]
public string MyTest { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("DictData")]
public DictData DictData { get; set; }


}

}

Usefull

Download Example (.NET C# )

Share RSCG_JSON2Class

https://ignatandrei.github.io/RSCG_Examples/v2/docs/RSCG_JSON2Class

In the same category (FilesToCode) - 13 other generators

Chorn.EmbeddedResourceAccessGenerator

corecraft

Datacute.EmbeddedResourcePropertyGenerator

DotnetYang

EmbedResourceCSharp

LingoGen

NotNotAppSettings

Podimo.ConstEmbed

ResXGenerator

RSCG_Utils

ThisAssembly_Resources

ThisAssembly.Strings

Weave