json.net

Serializing an interface/abstract object using NewtonSoft.JSON

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 09:58:35
问题 One way of deserializing interface and abstract properties is a class is by setting TypeNameHandling to Auto during serialization and deserialization. However, when I try the same when serializing and deserializing an interface object directly, it does not work - interface ISample { string Key { get; set; } } class A : ISample { public string Key { get; set; } public A(string key) { this.Key = key; } } class B : ISample { public string Key { get; set; } public B(string key) { this.Key = key;

Why are some members missing when trying to print an object by serializing to JSON?

一笑奈何 提交于 2019-12-17 09:58:19
问题 How to print any arbitrary variable in C# so as to print all members? I found three answers with the same technique: https://stackoverflow.com/a/26181763/2125837 which suggests serializing with Json.NET among other answers, and https://tech.io/playgrounds/2098/how-to-dump-objects-in-c/using-json-and-yaml-serializers, https://www.codeproject.com/Articles/1194980/How-to-Dump-Object-for-Debugging-Purposes-in-Cshar However, when I tried it out, with the following code, using System; using System

Serializing an interface/abstract object using NewtonSoft.JSON

纵然是瞬间 提交于 2019-12-17 09:58:06
问题 One way of deserializing interface and abstract properties is a class is by setting TypeNameHandling to Auto during serialization and deserialization. However, when I try the same when serializing and deserializing an interface object directly, it does not work - interface ISample { string Key { get; set; } } class A : ISample { public string Key { get; set; } public A(string key) { this.Key = key; } } class B : ISample { public string Key { get; set; } public B(string key) { this.Key = key;

json.net - how to add property $type ONLY on root object

此生再无相见时 提交于 2019-12-17 09:56:34
问题 I want to modify my json.NET serializer to add the $type property only to the objects which implements a given interface but not to any property or nested objects. With TypeNameHandling.Auto (default) { "PropertyA": 123, "PropertyB": "foo", "PropertyC": [1, 2, 3, 4] } With TypeNameHandling.All { "$type": "JsonNetTypeNameHandling.TestEvent, jsonNetTypeNameHandling", "PropertyA": 123, "PropertyB": "foo", "PropertyC": { "$type": "System.Collections.Generic.List`1[[System.Int32, mscorlib]],

How do I parse a JSON object in C# when I don't know the key in advance?

删除回忆录丶 提交于 2019-12-17 09:51:21
问题 I have some JSON data that looks like this: { "910719": { "id": 910719, "type": "asdf", "ref_id": 7568 }, "910721": { "id": 910721, "type": "asdf", "ref_id": 7568 }, "910723": { "id": 910723, "type": "asdf", "ref_id": 7568 } } How can I parse this using JSON.net? I can first do this: JObject jFoo = JObject.Parse(data); I need to be able to iterate over each object in this list. I would like to be able to do something like this: foreach (string ref_id in (string)jFoo["ref_id"]) {...} or

Serializing multiple DateTime properties in the same class using different formats for each one

徘徊边缘 提交于 2019-12-17 09:50:11
问题 I have a class with two DateTime properties. I need to serialize each of the properties with a different format. How can I do it? I tried: JsonConvert.SerializeObject(obj, Formatting.None, new IsoDateTimeConverter {DateTimeFormat = "MM.dd.yyyy"}); This solution doesn't work for me because it applies date format to all the properties. Is there any way to serialize each DateTime property with different format? Maybe there is some attribute? 回答1: NewtonSoft.Json has a structure that's a bit

WebApi Json.NET custom date handling

扶醉桌前 提交于 2019-12-17 09:47:53
问题 I have globally explicitly configured my MVC4 app to use the JSON.NET serializer . I know i have the choice of using the ISO standard dates or the old Microsoft date format when serializing dates. But how can i output my own custom dateTime format string ,like :"dd/MM/yyyy hh:mm". I could do this in MVC3 when plugging in Json.NET as the default serializer but cant seem to do it in MVC4 . So far in the application_start i have done : var settings = GlobalConfiguration.Configuration.Formatters

How to deserialize a property with a dash (“-”) in it's name with NewtonSoft JsonConvert?

混江龙づ霸主 提交于 2019-12-17 09:43:21
问题 We have a JSON object with one of the object having a dash in its name. Ex below. { "veg": [ { "id": "3", "name": "Vegetables", "count": "25" }, { "id": "4", "name": "Dal", "count": "2" }, { "id": "5", "name": "Rice", "count": "8" }, { "id": "7", "name": "Breads", "count": "6" }, { "id": "9", "name": "Meals", "count": "3" }, { "id": "46", "name": "Extras", "count": "10" } ], "non-veg": [ { "id": "25", "name": "Starters", "count": "9" }, { "id": "30", "name": "Gravies", "count": "13" }, { "id"

Custom Json Serialization of class

坚强是说给别人听的谎言 提交于 2019-12-17 09:42:48
问题 I have code structured like below. public class Stats { public string URL { get; set; } public string Status { get; set; } public string Title { get; set; } public string Description { get; set; } public int Length { get; set; } } and public class UrlStats { public string URL { get; set; } public int TotalPagesFound { get; set; } public List<Stats> TotalPages { get; set; } public int TotalTitleTags { get; set; } public List<Stats> TotalTitles { get; set; } public int NoDuplicateTitleTags {

Ignore parsing errors during JSON.NET data parsing

*爱你&永不变心* 提交于 2019-12-17 09:14:09
问题 I have an object with predefined data structure: public class A { public string Id {get;set;} public bool? Enabled {get;set;} public int? Age {get;set;} } and JSON is supposed to be { "Id": "123", "Enabled": true, "Age": 23 } I want to handle JSON error in positive way, and whenever server returns unexpected values for defined data-types I want it to be ignore and default value is set (null). Right now when JSON is partially invalid I'm getting JSON reader exception: { "Id": "123", "Enabled":