https://github.com/ktsu-dev/njsonschemajsonconverter
https://github.com/ktsu-dev/njsonschemajsonconverter
Last synced: 29 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/ktsu-dev/njsonschemajsonconverter
- Owner: ktsu-dev
- License: mit
- Created: 2024-12-30T06:01:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-02-14T01:50:27.000Z (4 months ago)
- Last Synced: 2026-02-14T08:51:24.590Z (4 months ago)
- Language: C#
- Size: 229 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Authors: AUTHORS.md
- Copyright: COPYRIGHT.md
Awesome Lists containing this project
README
# ktsu.NJsonSchemaJsonConverter
> A `JsonConverterFactory` for System.Text.Json that enables seamless serialization and deserialization of NJsonSchema `JsonSchema` objects.
[](https://github.com/ktsu-dev/NJsonSchemaJsonConverter/blob/main/LICENSE.md)
[](https://www.nuget.org/packages/ktsu.NJsonSchemaJsonConverter/)
[](https://www.nuget.org/packages/ktsu.NJsonSchemaJsonConverter/)
[](https://github.com/ktsu-dev/NJsonSchemaJsonConverter/actions)
[](https://github.com/ktsu-dev/NJsonSchemaJsonConverter/stargazers)
## Introduction
`ktsu.NJsonSchemaJsonConverter` bridges the gap between [NJsonSchema](https://github.com/RicoSuter/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
```powershell
Install-Package ktsu.NJsonSchemaJsonConverter
```
### .NET CLI
```bash
dotnet add package ktsu.NJsonSchemaJsonConverter
```
### Package Reference
```xml
```
## Usage Examples
### Basic Setup
Register the converter factory in your `JsonSerializerOptions`:
```csharp
using ktsu.NJsonSchemaJsonConverter;
using System.Text.Json;
var options = new JsonSerializerOptions
{
Converters = { new NJsonSchemaJsonConverterFactory() }
};
```
### Serializing an Object Containing a JsonSchema
```csharp
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();
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
```csharp
// Deserialize - the schema is parsed back into a JsonSchema object
var result = JsonSerializer.Deserialize(json, options);
// result.Schema is a fully functional JsonSchema instance
Console.WriteLine(result?.Schema?.Properties.Count);
```
### Using with Dependency Injection
```csharp
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` 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](LICENSE.md) file for details.