c# - Json.net override method in DefaultContractResolver to deserialize private setters -


I have a class with properties in which there are personal sets and I would like them for those qualities DeSerialized using Json.Net I know that I can use the [JsonProperty] attribute to do this, I will call it DefaultContractResolver I want to implement it. Here are some example codes I'm using, but it's a coherent job.

  Fixed zero main (string [] args) {var a = new a (); A.s = "Monday"; One set (); Console.WriteLine (JsonConvert.SerializeObject (a)); Var strrr = JsonConvert.SerializeObject (a); On strobj = JsonConvert.DeserializeObject & lt; A & gt; (Strrr, new JsonSerializerSettings {ContractResolver = new PrivateSetterContractResolver ()}); Console.Read (); }  

This is the class that I want to serial

  public class A {Private int test}; Public examination test {return examination; } Private set {test = value; }} Public string {get; Set; } Public Zero Set () {Test = 33; }}  

The implementation of this is DefaultContractResolver

Public class PrivateSetterContractResolver: DefaultContractResolver {from the protected override list & lt; MemberInfo & gt; GetSerializable member (type objectType) {// whait what i have here ??? // This dosent work return new list & amp; Member; Interface & gt; (ObjectType.GetProperties (). ToList ()); }}

Found the solution I was trying to override incorrectly. You need to override the CreateProperty method

  protected override JsonProperty CreateProperty (MemberInfo member, MemberSerialization memberSerialization) {var prop = base.CreateProperty (member, memberSerialization) ; If (! Prop.Writable) {var property = member property; If (property! = Null) {var hasPrivateSetter = property.GetSetMethod (true)! = Null; Prop.Writable = hasPrivateSetter; }} Return support; }  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -