Paste JSON As Classes’ in ASP.NET and Web Tools 2012.2 RC


‘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:

clip_image001

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

  1. Outermost class name is always Rootobject.
  2. All classes and properties are public.
  3. If property is a keyword in C#/VB.NET then it will be prepended with _ (underscore).
  4. If property is number then it will be converted to int/float/double/Single type
  5. JSON string is converted to string in C# and String in VB.NET
  6. Boolean data types are converted to bool in C# and Boolean in VB.NET
  7. 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.
  8. With single JSON object, nullable data type will not be emitted.

Let’s look at some examples:

C# class for above JSON object will look like below.

VB.NET class will be like below

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

C# class for above JSON object will look like below.

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.

C# class for above JSON object will look like below.

VB. NET class as below

 

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:

VB.NET class:

Also you can convert JSON with objects having arrays inside array as shown in the example below

C# class for above JSON object will look like below:

VB.NET 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).

This will result in error like below when “Paste JSON As Classes” is called.

clip_image002

Another example of invalid JSON object where “” are not valid character:

This will result in error like below when “Paste JSON As Classes” is called.

clip_image003

Thank you for your time reading this blog post. Looking forward to hear your feedback on this feature!