PrivateSetterContractResolver 2.1.0
dotnet add package PrivateSetterContractResolver --version 2.1.0
NuGet\Install-Package PrivateSetterContractResolver -Version 2.1.0
<PackageReference Include="PrivateSetterContractResolver" Version="2.1.0" />
paket add PrivateSetterContractResolver --version 2.1.0
#r "nuget: PrivateSetterContractResolver, 2.1.0"
// Install PrivateSetterContractResolver as a Cake Addin #addin nuget:?package=PrivateSetterContractResolver&version=2.1.0 // Install PrivateSetterContractResolver as a Cake Tool #tool nuget:?package=PrivateSetterContractResolver&version=2.1.0
PrivateSetterContractResolver
This small library provides a JSON.Net contract resolver to (de-)serialize properties with private setters or getter-only auto properties on a model.
Install-Package PrivateSetterContractResolver
You can activate the contract resolver using the JsonSettings when (de-)serializing json:
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
JsonSerializerSettings settings = new JsonSerializerSettings() {
ContractResolver = new PrivateSetterContractResolver()
};
// Use constructor to instantiate the model object
ApiResult model = new ApiResult("Error message");
// Serialize using public getters of the properties
string serialized = JsonConvert.SerializeObject(model, settings);
// { "success": false, "errorMessage": "Error message" }
// Deserialize by using no constructor and directly setting the backing field of the getter-only auto properties
model = JsonConvert.DeserializeObject<Model>(serialized, settings);
// model has same state right after instantiation
This contract resolver will not invoke any constructor when deserializing. The combination of setting getter-only auto properties and not using any initialization allows for a very resticted model class for creating valid model instances at runtime without affecting the deserialization process of the receiver. An example would be:
public class ApiResult {
/// <summary>
/// Creates a successful result with no error message.
/// </summary>
public ApiResult() {
// This default constructor will NOT be called on deserialization
Success = true;
}
/// <summary>
/// Creates a failed result with <paramref name="errorMessage"/> as message.
/// </summary>
/// <param name="errorMessage">The error message</param>
public ApiResult(string errorMessage) {
Success = false;
ErrorMessage = errorMessage;
}
public bool Success { get; }
public string ErrorMessage { get; }
}
This model implements a default constructor in which the model is set in a successful state. This parameterless constructor would be called by default and the Success-Property wouldn't be changed to it's real value anymore since it's a getter-only auto property. With the PrivateSetterContractResolver however no constructor will be called at all and the getter-only property will be set to the correct value.
Using uninitialized objects can be dangerous, so use this contract resolver only in places where initialization is generally not needed (like in serializing api models).
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. |
.NET Core | netcoreapp2.0 is compatible. 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 | net35 is compatible. net40 was computed. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. 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. |
-
.NETCoreApp 2.0
- Newtonsoft.Json (>= 10.0.3)
-
.NETFramework 3.5
- Newtonsoft.Json (>= 10.0.3)
-
.NETStandard 2.0
- Newtonsoft.Json (>= 10.0.3)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on PrivateSetterContractResolver:
Package | Downloads |
---|---|
EngineBay.Blueprints
Package Description |
|
EngineBay.ActorEngine
Package Description |
|
MikeStonogaProjects.BusinessDomain
Package Description |
|
TheBajanGuy.Development.EntityInfrastructure
This is a project that expose Entity Framework using a Base Repositiory. |
|
Golder.Erp.Common
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.