json.net

Json.Net serialization of IEnumerable with TypeNameHandling=auto

半城伤御伤魂 提交于 2021-02-08 13:09:44
问题 According to Json.Net documentation all IEnumerable types should be serialized as json array. So I expect the following class: public class MyClass { public IEnumerable<string> Values { get; set; } } to be serialized as: { "Values": [] } The problem is that when I use TypeNameHandling=Auto I get: { "Values": { "$type": "System.String[], mscorlib", "$values": [] } } I need TypeNameHandling=Auto for other properties but I expect IEnumerable to use the default serialization. Other types ( IList

Serialize List<LinkedListNode<object>> using Json.net

一个人想着一个人 提交于 2021-02-08 12:16:16
问题 example of code with string also throws exception: LinkedList<string> l = new LinkedList<string>(); l.AddLast("Kuku"); l.AddLast("Riku"); l.AddLast("Ok"); List<LinkedListNode<string>> lst = new List<LinkedListNode<string>>(); lst.Add(l.First); lst.Add(l.First.Next); lst.Add(l.Last); string json = JsonConvert.SerializeObject(lst, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize }); File.WriteAllText(@"C:\Student Routine\Data.txt", json);

Serialize List<LinkedListNode<object>> using Json.net

萝らか妹 提交于 2021-02-08 12:14:26
问题 example of code with string also throws exception: LinkedList<string> l = new LinkedList<string>(); l.AddLast("Kuku"); l.AddLast("Riku"); l.AddLast("Ok"); List<LinkedListNode<string>> lst = new List<LinkedListNode<string>>(); lst.Add(l.First); lst.Add(l.First.Next); lst.Add(l.Last); string json = JsonConvert.SerializeObject(lst, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize }); File.WriteAllText(@"C:\Student Routine\Data.txt", json);

How to parse JSON with a recursive structure representing a query

帅比萌擦擦* 提交于 2021-02-08 09:36:26
问题 I have a specified JSON document, like this: { "user":"human1", "subsystems":[1,2,3], "query":{"AND":[ {"eq":["key1","val1"]}, {"eq":["key2","val2"]}, {"OR":[ {"eq":["subkey1","subval1"]}, {"eq":["subkey2","subval2"]}]} ] } } Expected transformation of the query field: (key1 eq val1 and key2 eq val2 and (subkey1 eq subval1 OR subkey2 eq subval2)) I am using Newtonsoft.Json ( JsonConvert.DeserializeObject ), and I don't understand how to transform this field. 回答1: Well, like most things, there

c# Json.net immutable class

故事扮演 提交于 2021-02-08 07:39:45
问题 I saw other similar questions/answers but none show both serialization/deserialization Example: public class DeepNested { [JsonProperty] int X { get; } [JsonProperty] int Y { get; } public DeepNested(int x, int y) { X = x; Y = y; } [JsonConstructor] public DeepNested(DeepNested dn) { X = dn.X; Y = dn.Y; } } public class Nested { [JsonProperty] DeepNested DN { get; } [JsonProperty] int Z { get; } [JsonProperty] int K { get; } [JsonConstructor] public Nested(DeepNested dn, int z, int k) { DN =

c# Json.net immutable class

烂漫一生 提交于 2021-02-08 07:38:37
问题 I saw other similar questions/answers but none show both serialization/deserialization Example: public class DeepNested { [JsonProperty] int X { get; } [JsonProperty] int Y { get; } public DeepNested(int x, int y) { X = x; Y = y; } [JsonConstructor] public DeepNested(DeepNested dn) { X = dn.X; Y = dn.Y; } } public class Nested { [JsonProperty] DeepNested DN { get; } [JsonProperty] int Z { get; } [JsonProperty] int K { get; } [JsonConstructor] public Nested(DeepNested dn, int z, int k) { DN =

Using Newtonsoft.Json, how can I use a SerializationBinder and CustomResolver together to deserialize abstract/interface types?

淺唱寂寞╮ 提交于 2021-02-08 05:43:25
问题 Building on this very helpful answer from Brian Rogers I wrote the this, but it throws an exception, see more below. I re-wrote the code, because the original version unfortunately doesn't quite meet my requirements: I need to include an ISerializationBinder implementation, because I have to map types differently for use with an IOC ("Inversion Of Control") container: For serialization I need to bind a component type (i.e. class) to a type alias. For de serialization I need to bind the type

Validate Json data while calling DeserializeObject<Object>( … )

試著忘記壹切 提交于 2021-02-08 03:31:11
问题 I want to validate Json code after I deserialize it. For example if I have ... using Newtonsoft.Json; ... public Car { public int Year{ get; set; } public String Make{ get; set; } } ... JsonConvert.DeserializeObject<Car>(json) I want to validate that the year is < 2017 && >=1900 , (for example). Or maybe make sure that the Make is a non empty string, (or it is an acceptable value). I know I could add a Validate() type function after I deserialize, but I am curious if there was a way of doing

Handling reference loops in JSON.net

妖精的绣舞 提交于 2021-02-07 14:39:27
问题 I wish to serialize a collection ( List<Item> ) of items to JSON. These items have a collection of Connection which gives information about the connection from one Item to a second Item . And since the connection object has a reference to the items it makes it an infinite loop. My question is is there a way for me to skip serialization of the connection collection when serializing the object the second time. I've tried things like inheriting from JsonConverter and writing a custom WriteJson()

Handling reference loops in JSON.net

☆樱花仙子☆ 提交于 2021-02-07 14:38:19
问题 I wish to serialize a collection ( List<Item> ) of items to JSON. These items have a collection of Connection which gives information about the connection from one Item to a second Item . And since the connection object has a reference to the items it makes it an infinite loop. My question is is there a way for me to skip serialization of the connection collection when serializing the object the second time. I've tried things like inheriting from JsonConverter and writing a custom WriteJson()