Visual Studio – Paste Special

I began using Visual Studio back in version 2005. We’re now in version 2017 and in all these years I cannot remember opening the edit option; not a single time. That changed yesterday when a colleague told me about the Paste Special option. It’s a very neat capability that I’d never ever thought existed, I was shocked!

Have you ever been in a situation where you need to create classes and properties to match a JSON file? Let’s suppose you’re expected to receive a JSON like this:

[csharp light=”true”]
{
"users": [
{
"id": 0,
"name": "Adam Carter",
"work": "Unilogic",
"email": "adam.carter@unilogic.com",
"dob": "1978",
"address": "83 Warner Street",
"city": "Boston",
"optedin": true
},
{
"id": 1,
"name": "Leanne Brier",
"work": "Connic",
"email": "leanne.brier@connic.org",
"dob": "13/05/1987",
"address": "9 Coleman Avenue",
"city": "Toronto",
"optedin": false
}
],
"images": [
"img0.png",
"img1.png",
"img2.png"
],
"coordinates": {
"x": 35.12,
"y": -21.49
},
"price": "$59,395"
}
[/csharp]

Now you have to create matching classes in your language of choice to map those values to properties/fields. Up until now I’ve done this by hand, but it just happens that if you add the JSON contents to your clipboard and access the Visual Studio menu: Edit -> Paste Special -> Paste JSON as Classes (Notice that there’s also an option for XML) and this is what you get:

[csharp light=”true”]
public class Rootobject
{
public User[] users { get; set; }
public string[] images { get; set; }
public Coordinates coordinates { get; set; }
public string price { get; set; }
}

public class Coordinates
{
public float x { get; set; }
public float y { get; set; }
}

public class User
{
public int id { get; set; }
public string name { get; set; }
public string work { get; set; }
public string email { get; set; }
public string dob { get; set; }
public string address { get; set; }
public string city { get; set; }
public bool optedin { get; set; }
}
[/csharp]

It generated the necessary matching classes and properties, saving the effort of doing it by hand.

At this point, you may be shaking your head as you may not want your classes to follow the same name convention as in the JSON, and that is fine, I usually don’t use JSON notation in my classes either, but it saves the trouble of writing down those properties anyway. I can then add the necessary attributes if I want to.

Now I’m left wondering, what other cool features am I missing?

2 thoughts on “Visual Studio – Paste Special

Leave a Reply

Your email address will not be published. Required fields are marked *