CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2019-04-08
by Risto M

Original Post

Original - Posted on 2014-04-11
by Brian Rogers



            
Present in both answers; Present only in the new answer; Present only in the old answer;

You can make Custom JsonConverter as following:
public class ResponseConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(Response); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) return null;
JArray arr = JArray.Load(reader);
// Populate the "Info" field last so it will not be overwritten var response = new Response(); var list = arr.ToList(); var itemList = new List<Item>(); foreach (var item in list) { var i = new Item(); i.A = (string)item["A"]; i.B = (string)item["B"]; itemList.Add(i); } response.Items = itemList.ToArray(); return response; }
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } }
[JsonConverter(typeof(ResponseConverter))] public class Response { public Item[] Items { get; set; } }
Now you only need to decorate your root class with following attribute: [JsonConverter(typeof(ResponseConverter))] public class Response { public Item[] Items { get; set; } }
And finally you can deserialise json to `Response` with `JsonConvert.DeserializeObject<Response>(content);`
Json.Net prefers to use the default (parameterless) constructor on an object if there is one. If there are multiple constructors and you want Json.Net to use a non-default one, then you can add the `[JsonConstructor]` attribute to the constructor that you want Json.Net to call.
[JsonConstructor] public Result(int? code, string format, Dictionary<string, string> details = null) { ... }
It is important that the constructor parameter names match the corresponding property names of the JSON object (ignoring case) for this to work correctly. You do not necessarily have to have a constructor parameter for every property of the object, however. For those JSON object properties that are not covered by the constructor parameters, Json.Net will try to use the public property accessors (or properties/fields marked with `[JsonProperty]`) to populate the object after constructing it.
If you do not want to add attributes to your class or don't otherwise control the source code for the class you are trying to deserialize, then another alternative is to create a custom [JsonConverter][1] to instantiate and populate your object. For example:
class ResultConverter : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(Result)); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { // Load the JSON for the Result into a JObject JObject jo = JObject.Load(reader); // Read the properties which will be used as constructor parameters int? code = (int?)jo["Code"]; string format = (string)jo["Format"]; // Construct the Result object using the non-default constructor Result result = new Result(code, format); // (If anything else needs to be populated on the result object, do that here) // Return the result return result; }
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } }
Then, add the converter to your serializer settings, and use the settings when you deserialize:
JsonSerializerSettings settings = new JsonSerializerSettings(); settings.Converters.Add(new ResultConverter()); Result result = JsonConvert.DeserializeObject<Result>(jsontext, settings);
[1]:http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverter.htm


        
Present in both answers; Present only in the new answer; Present only in the old answer;