I have a C # class that looks like
public class Node { public int Id { get; set; } public Node ParentNode { get; set; } }
In the browser, I sent the JSON object as follows
{"Id":1, "ParentNode":1}
If the value 1 assigned to the ParentNode property is the database identifier. So, in order to properly bind to my model, I need to write a custom JSON converter
public class NodeJsonConverter : JsonConverter { public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return null; } JObject jObject = JObject.Load(reader); Node node = new Node(); serializer.Populate(jObject.CreateReader(), node); return node; } }
Since I get "The current JsonReader is not an object: Integer. Path ParentNode", how can I adapt the ReadJson method to bind the ParentNode property or anything else that needs an individual conversion?
UPDATE
I saw JsonPropertyAttribute , in the API documentation of which
Instructs JsonSerializer to always serialize the member with the specified name
However, how can I instruct JsonSerializer programmatically - in my case, in the ReadJson method - to use this JsonPropertyAttribute?
http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm
source share