ktsu.NJsonSchemaJsonConverter 1.0.2
ktsu.NJsonSchemaJsonConverter
A
JsonConverterFactoryfor System.Text.Json that enables seamless serialization and deserialization of NJsonSchemaJsonSchemaobjects.
Introduction
ktsu.NJsonSchemaJsonConverter bridges the gap between NJsonSchema and System.Text.Json. NJsonSchema provides its own JSON parsing and generation methods, but doesn't integrate natively with System.Text.Json.Serialization. This library provides a JsonConverterFactory that allows JsonSchema objects (and subclasses) to be serialized and deserialized as part of larger object graphs using System.Text.Json.JsonSerializer.
Features
- Full JsonSchema round-tripping - Serialize and deserialize
JsonSchemaobjects to/from JSON - Subclass support - Automatically handles any type derived from
JsonSchema - Drop-in integration - Register as a converter in
JsonSerializerOptionsand it just works - Lightweight - Single source file, minimal dependencies
Installation
Package Manager Console
Install-Package ktsu.NJsonSchemaJsonConverter
.NET CLI
dotnet add package ktsu.NJsonSchemaJsonConverter
Package Reference
<PackageReference Include="ktsu.NJsonSchemaJsonConverter" Version="x.y.z" />
Usage Examples
Basic Setup
Register the converter factory in your JsonSerializerOptions:
using ktsu.NJsonSchemaJsonConverter;
using System.Text.Json;
var options = new JsonSerializerOptions
{
Converters = { new NJsonSchemaJsonConverterFactory() }
};
Serializing an Object Containing a JsonSchema
using NJsonSchema;
using System.Text.Json;
public class SchemaContainer
{
public string Name { get; set; } = string.Empty;
public JsonSchema? Schema { get; set; }
}
// Create a schema
var schema = await JsonSchema.FromTypeAsync<MyModel>();
var container = new SchemaContainer
{
Name = "MyModel Schema",
Schema = schema
};
// Serialize - the schema is written as an inline JSON object
string json = JsonSerializer.Serialize(container, options);
Deserializing an Object Containing a JsonSchema
// Deserialize - the schema is parsed back into a JsonSchema object
var result = JsonSerializer.Deserialize<SchemaContainer>(json, options);
// result.Schema is a fully functional JsonSchema instance
Console.WriteLine(result?.Schema?.Properties.Count);
Using with Dependency Injection
services.AddSingleton(new JsonSerializerOptions
{
Converters = { new NJsonSchemaJsonConverterFactory() }
});
How It Works
The converter handles JsonSchema serialization in two directions:
- Writing: Calls
JsonSchema.ToJson()to produce the schema's JSON representation and writes it inline usingWriteRawValue - Reading: Reads the JSON token as a string and parses it using
JsonSchema.FromJsonAsync()to reconstruct the schema object
The factory uses reflection to create type-specific generic converter instances, supporting JsonSchema and any subclass.
API Reference
NJsonSchemaJsonConverterFactory
A JsonConverterFactory that creates converters for JsonSchema and its subclasses.
| Method | Description |
|---|---|
CanConvert(Type) |
Returns true for JsonSchema and any type deriving from it |
CreateConverter(Type, JsonSerializerOptions) |
Creates a type-specific JsonConverter<T> instance via reflection |
Contributing
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please make sure to update tests as appropriate.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
No packages depend on ktsu.NJsonSchemaJsonConverter.
.NET 8.0
- NJsonSchema (>= 11.5.2)
- Polyfill (>= 9.8.1)
.NET 9.0
- NJsonSchema (>= 11.5.2)
- Polyfill (>= 9.8.1)
.NET 10.0
- NJsonSchema (>= 11.5.2)
- Polyfill (>= 9.8.1)
| Version | Downloads | Last updated |
|---|---|---|
| 1.0.6 | 1 | 02/16/2026 |
| 1.0.5 | 2 | 02/16/2026 |
| 1.0.4 | 15 | 02/14/2026 |
| 1.0.3 | 15 | 02/14/2026 |
| 1.0.2 | 53 | 02/10/2026 |
| 1.0.2-pre.3 | 50 | 02/08/2026 |
| 1.0.2-pre.2 | 55 | 02/04/2026 |
| 1.0.2-pre.1 | 51 | 02/03/2026 |
| 1.0.1 | 54 | 01/31/2026 |