Compare JSON Arrays

How do you compare two similar JSON array set ? I bet that is a scenario you might have across atleast once in your developer life. Let us say you have following two sets of JSONs. Json Set 1 [ { "SourceLocation":"England", "DestinationLocation":"Spain", "DeliveryDate":"9/12" }, { "SourceLocation":"England", "DestinationLocation":"Germany", "DeliveryDate":"9/12" } ] Json Set 2 [ … Continue reading Compare JSON Arrays

Json To Xml : Wrapping the Array Elements

In this post, we will look into a scenario when you need to convert a Json to XML, but at the same time need to add a wrapping Property for each array. For example, consider the following Json { reportId : 23232, submittedBy : 'Anu Viswan', projects:[ { id:1001, name:'Project 1', }, { id:1002, name:'Project … Continue reading Json To Xml : Wrapping the Array Elements

Member Serialization

Serialization at times throws curious situations that makes us look beyond the normal usages. Here is another situation that took sometime to find a solution for. Let's examine the following code. What would be the output if one was to serialize an instance of the Test class ? Let's examine the output Not surprising right … Continue reading Member Serialization

Deserialize Json to Generic Type

One of the recent question in stackoverflow found interesting was about a Json, which needs to be deserialized to a Generic Class. What makes the question interesting was the Generic Property would have a different Json Property name depending on the type T. Consider the following Json This needs to be Deseriliazed to the following … Continue reading Deserialize Json to Generic Type

String or Array Converter : Json

Imagine you have a method which returns a Json String of following format. In order to deserialize the JSON, you could define a class as the following. This work flawlessly. But imagine a situation when your method could return either a single Language as seen the example above, but it could additionally return a json … Continue reading String or Array Converter : Json

Conditional Serialization using NewtonSoft Json

One of the least explored feature of Newtonsoft Json is the ability serialize properties conditionally. Consider the hypothetical situation wherein you want to serialize a property in a class only if a condition is satisfied. For example, If the requirement is that you need to include serialize the Department Property only if the User Is … Continue reading Conditional Serialization using NewtonSoft Json

Serializing/Deserializing Dictionaries with Tuple as Key

Sometimes you run into things that might look trivial but it just do not work as expected. One such example is when you attempt to serialize/Deserialize a Dictionary with Tuples as the key. For example The above code would trow an JsonSerializationException when deserializing. But the good part is, the exception tells you exactly what … Continue reading Serializing/Deserializing Dictionaries with Tuple as Key

Json Recipies – Part 2

Continuing with our earlier post on Json Recipies, let's explore couple of quick reference recipes using the famous NewtonSoft.Json. Recipe 04: Deserialize to Anonymous Type One of the least explored feature in NewtonSoft is the ability to deserialize a Json to anonymous type. For example, This would produce an output as Isn't that quite useful. … Continue reading Json Recipies – Part 2

Json Custom Filtering : Remove Properties of a Type T

One of the recent questions that came up in SO which fascinated me was how do one filter a Json string by removing properties from Json, which is of a particular Type. For example, consider the following Json. What if you want to remove all Json Properties that has a value of Integer Type. What … Continue reading Json Custom Filtering : Remove Properties of a Type T