Visually Located

XAML and GIS

Creating C# classes from JSON (or XML) in Visual Studio 2015

I am continually creating classes from JSON. In the past I have used json2csharp.com to accomplish this task. It is a very simply website with one large textbox. Paste your text or a link to text and click the generate button and suddenly you have C# classes!

json2csharp

Visual Studio 2015 removes the need for this website. The Visual Studio team has added a new way to paste JSON or XML text and have classes created. This new feature even works better than json2charp.com did. To use the new feature follow the below steps

  1. Open a file. If you want to paste JSON the file must be a class file (have the .cs extension).
  2. Copy some JSON
  3. Select Edit –> Paste Special –> Paste JSON As Classes
    paste-menu

It will generate as many classes as are needed. I’ll give a few examples to see the benefits of this new feature and how it could be improved.

Simple base types

{
    "firstName" : "John",
    "lastName" : "Doe",
    "age" : 13,
    "dateOfBirth" : "2002-10-05T14:13:25-06:00"
}

And the generated class

public class Rootobject
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public int age { get; set; }
    public DateTime dateOfBirth { get; set; }
}

You’ll notice that the properties are in lower camel case to match the property names of the JSON. Notice that the age property is correctly of type int and that the dateOfBirth property is of type DateTime. json2csharp.com would turn this property into a string. However, the dateOfBirth property in JSON format included an offset from UTC so the property should have been of type DateTimeOffset to ensure that it is properly de-serialized.

Multiple classes

What if you have multiple classes that need to be de-serialized? Each class will be generated.

{
    "id" : 12345,
    "friend" : {
        "name" : "Mickey"
    }
}
public class Rootobject
{
    public int id { get; set; }
    public Friend friend { get; set; }
}
 
public class Friend
{
    public string name { get; set; }
}

Collections

Collections of items are handled as well, but not in a very great way. If you change the friend property to be a collection of friends, we get the following

public class Rootobject
{
    public int id { get; set; }
    public Friend[] friends { get; set; }
}
 
public class Friend
{
    public string name { get; set; }
}

Notice that it made a Friend array rather than using the preferred (in my opinion) IEnumerable<Friend>. This is primarily because some JSON converters need a concrete class to de-serialize to. Serializers like JSON.Net allow you to specify an interface and it will default to using a concrete class. So if you specify IEnumerable<T> JSON.Net will create a new List<T>.

Error handling

If the JSON you are pasting contains errors, Visual Studio will tell you the line and position of the error! Maybe you wrote the JSON incorrectly or copied only part of it. The following example is missing a comma.:

{
    "id" : 12345
    "name" : "Shawn"
}

The following error message is shown:

error

Room for improvement

One of the biggest problems with json2csharp.com is it’s ability to “reuse” a class. This problem is present within the new Visual Studio feature as well. Take the following example of providing paging links in a result.

{
    "items" : [
        {
            "id" : 11,
            "id" : 12
        }
    ],
    "previous":{
        "href": "http://paging.com/1"
    },
    "next":{
        "href": "http://paging.com/3"
    }
}

The resulting class generation is to create a class for “previous” and for “next”.

public class Rootobject
{
    public Item[] items { get; set; }
    public Previous previous { get; set; }
    public Next next { get; set; }
}
 
public class Previous
{
    public string href { get; set; }
}
 
public class Next
{
    public string href { get; set; }
}
 
public class Item
{
    public int id { get; set; }
}

It would be great if it would “reuse” the first class it created rather than creating a new one. Deleting the extra classes can become quote a pain when you have some json that returns say 10 or more links to other information as well. Side note: If anyone knows if I can help contribute to this tool please let me know!

Another area for improvement is to say you want to default to using a particular JSON serializer so that it could capitalize property names. For example, if I could specify that I wanted to use JSON.Net then it would capitalize the classes and add the JsonPropertyAttribute for proper serializing and de-serializing.

blog comments powered by Disqus