Universal.Common.Json
2.0.1
dotnet add package Universal.Common.Json --version 2.0.1
NuGet\Install-Package Universal.Common.Json -Version 2.0.1
<PackageReference Include="Universal.Common.Json" Version="2.0.1" />
<PackageVersion Include="Universal.Common.Json" Version="2.0.1" />
<PackageReference Include="Universal.Common.Json" />
paket add Universal.Common.Json --version 2.0.1
#r "nuget: Universal.Common.Json, 2.0.1"
#:package Universal.Common.Json@2.0.1
#addin nuget:?package=Universal.Common.Json&version=2.0.1
#tool nuget:?package=Universal.Common.Json&version=2.0.1
Universal.Common.Json
Creates, infers, validates, and extracts JSON using JSON Schema Draft 2020-12, and canonicalizes JSON using RFC 8785 (JCS).
Quick start
dotnet add package Universal.Common.Json
Generate a schema
using Universal.Common.Json;
JsonSchema schema = new JsonSchemaConverter().Convert<Person>();
// Use definitions and references for recursive object graphs.
JsonSchema recursiveSchema = new ReferencingJsonSchemaConverter()
.Convert<Employee>();
The referencing converter uses per-call state and collision-safe $defs identifiers for shared and recursive types. Nullable value types use a non-null branch plus null; string-key dictionaries use additionalProperties; object and JSON-value types are unconstrained. Byte arrays use base64 strings. Ordinary enums use their configured string names; flags use an unrestricted string schema to permit combinations.
JsonNumber generates a numeric schema. Optional CLR schema references and nullable numbers round-trip through both serializers, including array and dictionary values. Serializer-level null represents an absent CLR value; JsonSchema.Parse("null") and null values in schema keyword positions remain invalid JSON Schema documents.
Generation describes the supported CLR and attribute mappings; it does not reproduce arbitrary serializer configuration. Reference-type nullable annotations, custom converters, numeric enum serialization and RequiredAttribute whitespace/empty-string policies require consumer configuration or overrides. RequiredAttribute excludes null for nullable value properties; compiler-required and JSON-required members require presence while preserving their nullable value contract. Byte-array length attributes, non-string dictionary keys, multidimensional arrays and inline recursive types throw NotSupportedException.
Infer and validate
JsonSchema schema = new JsonSchemaInferrer()
.Infer("""{ "name": "Ada", "age": 36 }""");
JsonSchemaValidator.ValidationResult result = new JsonSchemaValidator(schema)
.Validate("""{ "name": "Grace", "age": 37 }""");
Parse and validate a schema with references
JsonSchema schema = JsonSchema.Parse(schemaJson);
JsonSchemaValidator.ValidationResult result = new JsonSchemaValidator(schema).Validate(instanceJson);
JsonSchema.Parse preserves $id, $ref, boolean schemas, explicit JSON null values and arbitrary-precision numbers. System.Text.Json and Newtonsoft.Json writers preserve the model's JSON values across serializer boundaries. Newtonsoft's reader may round floating-point input before a converter sees it; use JsonSchema.Parse or System.Text.Json when exact input precision matters. Non-finite reader values are rejected.
Use version 2.0.1 or later with Newtonsoft DOM APIs such as JObject.FromObject, JToken.FromObject and ToObject. Schema containers and ordinary scalar values become native JSON tokens. Numbers use exact integer, BigInteger or decimal tokens where possible, with raw numeric leaves for precision or exponents beyond those representations. This avoids expanding enormous exponents or rounding values. Numeric output spelling may normalize; JsonNumber.ToString() retains the original spelling.
Validation resolves JSON Pointers, anchors, bundled resources, recursive references and dynamic references, and propagates evaluated properties and items through applicators. Supply external schemas through JsonSchemaValidator.Options.Resources or SchemaResolver; the library performs no network retrieval. Missing resources and unsupported dialects or required vocabularies throw rather than reporting an invalid instance.
format and content keywords are annotations by default. Consumers can supply FormatValidator for format assertions, including the format-assertion vocabulary. The default pattern matcher supports .NET ECMAScript-mode BMP patterns with Unicode general-category aliases and ECMA whitespace and line-terminator rules. Supplementary Unicode, complemented whitespace inside character classes and unsupported syntax require a consumer-supplied PatternMatcher. Earlier schema drafts and the removed dependencies keyword are outside the Draft 2020-12 validation contract.
The tests include the pinned official Draft 2020-12 suite: all 1,300 core validation cases, lossless schema writer tests, supported optional profiles, and separate tests for explicitly unsupported profiles. Passing these cases does not establish support for every ECMA-262 regular expression or every optional format assertion.
Migrating from 1.x
Version 2 changes numeric constraint properties from int? and decimal? to JsonNumber?. Rebuild consuming assemblies and update code that reads these properties. Ordinary integer and decimal assignments remain supported through implicit conversions; use JsonNumber.Parse for values beyond platform numeric ranges. JsonNumber compares values mathematically and retains their original JSON spelling.
Schema-valued properties such as Items and AdditionalProperties now use JsonSchema directly. JsonSchemaOrBoolean is removed: replace .Items.Schema with .Items, inspect BooleanValue for boolean schemas, and continue assigning true or false through implicit conversion. Numeric reads support exact TryGetInt32, TryGetInt64 and TryGetDecimal, or explicit casts that throw on precision or range loss.
SelfReferencingJsonSchemaConverter is renamed to ReferencingJsonSchemaConverter. It no longer shares state across conversions or uses simple CLR names as definition identities. Generated nullable values, dictionaries, byte arrays, unconstrained values and ignored/static/indexer properties now have corrected shapes. Generated definition identifiers are implementation details; follow the schema's references rather than constructing identifiers externally.
Inference defaults allow additional properties, require only properties present in every object sample, infer null-only values as null, and leave empty-array items unconstrained. For the previous strict policy, supply new JsonSchemaInferrer.Options { AdditionalProperties = false, RequireAllProperties = true, DefaultLeafType = "string" }. Options.SchemaVersion is renamed to Dialect. String and JsonElement inference preserve JSON numbers without date or floating-point coercion.
Extractor matches are immutable: Value is a precision-preserving JsonElement, Text and Index are constructor-supplied, and Length is derived. Replace As<T>() with Deserialize<T>(JsonSerializerOptions). Extractors capture schema and validation policies, support object/array unions, and propagate schema failures. Scalar extraction remains unsupported. JsonSchemaType equality compares sets of type names; object equality no longer equates a schema type with a string. String comparison operators remain available.
Const = null and Default = null now explicitly emit those keywords. Use ClearConst() or ClearDefault() to remove them. JsonSchema can represent boolean schemas directly, including inside existing schema collections. Combining a boolean schema with object keywords throws on serialization. Keyword shapes are checked during parsing and serialization.
Validation treats mathematically integral numbers such as 1.0 as integers, compares numeric enum/const/unique values mathematically, counts Unicode code points for string lengths, and treats format as annotation unless a consumer assertion callback is configured. Unresolved schema references now throw; callers must distinguish unsupported schema capabilities from invalid instance data.
Extract matching JSON
JsonSchemaExtractor extractor = new JsonSchemaExtractor(schema);
foreach (JsonSchemaExtractor.Match match in extractor.Extract(input))
{
Console.WriteLine(match.Text);
}
Canonicalize JSON for signatures or hashing
string canonical = JsonCanonicalizer.Canonicalize("{\"z\": 2, \"a\": 1.0}");
// {"a":1,"z":2}
byte[] utf8 = JsonCanonicalizer.CanonicalizeToUtf8(json);
Canonicalization sorts object properties by ordinal UTF-16 order, preserves array order, and uses ECMAScript-compatible shortest IEEE 754 binary64 number formatting. It rejects duplicate property names, invalid Unicode, non-finite numbers, and invalid JSON. JSON nesting is limited to 100 levels. UTF-8 output has no byte-order mark.
Numbers follow the RFC's binary64 semantics; represent integers or decimal quantities requiring greater precision as JSON strings. Canonicalization does not normalize Unicode or compute a signature or hash. Targets .NET Standard 2.0 with C# 10 and nullable annotations; canonicalization adds no dependencies beyond the package's existing references.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. net8.0 was computed. 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 was computed. 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 was computed. 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. |
| .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
- Newtonsoft.Json (>= 13.0.4)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Text.Json (>= 10.0.14)
NuGet packages (14)
Showing the top 5 NuGet packages that depend on Universal.Common.Json:
| Package | Downloads |
|---|---|
|
Universal.Operative.Sdk
Shared kernel for the Universal.Operative.Sdk family: content Block model, ITool/ToolResult/ToolDefinition/ToolChoice, Usage, and opt-in standard .NET logging. Provider/execution-model neutral -- pair with Universal.Operative.Sdk.Discrete (turn-based) or Universal.Operative.Sdk.Realtime (duplex-stream). |
|
|
Universal.Anthropic.Client
Class library for interacting with the Anthropic REST API. |
|
|
Universal.OpenAI.Client
Class library for interacting with OpenAI APIs. |
|
|
Universal.Design
Canonical design-system model, validation, inheritance, parameterization, and resolution. |
|
|
Universal.Microsoft.Foundry.Client
Standalone HTTP client for Microsoft Foundry project discovery and OpenAI, Anthropic, and MAI inference APIs. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.1 | 5,310 | 11/11/2026 |
| 2.0.0 | 93 | 11/10/2026 |
| 1.11.0 | 340 | 11/10/2026 |
| 1.10.0 | 3,780 | 9/9/2026 |
| 1.9.1 | 1,638 | 9/4/2026 |
| 1.9.0 | 4,683 | 7/28/2026 |
| 1.8.0 | 801 | 7/20/2026 |
| 1.7.0 | 948 | 7/6/2026 |
| 1.6.0 | 478 | 6/19/2026 |
| 1.5.1 | 925 | 4/3/2026 |
| 1.5.0 | 2,690 | 11/13/2024 |
| 1.4.0 | 242 | 11/11/2024 |
| 1.3.0 | 228 | 11/6/2024 |
| 1.2.0 | 228 | 11/6/2024 |
| 1.1.0 | 209 | 10/30/2024 |
| 1.0.2 | 455 | 9/4/2024 |
| 1.0.1 | 268 | 9/4/2024 |
Fixes Newtonsoft schema/number DOM composition and nullable schema/number reads. Generates numeric schemas for JsonNumber and preserves exact numeric values, including extreme precision and exponents.