json.net

How to serialize only inherited properties of a class using a JsonConverter

允我心安 提交于 2020-01-09 08:20:51
问题 I'm trying to serialize only the inherited properties of a class using json.net. I'm aware of the [JsonIgnore] attribute, but I only want to do ignore them on certain occasion, so I used a custom JsonConverter instead. Here's my class: public class EverythingButBaseJsonConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { // Find properties of inherited class var classType = value.GetType(); var classProps = classType

How to serialize only inherited properties of a class using a JsonConverter

为君一笑 提交于 2020-01-09 08:20:13
问题 I'm trying to serialize only the inherited properties of a class using json.net. I'm aware of the [JsonIgnore] attribute, but I only want to do ignore them on certain occasion, so I used a custom JsonConverter instead. Here's my class: public class EverythingButBaseJsonConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { // Find properties of inherited class var classType = value.GetType(); var classProps = classType

JSON.Net: deserializing polymorphic types without specifying the assembly

做~自己de王妃 提交于 2020-01-09 07:47:40
问题 I see that using JSON.Net, I can decode polymorphic objects if a $type attribute specifies the specific type of the JSON object. In all the examples I've seen, $type includes the namespace. Is it possible to make this work including just a simple type name without the assembly? I'd be happy to specify a default assembly to the JsonSerializer if that's possible. I am able to deserialize the JSON using: public class SingleAssemblyJsonTypeBinder : SerializationBinder { private readonly Assembly

JSON.Net: deserializing polymorphic types without specifying the assembly

流过昼夜 提交于 2020-01-09 07:47:22
问题 I see that using JSON.Net, I can decode polymorphic objects if a $type attribute specifies the specific type of the JSON object. In all the examples I've seen, $type includes the namespace. Is it possible to make this work including just a simple type name without the assembly? I'd be happy to specify a default assembly to the JsonSerializer if that's possible. I am able to deserialize the JSON using: public class SingleAssemblyJsonTypeBinder : SerializationBinder { private readonly Assembly

How to deserialize a JSON array into an object using Json.Net?

≯℡__Kan透↙ 提交于 2020-01-09 03:52:10
问题 I've a valid JSON object that has a JSON array in it. The JSON array doesn't have curly braces and contains a comma separated list of mixed type in it. It looks like this: { "ID": 17, "Days": 979, "Start_Date": "10/13/2012", "End_Date": "11/12/2012", "State": "", "Page": 1, "Test": "Valid", "ROWS": [ [441210, "A", "12/31/2009", "OK", "Done", "KELLEY and Co'", "12/31/2009", "06/29/2010", "TEXAS", "Lawyers", 6, "", "<img src=\"/includes/images/Icon_done.gif\" border=\"0\" alt=\"Done\" title=\

How to deserialize a unix timestamp (μs) to a DateTime from JSON?

空扰寡人 提交于 2020-01-08 21:19:51
问题 JSON { "title":"Mozilla Firefox", "id":24, "parent":2, "dateAdded":1356753810000000, "lastModified":1356753810000000, "type":"text/x-moz-place-container", "children":[] } C# class Bookmark { public string title; public string id; [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))] public DateTime dateAdded; [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))] public DateTime lastModified; public string type; public string root; public long parent; public

JsonSerializerSettings and Asp.Net Core

我的梦境 提交于 2020-01-08 16:55:13
问题 Trying to set JsonOutputFormatter options: var jsonFormatter = (JsonOutputFormatter) options.OutputFormatters.FirstOrDefault(f => f is JsonOutputFormatter); if (jsonFormatter != null) { jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); } or mvcBuilder.AddJsonOptions(jsonOptions => { jsonOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); But as soon as I add this, I get: MissingMethodException: Method

Json.NET Disable the deserialization on DateTime

China☆狼群 提交于 2020-01-08 16:22:25
问题 Here is the code: string s = "2012-08-08T01:54:45.3042880+00:00"; JObject j1 = JObject.FromObject(new { time=s }); Object o = j1["time"]; We can check that o is string and equals "2012-08-08T01:54:45.3042880+00:00" Now we transfer j1.ToString() to another program, which is { "time": "2012-08-08T01:54:45.3042880+00:00" } then at the other program, try to parse it back to JObject, which is JObject j2 = JObject.Parse(j1.ToString()); Object o2 = j2["time"]; Now, if we check o2, o2's type is Date,

Deserialize json array stream one item at a time

房东的猫 提交于 2020-01-08 12:29:31
问题 I serialize an array of large objects to a json http response stream. Now I want to deserialize these objects from the stream one at a time. Are there any c# libraries that will let me do this? I've looked at json.net but it seems I'd have to deserialize the complete array of objects at once. [{large json object},{large json object}.....] Clarification: I want to read one json object from the stream at a time and deserialize it. 回答1: In order to read the JSON incrementally, you'll need to use

Deserialize json array stream one item at a time

我们两清 提交于 2020-01-08 12:28:01
问题 I serialize an array of large objects to a json http response stream. Now I want to deserialize these objects from the stream one at a time. Are there any c# libraries that will let me do this? I've looked at json.net but it seems I'd have to deserialize the complete array of objects at once. [{large json object},{large json object}.....] Clarification: I want to read one json object from the stream at a time and deserialize it. 回答1: In order to read the JSON incrementally, you'll need to use