danielwertheim

danielwertheim


notes from a passionate developer

Share


Sections


Tags


Disclaimer

This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

JSON.Net Private Setters NuGet

First. I have nothing to do with the awesome official library "Newtonsoft.Json". This is merely a tiny extension to it. In 2010 I was writing a NoSQL'ish document oriented provider over Microsoft SQL Server (SisoDB). While doing this I found the need for a custom contract resolver to Newtonsoft JSON.Net. One that supported private setters as well. I've written about the solutions to this problem before. Since it still seems to solve issues for people I have put up a GitHub repository and created a source distribution NuGet for it as well.

Update

The source NuGet package has been replaced by a new package that contains more resolvers and are distributed in binary from instead of source.

Continue...

Usage of the source package:

Install-Package JsonNet.PrivateSettersContractResolvers.Source

It installs a single file: PrivateSettersContractResolvers.cs; into your project of choice. After that, you just consume it by creating an instance of either:

which you then assign to the JsonSerializerSettings.ContractResolver.

var settings = new JsonSerializerSettings
{
    ContractResolver = new PrivateSetterContractResolver()
};

var model = JsonConvert.DeserializeObject<Model>(json, settings);

and the model

public class Model
{
    public string Value { get; private set; }
}

That's it.

//Daniel

View Comments