JsonPact.System 0.0.3

dotnet add package JsonPact.System --version 0.0.3
                    
NuGet\Install-Package JsonPact.System -Version 0.0.3
                    
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="JsonPact.System" Version="0.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JsonPact.System" Version="0.0.3" />
                    
Directory.Packages.props
<PackageReference Include="JsonPact.System" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JsonPact.System --version 0.0.3
                    
#r "nuget: JsonPact.System, 0.0.3"
                    
#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.
#:package JsonPact.System@0.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JsonPact.System&version=0.0.3
                    
Install as a Cake Addin
#tool nuget:?package=JsonPact.System&version=0.0.3
                    
Install as a Cake Tool

.net

JsonPact.Newtonsoft NuGet JsonPact.System NuGet license

tests status release status coverage coverage

🤝 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 RequiredValue with rest of the values NullableValue & OptionalValue defined as default-able properties.

So if the RequiredValue is 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

SetupJsonPact settings along with your own Newtonsoft settings.

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 JsonPact attribute, 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.Json will not work as shown above only the top level JsonPact attribute is respected but it will work perfectly with Newtonsoft.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
0.0.3 598 8/16/2022
0.0.2 543 8/16/2022
0.0.1 581 7/27/2022