Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Updated, new answer</h2> <p>I've written a source distribution NuGet for this, that installs a single file with two custom contract resolvers:</p> <ul> <li>PrivateSetterContractResolver</li> <li>PrivateSetterCamelCasePropertyNamesContractResolver</li> </ul> <p>Install the NuGet:</p> <pre><code>Install-Package JsonNet.PrivateSettersContractResolvers.Source </code></pre> <p>Then just use any of the resolvers:</p> <pre><code>var settings = new JsonSerializerSettings { ContractResolver = new PrivateSetterContractResolver() }; var model = JsonConvert.DeserializeObject&lt;Model&gt;(json, settings); </code></pre> <p>You can read about it here: <a href="http://danielwertheim.se/json-net-private-setters-nuget/" rel="noreferrer">http://danielwertheim.se/json-net-private-setters-nuget/</a></p> <p>GitHub repo: <a href="https://github.com/danielwertheim/jsonnet-privatesetterscontractresolvers" rel="noreferrer">https://github.com/danielwertheim/jsonnet-privatesetterscontractresolvers</a></p> <h2>Old answer (still valid)</h2> <p>There are two alternatives that can solve the problem.</p> <p><strong>Alt 1: On the deserializers</strong></p> <pre><code>ContractResolver.DefaultMembersSearchFlags = DefaultMembersSearchFlags | BindingFlags.NonPublic; </code></pre> <p>The default serialization option supports all types of class member. Therefore this solution will return all private members types including fields. I'm only interested in also supporting private setters.</p> <p><strong>Alt2: Create a custom ContractResolver:</strong></p> <p>Therefore this is the better options since we just check the properties.</p> <pre><code>public class SisoJsonDefaultContractResolver : DefaultContractResolver { protected override JsonProperty CreateProperty( MemberInfo member, MemberSerialization memberSerialization) { //TODO: Maybe cache var prop = base.CreateProperty(member, memberSerialization); if (!prop.Writable) { var property = member as PropertyInfo; if (property != null) { var hasPrivateSetter = property.GetSetMethod(true) != null; prop.Writable = hasPrivateSetter; } } return prop; } } </code></pre> <p>For more information, read my post: <a href="http://danielwertheim.se/json-net-private-setters/" rel="noreferrer">http://danielwertheim.se/json-net-private-setters/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload