Your official information source from the .NET Web Development and Tools group at Microsoft.
‘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:
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
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
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", } ]
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.
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" } ]
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
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" ] }], }] }
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; } }
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
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":””;}
Thank you for your time reading this blog post. Looking forward to hear your feedback on this feature!
Thanks for the information. This is very helpful!!!
For "If property is a keyword in C#/VB.NET then it will be prepended with _ (underscore).", is there a reason why you didn't just use the "@" symbol to escape the reserved word?
Can we have this for all projects, not just web? We have class libraries that do parsing too and this feature is sorely missed now that WE doesn't have it.
Very nice. Will try it now.
I would prefer to see the VB.Net classes with properties rather than public fields.
Public Class Rootobject
Public Property link As String
Public Property virtual As String
End Class
Wow, thats super cool !
Cool stuff! Very useful, very helpful, very Visual Studio!
Awesome!
Thank you all for taking time in reading this blog post and appreciate your valuable comments!
@PeteGoo: There is no specific reason for using _(underscore) instead of "@".
@Oren Novotny: We are working on your ask. No commitments here.
@This is very cool but needs a consistency tweak: I have open up a bug for issue you mentioned and feature team will triage this for RTM release.
@Shri: Do let us know your feedback once you try out this feature.
Oren - not sure if it'll help you or not, but in the past I've used http://json2csharp.com/ and the codeplex project it uses (jsonclassgenerator.codeplex.com) with success when needing to create a set of classes to deserialize a json object graph into memory. JSON.Net (and likely other deserializers) supports attributes for allowing mapping in case you want/need the C# property to be different in name than what's in the json.
There's likely other tools out there to do the same as well, but those were 'good enough' for me at the time. :)
thank you very much for this particular feature, i often find myself writing the js representations of my classes and this feature will save me a lot of time.....
Hi, just wondering I couldn't see this menu option in VS2012?
I can only see Paste XML as Classes. (using ver 2.2)
@Samir, Thanks!
@Dave, this option is visible only in web application projects and websites.
Also make sure you have installed Web Tools 2012.2 RC.
This is great, just one ask - could Lists be generated instead of arrays? Perhaps this could be configurable?
I'm on Visual Studio 2012 English Version and Windows 8 x64 Chinese.
When pasting json string, Visual Studio prompts:
---------------------------
Microsoft Visual Studio
The culture 'Chinese (Simplified, PRC)' is not supported. Pluralization is currently only supported for the English language.
确定
How to change the culture?