JsonTypeDefinition 1.0.0-alpha001

.NET 6.0 .NET Standard 2.0
This is a prerelease version of JsonTypeDefinition.
dotnet add package JsonTypeDefinition --version 1.0.0-alpha001
NuGet\Install-Package JsonTypeDefinition -Version 1.0.0-alpha001
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="JsonTypeDefinition" Version="1.0.0-alpha001" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonTypeDefinition --version 1.0.0-alpha001
#r "nuget: JsonTypeDefinition, 1.0.0-alpha001"
#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.
// Install JsonTypeDefinition as a Cake Addin
#addin nuget:?package=JsonTypeDefinition&version=1.0.0-alpha001&prerelease

// Install JsonTypeDefinition as a Cake Tool
#tool nuget:?package=JsonTypeDefinition&version=1.0.0-alpha001&prerelease

JsonTypeDefinition

An RFC 8927 compliant parser for .NET.

Json Type Definitions (JTDs) provide a human-readable, portable type description that can be consumed by any programming language.

A common use-case would be a REST API describing the form in which payloads are expected. It could publicize this form as a Json Type Definition which could subsequently be interpreted by any client independent of their technology stack.

</br>

Installation

dotnet add package JsonTypeDefinition

</br>

Usage

The JsonTypeDefinitionParser provides two methods:

var x = JsonTypeDefinitionParser.Parse(typeof(User));
var y = JsonTypeDefinitionParser.Parse<User>();

Both methods will produce the same output. Given the following types:

class User
{
    [Required]
    public string Name { get; set; }
    public Address Address { get; set; }
    public DateTimeOffset JoinedAt { get; set; }
    public double GPA { get; set; }
}

class Address 
{
    public string StreetName { get; set; }
    public int HouseNumber { get; set; }
}

The parser would produce the following Json Type Definition:

{
    "properties": {
        "Name": { "type": "string" }
    },
    "optionalProperties": {
        "Address": { "ref": "Address" },
        "JoinedAt": { "type": "timestamp" },
        "GPA": { "type": "float64" }
    },
    "definitions": {
        "Adress": {
            "optionalProperties": {
                "StreetName": { "type": "string" },
                "HouseNumber": { "type": "int32" }
            }
        }
    }
}

Do note that JsonTypeDefinitionParser.Parse returns a record of type RootSchema and has no reference to Newtonsoft.Json or System.Text.Json. Feel free to use your favorite JSON serializer to get the appropriate JSON representation! Recommended serialization settings:

// Newtonsoft.Json:
var settings = new Newtonsoft.Json.JsonSerializerSettings()
{
    NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
    Converters = new Newtonsoft.Json.JsonConverter[] { new Newtonsoft.Json.Converters.StringEnumConverter() }
};

// System.Text.Json:
var options = new System.Text.Json.JsonSerializerOptions { IgnoreNullValues = true };
options.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());

</br>

Limitations

When defining the models it is important to design them around the limitations that come with a portable, consumer agnostic type definition.

Some commonly used .NET types are currently not supported, including System.TimeSpan. Imagine what would happen if you were to create a JTD from a time span object. RFC 8927 does not define a fitting primitive type that could be used (timestamp is more akin to System.DateTimeOffset) as there is no universally agreed upon way to describe durations. 10 minutes could be described as 00:10:00, PT10M (ISO 8601), 3600 (in seconds), etc. There would be no way for consumers of the JTD to know which notation to use. Instead you're encouraged to work around these limitations for example by replacing

public TimeSpan TimeToLive { get; set; }

with

public double TimeToLiveInSeconds { get; set; }

so that any consumer nows exactly what format you expect.

</br>

Future improvements

Contributions are welcome 😃

  • custom handlers for handling otherwise unsupported types
  • generate .NET types from JTDs
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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.0-alpha001 179 2/13/2022