TopMenu

Deserialize list of json objects as list with JSON.Net

I’ve been a big fan of JSON.Net since past few years. Always helping me with less code and high performance conversion of JSON to C# objects. Thanks to James who made it possible as a fastest framework for manipulating JSON with a great documentation. In this post we will talk about a json format where a list of json objects presents with no root elements. This would be easy if you have root element you can use JObject of json.net. But in case you don’t then this would throw an exception.

Let say we have list of elements in the JSON data:

[
{
"major": 4,
"minor": 0,
"profile": "client",
"servicePack": null,
"url": "http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=5765d7a8-7722-4888-a970-ac39b33fd8ab"
},
{
"major": 4,
"minor": 0,
"profile": "full",
"servicePack": null,
"url": "http://www.microsoft.com/downloads/details.aspx?FamilyID=9cfb2d51-5ff4-4491-b0e5-b386f32c0992&displaylang=en"
},
{
"major": 3,
"minor": 5,
"profile": "client",
"servicePack": 1,
"url": "http://www.microsoft.com/downloads/details.aspx?FamilyId=8CEA6CD1-15BC-4664-B27D-8CEBA808B28B&displaylang=en"
},
{
"major": 3,
"minor": 5,
"profile": "full",
"servicePack": 1,
"url": "http://go.microsoft.com/fwlink/?LinkId=124150"
},
{
"major": 3,
"minor": 0,
"profile": "full",
"servicePack": 1,
"url": "http://www.microsoft.com/downloads/details.aspx?FamilyId=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=en"
},
{
"major": 2,
"minor": 0,
"profile": "full",
"servicePack": 2,
"url": "http://www.microsoft.com/downloads/details.aspx?familyid=5B2C0358-915B-4EB5-9B1D-10E506DA9D0F&displaylang=en"
},
{
"major": 1AAA,
"minor": 1,
"profile": "full",
"servicePack": 1,
"url": "http://www.microsoft.com/downloads/details.aspx?FamilyID=a8f5654f-088e-40b2-bbdb-a83353618b38&DisplayLang=en"
}
]
// Sample taken from http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx

You can use simple Jsonconvert.Deserialize<yourType>(jsonString) to get the typed object from a JSON. But his can throw an error if the items in Json have field with incorrect value. Let’s an int field have some alphabetic character. The exception will thrown for parsing and you’ll have very little details on error.

Now if you need to parse it as list and let’s say you want to find elements who have fields which are not of correct type as it should be then we can filter them out and log them somewhere. And so you will get a complete list of correct items and corrupted data.


We’ll use a JArray class from namespace Newtonsoft.Json.Linq to parse the data as list of array of object and then we’ll convert one by one each item to typed object and add it to list. Here’s the sample generic deserializer that can parse Json and get the valid and invalid elements.


Converter/Deserializer:


public static List<string> InvalidJsonElements;

public static IList<T> DeserializeToList<T>(string jsonString)
{
InvalidJsonElements = null;
var array = JArray.Parse(jsonString);
IList<T> objectsList = new List<T>();

foreach (var item in array)
{
try
{
// CorrectElements
objectsList.Add(item.ToObject<T>());
}
catch (Exception ex)
{
InvalidJsonElements = InvalidJsonElements ?? new List<string>();
InvalidJsonElements.Add(item.ToString());
}
}

return objectsList;
}


Usage:


Product productObj = new Product();
IList<Product> validProdcuts;
IList<string> invalidProductItemInJson;
string jsonString = "[{json specified above}]";

// Call the deserializer
validProdcuts = JsonHelper.DeserializeToList<Product>(jsonString);

// Check for errors
if (JsonHelper.InvalidJsonElements.Count != 0)
{
invalidProductItemInJson = InvalidJsonElements;

// Here we have invalid items of json now we are rectified with
// the problematic data from json
}


So here you can see that you’ll have valid items in list and invalid in errors. For e.g. in the above shown JSON data the last element have invalid value to int field major as ‘1AAA’


Enjoy Jsonifying.

Creating dynamic object with Dynamic feature in CSharp

There were new features introduced in .Net 4.0 and one of the awesome feature was addition of System.Dynamic in framework library. With this dynamic objects C# can provide dynamic language features like Python.

