Purview.ZodSharp.NewtonsoftJson
3.0.0
dotnet add package Purview.ZodSharp.NewtonsoftJson --version 3.0.0
NuGet\Install-Package Purview.ZodSharp.NewtonsoftJson -Version 3.0.0
<PackageReference Include="Purview.ZodSharp.NewtonsoftJson" Version="3.0.0" />
<PackageVersion Include="Purview.ZodSharp.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Purview.ZodSharp.NewtonsoftJson" />
paket add Purview.ZodSharp.NewtonsoftJson --version 3.0.0
#r "nuget: Purview.ZodSharp.NewtonsoftJson, 3.0.0"
#:package Purview.ZodSharp.NewtonsoftJson@3.0.0
#addin nuget:?package=Purview.ZodSharp.NewtonsoftJson&version=3.0.0
#tool nuget:?package=Purview.ZodSharp.NewtonsoftJson&version=3.0.0
Purview.ZodSharp.NewtonsoftJson
Newtonsoft.Json integration for Purview.ZodSharp . Deserialize-and-validate JSON in a single pass, create validating
JsonConverter instances, and import JSON Schema definitions (Z.FromJsonSchema).
Installation
dotnet add package Purview.ZodSharp.NewtonsoftJson
Deserialize and validate
using ZodSharp;
var userSchema = Z.Object()
.Field("name", Z.String().Min(3))
.Field("age", Z.Number().Min(0).Int())
.Build();
var json = """{ "name": "John", "age": 30 }""";
var result = userSchema.DeserializeAndValidate(json);
if (result.IsSuccess)
Console.WriteLine($"Valid: {result.Value}");
Async stream overload and JToken overload:
await using var stream = File.OpenRead("user.json");
var result = await userSchema.DeserializeAndValidateAsync(stream);
var jToken = JObject.Parse(json);
var result2 = userSchema.DeserializeAndValidate(jToken);
Validating converter
var converter = userSchema.CreateValidatingConverter();
var value = JsonConvert.DeserializeObject<User>(json, converter);
Errors
A failure to read the JSON (malformed text, a null document, a value the serializer cannot convert) is reported as one
error with code invalid_json (NewtonsoftJsonExtensions.InvalidJsonCode), category invalid_type and the fixed
message "The JSON value could not be converted to {expected}.". The serializer's path becomes the error's FullPath
(items[1].age) and its line and position become the line and position parameters. The serializer's own message
is never surfaced, because it names CLR types and can quote the input. DeserializeAndValidateAsync awaits the
schema's ValidateAsync, so asynchronous refinements run.
The validating converter throws ZodNewtonsoftJsonValidationException (namespace ZodSharp.Json, a
JsonSerializationException) carrying the schema's full Errors, located under the converter's JSON path; its
message still starts with "Validation failed: ". DeserializeAndValidate unwraps it into the result.
UTC DateTimeOffset contract
The default PreserveOffset schema is exact and needs no converter. When the generated JSON Schema uses
DateTimeOffsetSerialization.Utc, add the matching converter so the serializer writes UTC and rejects a value
without an explicit Z/zero offset (or with a non-zero offset) on read:
using Newtonsoft.Json;
using ZodSharp.Json.Serialization.NewtonsoftJson;
var settings = new JsonSerializerSettings().UseZodSharpUtcDateTimeOffset();
The helper sets DateParseHandling = DateParseHandling.None, which is required so the converter can inspect
the original offset, and adds the UTC converter. UseZodSharpPreserveOffsetDateTimeOffset() is the
per-type/property opt-out equivalent.
PreserveOffsetDateTimeOffsetConverter is available to opt a type or property back out of a project-wide UTC
converter. See JSON Schema Export.
Import from JSON Schema
using ZodSharp;
using ZodSharp.JsonSchema.NewtonsoftJson;
var schema = Z.FromJsonSchema(jsonSchemaString);
var result = schema.Validate(data);
This lets you share schemas defined in TypeScript/Zod with your .NET backend. (Export via Z.ToJsonSchema lives in the core package.)
Trimming and Native AOT
Newtonsoft.Json is not trim- or Native AOT-compatible: it discovers members and constructors by reflection and
generates code at runtime. Every API in this package that reaches Newtonsoft serialization (DeserializeAndValidate,
DeserializeAndValidateAsync, ValidateAndSerialize, ValidateAndSerializeAsync, CreateValidatingConverter,
Z.FromJsonSchema and FromJsonSchemaParser.Parse) is annotated [RequiresUnreferencedCode] and
[RequiresDynamicCode], so a trimmed or AOT application gets an IL2026/IL3050 warning at the call site instead of a
runtime failure. Use Purview.ZodSharp.SystemTextJson
with a source-generated JsonSerializerContext in those applications. See
Trimming and Native AOT.
Documentation
License
MIT — see the package metadata in Purview.ZodSharp.NewtonsoftJson on NuGet.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. net11.0 is compatible. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.15)
- Microsoft.Extensions.Options (>= 10.0.15)
- Newtonsoft.Json (>= 13.0.4)
- Purview.ZodSharp (>= 3.0.0)
-
net11.0
- Newtonsoft.Json (>= 13.0.4)
- Purview.ZodSharp (>= 3.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 |
|---|---|---|
| 3.0.0 | 84 | 11/16/2026 |
| 3.0.0-prerelease.6 | 55 | 11/12/2026 |
| 3.0.0-prerelease.5 | 61 | 11/11/2026 |
| 3.0.0-prerelease.3 | 60 | 11/8/2026 |
| 3.0.0-prerelease.2 | 66 | 11/2/2026 |
| 2.4.2 | 221 | 10/26/2026 |
| 2.4.1 | 102 | 10/23/2026 |
| 2.4.0 | 282 | 10/22/2026 |
| 2.3.0 | 101 | 10/22/2026 |
| 2.2.0 | 117 | 10/18/2026 |
| 2.1.0 | 105 | 10/16/2026 |
| 2.0.2 | 125 | 10/8/2026 |
| 2.0.1.1 | 109 | 10/7/2026 |
| 2.0.1 | 106 | 10/7/2026 |
| 2.0.0 | 99 | 9/29/2026 |
| 2.0.0-prerelease.28 | 71 | 9/28/2026 |
| 2.0.0-prerelease.27 | 81 | 9/27/2026 |
| 2.0.0-prerelease.26 | 81 | 9/27/2026 |
| 2.0.0-prerelease.25 | 70 | 9/27/2026 |
| 2.0.0-prerelease.24 | 84 | 9/25/2026 |