JsonPact.System
0.0.3
dotnet add package JsonPact.System --version 0.0.3
NuGet\Install-Package JsonPact.System -Version 0.0.3
<PackageReference Include="JsonPact.System" Version="0.0.3" />
<PackageVersion Include="JsonPact.System" Version="0.0.3" />
<PackageReference Include="JsonPact.System" />
paket add JsonPact.System --version 0.0.3
#r "nuget: JsonPact.System, 0.0.3"
#:package JsonPact.System@0.0.3
#addin nuget:?package=JsonPact.System&version=0.0.3
#tool nuget:?package=JsonPact.System&version=0.0.3
🤝 json-pact
json wrapper library that enforces casing & validates required properties by checking if the property uses the nullable ? property marker as specified by nullable value types
and also makes use of optional values without the need for extra attributes.
💪 Extending
[JsonPact(JsonPactCase.Snake)]
public record JsonDTO(
string RequiredValue,
string? NullableValue,
string OptionalValue = "default"
);
// vs
[JsonObject(NamingStrategyType = typeof(SnakeCaseNamingStrategy))]
public record JsonDTO(
[property: JsonProperty(Required = Required.Always)]
string RequiredValue,
string? NullableValue,
[property: DefaultValue("fallback")]
string OptionalValue
);
🥽 Prerequisites
You will need to enable the nullable project setting within your projects
.csproj file.
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
Install
Newtonsoft
dotnet add package JsonPact.Newtonsoft
System.Text.Json
dotnet add package JsonPact.System
🏎️💨 Getting Started
[JsonPact]
public record JsonDTO(
string RequiredValue,
string? NullableValue,
string OptionalValue = "default"
);
Here we define the schema with a required value
RequiredValuewith rest of the valuesNullableValue&OptionalValuedefined as default-able properties.
So if the
RequiredValueis found missing an error will be thrown and default-able values will be automatically defaulted.
var pact = JsonPacts.Default(JsonPactCase.Snake).IntoJsonPact();
var json = pact.Serialize(new JsonDTO("required", null));
// = { "required_value": "required", "optional_value": "default" }
var obj = pact.Deserialize<JsonDTO>(json);
// = JsonDTO("required", null, "default")
var err = pact.Deserialize<JsonDTO>("{ }");
// = JsonPactDecodeException
⚙️ Settings
Setup
JsonPactsettings along with your ownNewtonsoftsettings.
var settings = new NewtonSoft.JsonSerializerSettings {
// ...
}.AddJsonPact(JsonPactCase.Camel);
var pact = settings.IntoJsonPact();
🎮 Overriding casing
If you are dealing with mixed casing you can provide a casing type with the
JsonPactattribute, this will override the default casing and will allow you to have nested objects with different casing.
[JsonPact(JsonPactCase.Snake)]
public record Snake(Camel RequiredValue);
[JsonPact(JsonPactCase.Camel)]
public record Camel(Kebab RequiredValue);
[JsonPact(JsonPactCase.Kebab)]
public record Kebab(string RequiredValue);
var pact = JsonPacts.Default(JsonPactCase.Snake).IntoJsonPact();
var snake = new Snake(new Camel(new Kebab("hello")));
var json = pact.Serialize(snake);
// = { "required_value": { "requiredValue": { "required-value": "hello" } } }
⚠️ Currently nested objects with different casing while using
System.Text.Jsonwill not work as shown above only the top level JsonPact attribute is respected but it will work perfectly with Newtonsoft.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. 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. 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. |
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.