⚠️ THIS LIB IS UNDER MAINTENANCE
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/PropertyBuilderandILGenerator. - Supports nested objects and arrays (primitive and object arrays are handled).
- Caches generated types by schema signature to avoid re-emitting identical types.
Install (NuGet):
Install-Package DotNetJ2Classusing 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); // -> Alicedotnet build
dotnet test
dotnet run --project test/ConsoleApp1Important notes / gotchas
- The project targets
net9.0(seesrc/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 toList<object>. - Exceptions:
CreateObjectFromJsonwraps failures inInvalidOperationExceptionto surface errors;CreateObjectFromComplexJsonpreserves 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.
- Tests live in
test/DotNet.J2Class.Testsand use NUnit. They cover simple, complex, array, concurrency and basic performance scenarios.
- PRs welcome. For larger changes open an issue first.
- When changing parsing or type generation, add tests in
test/DotNet.J2Class.Testsdemonstrating expected behaviors.