https://github.com/brunozell/privatesettercontractresolver
Provides a JSON.Net contract resolver to (de-)serialize models with private setters or getter-only auto properties.
https://github.com/brunozell/privatesettercontractresolver
contract-resolver json newtonsoft-json
Last synced: about 1 month ago
JSON representation
Provides a JSON.Net contract resolver to (de-)serialize models with private setters or getter-only auto properties.
- Host: GitHub
- URL: https://github.com/brunozell/privatesettercontractresolver
- Owner: BrunoZell
- License: mit
- Created: 2017-10-02T09:37:55.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-05T13:40:05.000Z (over 8 years ago)
- Last Synced: 2025-08-20T13:54:45.430Z (10 months ago)
- Topics: contract-resolver, json, newtonsoft-json
- Language: C#
- Size: 26.4 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```c#
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(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:
```c#
public class ApiResult {
///
/// Creates a successful result with no error message.
///
public ApiResult() {
// This default constructor will NOT be called on deserialization
Success = true;
}
///
/// Creates a failed result with as message.
///
/// The error message
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).