‘Paste JSON As Classes’ is a cool feature in ASP.NET and Web Tools 2012.2 RC. This feature will help you generate strongly typed classes in C# or VB.NET from valid JSON text.
With ASP.NET and Web Tools 2012.2 RC installed, you will see new menu option like below for C# and VB.NET Website and Web Application projects only. This new menu option will be enabled for .cs and .vb file extensions inside these projects:
JSON to C#/VB.NET class conversion
To use this feature, just copy sample JSON text and “Paste JSON As Classes” inside .vb or .cs file. This feature uses Newtonsoft JSON parser to parse JSON text from clipboard. Once Newtonsoft JSON parser validates the clipboard data as valid JSON, then it will be converted into C# or VB.NET class depending on the selected file type.
JSON to Classes Conversion rules
- Outermost class name is always Rootobject.
- All classes and properties are public.
- If property is a keyword in C#/VB.NET then it will be prepended with _ (underscore).
- If property is number then it will be converted to int/float/double/Single type
- JSON string is converted to string in C# and String in VB.NET
- Boolean data types are converted to bool in C# and Boolean in VB.NET
- If JSON text in clipboard is array of multiple JSON objects and for given property if value is null in one object and value is supported type in another then it will be marked as nullable type.
- With single JSON object, nullable data type will not be emitted.
Let’s look at some examples:
{ "link": "http://www.microsoft.com/Surface/en-US", /*Awesome*/ "virtual": "Virtual Keyboard", "partial": " The magnesium panels are finished with partial vapor deposition", "Price": 499.99, "title": "Microsoft Surface", "शीर्षक": "माइक्रोसॉफ्ट सरफेस", "Like": true, "cdDrive":null, }
C# class for above JSON object will look like below.
public class Rootobject { public string link { get; set; } public string _virtual { get; set; } public string partial { get; set; } public float Price { get; set; } public string title { get; set; } public string शीर्षक { get; set; } public bool Like { get; set; } public object cdDrive { get; set; } }
VB.NET class will be like below
Public Class Rootobject Public link As String Public virtual As String Public _partial As String Public Price As Single Public title As String Public शीर्षक As String Public _Like As Boolean Public cdDrive As Object End Class
DateTime Formats
JSON parser recognizes some DateTime formats as valid DateTime data types and some are not.
Below JSON object has some valid DateTime data and some invalid DateTime data types
[ { "DateTimeValid": "2012-05-03T00:06:00.638Z", "NullableDateTime": null, "DateTimeToString": "2009-07-31T00:00Z", "DateTimeToObject": "2012-05-03T00:06:00.638Z", }, { "DateTimeValid": "2012-05-03T00:06:00Z", "NullableDateTime": "2012-05-03T00:06:00.638Z", "DateTimeToString": "2009-07-31T00:00Z", "DateTimeToObject": "2012-05-03T00:06", } ]
C# class for above JSON object will look like below.
public class Rootobject { public Class1[] Property1 { get; set; } } public class Class1 { public DateTime DateTimeValid { get; set; } public DateTime? NullableDateTime { get; set; } public string DateTimeToString { get; set; } public object DateTimeToObject { get; set; } }
First two properties of Class1 are valid DateTime objects, 3rd property is deemed as string as the DateTime format is not valid/recognized. Last property is converted as object as one object in array is valid DateTime and other object in array is invalid DateTime object. So property type cannot be DateTime nor can be string. Common base type for string and DateTime is object.
Single Dimensional Arrays
JSON Arrays are represented with [ ] notation in C# and with () in VB. Example as below.
[ { "Name": "Kamal, Employee", "Manager": { "Name": "Barry, Manager" } }, { "Name": "Barry, Manager" } ]
C# class for above JSON object will look like below.
public class Rootobject { public Class1[] Property1 { get; set; } } public class Class1 { public string Name { get; set; } public Manager Manager { get; set; } } public class Manager { public string Name { get; set; } }
VB. NET class as below
Public Class Rootobject Public Property1() As Class1 End Class Public Class Class1 Public Name As String Public Manager As Manager End Class Public Class Manager Public Name As String End Class
Multi-Dimensional Arrays
You can convert multi-dimension JSON arrays to classes. JSON Example for two dimensional array:
[ [ 1, 2, 3 ] ]
C# class for above JSON object will look like below:
public class Rootobject { public int[][] Property1 { get; set; } }
VB.NET class:
Public Class Rootobject Public Property1()() As Integer End Class
Also you can convert JSON with objects having arrays inside array as shown in the example below
{ "results": [{ "address_Office": [{ "street_number": "One", "city_name": "Bellevue", "state_name": "Washington", "street_name": [ "Microsoft Way" ] }], }] }
C# class for above JSON object will look like below:
public class Rootobject { public Result[] results { get; set; } } public class Result { public Address_Office[] address_Office { get; set; } } public class Address_Office { public string street_number { get; set; } public string city_name { get; set; } public string state_name { get; set; } public string[] street_name { get; set; } }
VB.NET class:
Public Class Rootobject Public results() As Result End Class Public Class Result Public address_Office() As Address_Office End Class Public Class Address_Office Public street_number As String Public city_name As String Public state_name As String Public street_name() As String End Class
Troubleshooting
If JSON Object in clipboard is invalid, error message will be shown with the reason.
Example: Below JSON object contains max value for decimal data type. Newtonsoft JSON Parser does not recognize values above 1.7976931348623157E+308(which is max value for double data type).
{ "DecimalMax": 1.79769313486232E+308, }
This will result in error like below when “Paste JSON As Classes” is called.
Another example of invalid JSON object where “” are not valid character:
{"login":””;}
This will result in error like below when “Paste JSON As Classes” is called.
Thank you for your time reading this blog post. Looking forward to hear your feedback on this feature!
0 comments