A short brief of Dynamic objects is, Dynamic objects expose members such as properties and methods at run time, instead of in at compile time. This enables you to create objects to work with structures that do not match a static type or format. For example, you can use a dynamic object to reference the HTML Document Object Model (DOM), which can contain any combination of valid HTML markup elements and attributes.

People still gets confused with Dynamic vs Anonymous types. Just to clarify Anonymous types cause the compile time errors. While Dynamic types work upon run time late binding for object properties. Let’s take an example:

var anonymousObject = new { Name=”Anonymous”, Value=”Foo” } ;

if you use anonymouseObject.Index then It’ll throw an compile time exception because it only know the Name and Value as it’s property. But using Dynamic you can use object.UnknownProperty will not cause compile time error.Let suppose you have requirement to create a object with properties which could be in any no. and of any type you don’t know. So how will you do this operation? I’ll introduce you with an exciting type of class called ExpandoObject with in System.Dynamic.


dynamic runTimeObject = new ExpandoObject();
runTimeObject.Name = "OnTheFlyFooObject";
runTimeObject.Value = "FooIsMoo";

This will automatically append these properties to the object. Now if you ask for runTimeObject.OtherUnknownProperty then it’ll not give any error during compilation but this will be check when you run the program. So you can also check if the object have this kind of property before using any runtime property. Behind the scene it’s storing these property as a Dictionary. So you can cast the runTimeObject to IDictionary<string,object> to get the list of late bound properties on the object.


var propertyCollection = (IDictionary<String, Object>)runTimeObject;
if (propertyCollection.ContainsKey("OtherUnknownProperty"))
{
Console.Write(runTimeObject.OtherUnknownProperty);
}

This is how you can be safe from getting any runtime exception. So you have seen how to use dynamic object with properties. Similarly you can also introduce run time methods to the dynamic object.


dynamic runTimeObject = new ExpandoObject();
runTimeObject.Name = "OnTheFlyFooObject";
runTimeObject.Value = "FooIsMoo";
runTimeObject.Print =
(Action)(() => { Console.Write("This a run time method for the dynamic on fly Foo"); });
//Now call the attached method
runTimeObject.Print();


Isn’t is cool? Yes it is. But beware before using it in your ordinary scenario can cost you some CPU time. These are runtime late binding operations which requires its own time to resolve the symbols and so are less optimized than normal early binding cases.


So choose wisely with your scenario. In some cases this is really an awesome feature provided in recent version of CSharp. I would say that CSharp is not just a Object Oriented language but it’s adapting features of Functional, Dynamic language. That’s why it’ll will long last than any language in the programming world.


If you enjoyed reading this then please leave a comment or suggestions.

Code coverage and Unit tests in VS2012

This post is about a question that people often asks when writing Unit tests and What troubles a developer is:

When should I stop writing Unit tests of something(Let say a simple Function)?

Lets talk about something known as Code coverage feature in visual studio(Premium/Ultimate editions). Most of the people don’t even know how it can help identifying unused code. Code coverage actually do the instrumenting of the assembly with special symbols putting over each statement and then analyse if that is being called when the code was getting executed. In visual studio this feature is added with Test manager. So you can run your unit tests via coverage tool and analyse the report how much production code is being covered by your unit tests.

Lets see how to run tests with coverage in simple steps:

1. In visual studio 2012 (Premium/Ultimate) you’ll find the option Test –> Analyze code coverage. Now there’s an option to run a single test coverage or all tests coverage.

image

Note: This option will be enabled only when your solution have UnitTests available. For unit tests written with Nunit you will need an Nuget package for Nunit Test Adapter. If you want then you can also download the Nunit VS Extension by searching into the VS Extension online.

2. Now select Run All Tests option and the magic will start. The coverage tool will be automatically launched and will run all Unit tests.

3. Once all the unit tests are done with running. You’ll see a coverage report in Code coverage Results window which will give you coverage in %.

image

In this report you’ll see in the Covered column the percentage of every function. And if this report says 100% then

Writing Unit tests at the moment.

Though as a standard testing strategy you can stop when coverage is >95%.