ktsu.NJsonSchemaJsonConverter 1.0.6

ktsu.NJsonSchemaJsonConverter

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

License NuGet NuGet Downloads Build Status GitHub Stars

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.

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.

No packages depend on ktsu.NJsonSchemaJsonConverter.

## v1.0.6 (patch) Changes since v1.0.5: - Merge remote-tracking branch 'refs/remotes/origin/main' ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.5 (patch) Changes since v1.0.4: - Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot])) - Bump Polyfill from 9.8.1 to 9.9.0 ([@dependabot[bot]](https://github.com/dependabot[bot])) ## v1.0.4 (patch) Changes since v1.0.3: - Remove legacy build scripts ([@matt-edmondson](https://github.com/matt-edmondson)) ## v1.0.3 (patch) Changes since v1.0.2: - Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync global.json ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.2 (patch) Changes since v1.0.1: - Add edge case and container tests ([@matt-edmondson](https://github.com/matt-edmondson)) - Add serialization and deserialization tests ([@matt-edmondson](https://github.com/matt-edmondson)) - Add CanConvert tests for NJsonSchemaJsonConverterFactory ([@matt-edmondson](https://github.com/matt-edmondson)) - Add test project scaffold ([@matt-edmondson](https://github.com/matt-edmondson)) - Add TAGS.md for NuGet package discoverability ([@matt-edmondson](https://github.com/matt-edmondson)) - Update README.md with installation instructions, usage examples, and API reference ([@matt-edmondson](https://github.com/matt-edmondson)) ## v1.0.2-pre.3 (prerelease) Changes since v1.0.2-pre.2: - Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync scripts\update-winget-manifests.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.2-pre.2 (prerelease) Changes since v1.0.2-pre.1: - Bump Polyfill from 9.8.0 to 9.8.1 ([@dependabot[bot]](https://github.com/dependabot[bot])) - Bump MSTest.Sdk from 4.0.2 to 4.1.0 ([@dependabot[bot]](https://github.com/dependabot[bot])) ## v1.0.2-pre.1 (prerelease) No significant changes detected since v1.0.2. ## v1.0.1 (patch) Changes since v1.0.0: - Add CLAUDE.md for project guidance and update package references in Directory.Packages.props and global.json ([@matt-edmondson](https://github.com/matt-edmondson)) - Update configuration files and scripts for improved build and test processes ([@matt-edmondson](https://github.com/matt-edmondson)) ## v1.0.1-pre.18 (prerelease) Changes since v1.0.1-pre.17: - Merge remote-tracking branch 'refs/remotes/origin/main' ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .editorconfig ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .gitattributes ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .gitignore ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .mailmap ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .runsettings ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.1-pre.17 (prerelease) Changes since v1.0.1-pre.16: - Update ktsu.Extensions to 1.5.5 ([@dependabot[bot]](https://github.com/dependabot[bot])) ## v1.0.1-pre.16 (prerelease) Changes since v1.0.1-pre.15: - Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .editorconfig ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .gitattributes ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .gitignore ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .mailmap ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync .runsettings ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.1-pre.15 (prerelease) Changes since v1.0.1-pre.14: ## v1.0.1-pre.14 (prerelease) Changes since v1.0.1-pre.13: ## v1.0.1-pre.13 (prerelease) Changes since v1.0.1-pre.12: ## v1.0.1-pre.12 (prerelease) Changes since v1.0.1-pre.11: ## v1.0.1-pre.11 (prerelease) Changes since v1.0.1-pre.10: ## v1.0.1-pre.10 (prerelease) Changes since v1.0.1-pre.9: ## v1.0.1-pre.9 (prerelease) Changes since v1.0.1-pre.8: ## v1.0.1-pre.8 (prerelease) Changes since v1.0.1-pre.7: ## v1.0.1-pre.7 (prerelease) Changes since v1.0.1-pre.6: ## v1.0.1-pre.6 (prerelease) Changes since v1.0.1-pre.5: ## v1.0.1-pre.5 (prerelease) Changes since v1.0.1-pre.4: ## v1.0.1-pre.4 (prerelease) Changes since v1.0.1-pre.3: - Bump ktsu.Extensions from 1.5.2 to 1.5.3 in the ktsu group ([@dependabot[bot]](https://github.com/dependabot[bot])) ## v1.0.1-pre.3 (prerelease) Changes since v1.0.1-pre.2: ## v1.0.1-pre.2 (prerelease) Changes since v1.0.1-pre.1: ## v1.0.1-pre.1 (prerelease) No significant changes detected since v1.0.1. ## v1.0.0 Changes since v1.0.0-pre.19: - Remove obsolete build configuration files and scripts, including Directory.Build.props, Directory.Build.targets, and various PowerShell scripts for metadata and version management. Add copyright information to NJsonSchemaJsonConverter.cs. ([@matt-edmondson](https://github.com/matt-edmondson)) - Update project SDK to ktsu.Sdk.Lib version 1.8.0 ([@matt-edmondson](https://github.com/matt-edmondson)) - Update packages ([@matt-edmondson](https://github.com/matt-edmondson)) - Add LICENSE template ([@matt-edmondson](https://github.com/matt-edmondson)) ## v1.0.0-pre.19 (prerelease) Changes since v1.0.0-pre.18: - Bump ktsu.Extensions from 1.3.2 to 1.4.0 in the ktsu group ([@dependabot[bot]](https://github.com/dependabot[bot])) ## v1.0.0-pre.18 (prerelease) Changes since v1.0.0-pre.17: - Sync scripts\make-version.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync scripts\make-changelog.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.0-pre.17 (prerelease) Changes since v1.0.0-pre.16: - Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.0-pre.16 (prerelease) Changes since v1.0.0-pre.15: - Sync scripts\make-version.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync scripts\make-changelog.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.0-pre.15 (prerelease) Changes since v1.0.0-pre.14: - Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.0-pre.14 (prerelease) Changes since v1.0.0-pre.13: - Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.0-pre.13 (prerelease) Changes since v1.0.0-pre.12: ## v1.0.0-pre.12 (prerelease) Changes since v1.0.0-pre.11: ## v1.0.0-pre.11 (prerelease) Changes since v1.0.0-pre.10: - Bump MSTest from 3.7.2 to 3.7.3 ([@dependabot[bot]](https://github.com/dependabot[bot])) ## v1.0.0-pre.10 (prerelease) Changes since v1.0.0-pre.9: ## v1.0.0-pre.9 (prerelease) Changes since v1.0.0-pre.8: ## v1.0.0-pre.8 (prerelease) Changes since v1.0.0-pre.7: - Bump MSTest from 3.7.1 to 3.7.2 ([@dependabot[bot]](https://github.com/dependabot[bot])) ## v1.0.0-pre.7 (prerelease) Changes since v1.0.0-pre.6: ## v1.0.0-pre.6 (prerelease) Changes since v1.0.0-pre.5: - Bump coverlet.collector from 6.0.3 to 6.0.4 ([@dependabot[bot]](https://github.com/dependabot[bot])) ## v1.0.0-pre.5 (prerelease) Changes since v1.0.0-pre.4: ## v1.0.0-pre.4 (prerelease) Changes since v1.0.0-pre.3: ## v1.0.0-pre.3 (prerelease) Changes since v1.0.0-pre.2: - Bump MSTest from 3.7.0 to 3.7.1 ([@dependabot[bot]](https://github.com/dependabot[bot])) ## v1.0.0-pre.2 (prerelease) Changes since v1.0.0-pre.1: ## v1.0.0-pre.1 (prerelease) No significant changes detected since v1.0.0. ## v0.0.1-pre.1 (prerelease) - Add mailmap ([@matt-edmondson](https://github.com/matt-edmondson)) - Add automation scripts for metadata generation and project management ([@matt-edmondson](https://github.com/matt-edmondson)) - Initial commit ([@matt-edmondson](https://github.com/matt-edmondson))

.NET 8.0

.NET 9.0

.NET 10.0

Version Downloads Last updated
1.0.6 2 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