ktsu.NJsonSchemaJsonConverter 1.0.54

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package ktsu.NJsonSchemaJsonConverter --version 1.0.54
                    
NuGet\Install-Package ktsu.NJsonSchemaJsonConverter -Version 1.0.54
                    
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="ktsu.NJsonSchemaJsonConverter" Version="1.0.54" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ktsu.NJsonSchemaJsonConverter" Version="1.0.54" />
                    
Directory.Packages.props
<PackageReference Include="ktsu.NJsonSchemaJsonConverter" />
                    
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 ktsu.NJsonSchemaJsonConverter --version 1.0.54
                    
#r "nuget: ktsu.NJsonSchemaJsonConverter, 1.0.54"
                    
#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 ktsu.NJsonSchemaJsonConverter@1.0.54
                    
#: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=ktsu.NJsonSchemaJsonConverter&version=1.0.54
                    
Install as a Cake Addin
#tool nuget:?package=ktsu.NJsonSchemaJsonConverter&version=1.0.54
                    
Install as a Cake Tool

ktsu.NJsonSchemaJsonConverter

A JsonConverterFactory for System.Text.Json that enables seamless serialization and deserialization of NJsonSchema JsonSchema objects.

License NuGet Version NuGet Version NuGet Downloads GitHub commit activity GitHub contributors GitHub Actions Workflow Status

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 JsonSchema objects to/from JSON
  • Subclass support - Automatically handles any type derived from JsonSchema
  • Drop-in integration - Register as a converter in JsonSerializerOptions and 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 using WriteRawValue
  • 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.

Deserializing never touches the network or the file system. A $ref that points into the same document (#/definitions/...) is resolved as usual. A $ref to an external document, such as http://..., https://... or a file path, is rejected with a JsonException and nothing is fetched, so a schema read from untrusted input cannot make the process issue requests.

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:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  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 is compatible.  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.0.55-pre.1 53 10/8/2026
1.0.54 99 10/7/2026
1.0.53 89 10/7/2026
1.0.53-pre.1 56 10/6/2026
1.0.52 117 9/29/2026
1.0.52-pre.1 64 9/28/2026
1.0.51 110 9/27/2026
1.0.50 115 9/26/2026
1.0.49 109 9/24/2026
1.0.48 109 9/23/2026
1.0.47 102 9/22/2026
1.0.46 110 9/21/2026
1.0.45 121 9/16/2026
1.0.44 110 9/14/2026
1.0.43 125 9/11/2026
1.0.42 137 9/3/2026
1.0.41 129 8/28/2026
1.0.40 123 8/26/2026
1.0.39 142 8/24/2026
1.0.38 133 8/21/2026
Loading failed

## v1.0.54 (patch)

Changes since v1.0.53:

- Keep the overridden ResolveUrlReferenceAsync's default cancellation token ([@Claude](https://github.com/Claude))
- Drop the unreachable file-reference override and pin that file refs are rejected ([@Claude](https://github.com/Claude))
- Move the external-reference tests to the end of the class ([@Claude](https://github.com/Claude))
- Refuse external $ref documents instead of fetching them [patch] ([@Claude](https://github.com/Claude))