JsonTypeDefinition 1.0.0-alpha001
dotnet add package JsonTypeDefinition --version 1.0.0-alpha001
NuGet\Install-Package JsonTypeDefinition -Version 1.0.0-alpha001
<PackageReference Include="JsonTypeDefinition" Version="1.0.0-alpha001" />
paket add JsonTypeDefinition --version 1.0.0-alpha001
#r "nuget: JsonTypeDefinition, 1.0.0-alpha001"
// 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 Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.ComponentModel.Annotations (>= 5.0.0)
-
net6.0
- System.ComponentModel.Annotations (>= 5.0.0)
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 | 217 | 2/13/2022 |