DotNetJ2Class 1.1.0

dotnet add package DotNetJ2Class --version 1.1.0
                    
NuGet\Install-Package DotNetJ2Class -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotNetJ2Class" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetJ2Class" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="DotNetJ2Class" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotNetJ2Class --version 1.1.0
                    
#r "nuget: DotNetJ2Class, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DotNetJ2Class@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DotNetJ2Class&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=DotNetJ2Class&version=1.1.0
                    
Install as a Cake Tool

⚠️ THIS LIB IS UNDER MAINTENANCE

DotNet.J2Class

NuGet

DotNet.J2Class generates plain .NET POCO types at runtime from JSON payloads using Reflection.Emit. It's useful for quickly mapping unknown or evolving JSON schemas into runtime-accessible objects without maintaining static DTOs.

Quick highlights

  • Runtime type generation using TypeBuilder/PropertyBuilder and ILGenerator.
  • Supports nested objects and arrays (primitive and object arrays are handled).
  • Caches generated types by schema signature to avoid re-emitting identical types.

Getting started

Install (NuGet):

Install-Package DotNetJ2Class

Basic usage

using DotNet.J2Class;

string simple = "{'Foo':'bar'}";
var obj = J2Class.CreateObjectFromJson(simple, "MyClass");
dynamic dyn = obj;
Console.WriteLine(dyn.Foo); // -> bar

string complex = "{'Person': {'Name':'Alice','Age':30}, 'Tags': ['x','y']}";
var obj2 = J2Class.CreateObjectFromComplexJson(complex, "RootClass");
dynamic d2 = obj2;
Console.WriteLine(d2.Person.Name); // -> Alice

Commands

dotnet build
dotnet test
dotnet run --project test/ConsoleApp1

Important notes / gotchas

  • The project targets net9.0 (see src/DotNet.J2Class/DotNet.J2Class.csproj).
  • Parsing: inputs using single quotes are normalized internally, but prefer valid JSON (double quotes) for robustness.
  • Arrays: homogeneous primitive arrays are materialized as typed List<T> (e.g. List<string>); mixed or empty arrays fall back to List<object>.
  • Exceptions: CreateObjectFromJson wraps failures in InvalidOperationException to surface errors; CreateObjectFromComplexJson preserves thrown exceptions for easier debugging in some paths.
  • Caching: generated types are cached by a schema signature — this improves performance but increases runtime memory usage for many distinct schemas.

Testing

  • Tests live in test/DotNet.J2Class.Tests and use NUnit. They cover simple, complex, array, concurrency and basic performance scenarios.

Contributing

  • PRs welcome. For larger changes open an issue first.
  • When changing parsing or type generation, add tests in test/DotNet.J2Class.Tests demonstrating expected behaviors.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 87 1/24/2026
1.0.3 652 4/20/2022
1.0.2 537 5/8/2021
1.0.1 502 5/1/2021
1.0.0 613 8/17/2020

Beta Version