loading...
Cover image for Quickly Scaffold REST API Model Classes in Visual Studio

Quickly Scaffold REST API Model Classes in Visual Studio

marcelmichau profile image Marcel Michau ・4 min read

Photo by Pakata Goh on Unsplash


Picture the following scenario:

PM:
We need to integrate with FooBar's API.

You:
Okay cool, what kind of API is it? REST? GraphQL? gRPC? SOAP? *shudders*

PM:
It's a JSON REST API.

You:
Oh wonderful, do they have some Swagger docs?

PM:
Not that I know of.

You:
Hmm, Postman collection?

PM:
Nope.

You:
Any documentation?

PM:
No, we've been given some endpoints & you'll need to check what they send back.

You:
Okay great.

PM:
So how long will it take to integrate with their API?

You:
Well, there are a lot of unknowns: we don't know what Auth they have on the API, how complex their response models are, or whether the data is even going to be useful or not.

PM:
Okay so rough estimate? More than a week? Less than a week?

You:
I will need to do some investigation first, can you give me until tomorrow?

PM:
Hmm, yeah, is there any way we can bring in the timeline? We need to get this done as soon as possible.

You:
Are you serious face

Okay, so maybe this tip will not solve all potential issues which may arise in the software development industry, but it may help you save some time when you need to work with a third party API.

Visual Studio Paste Special to the Rescue

For those of you who already know about this feature, stop reading now, you are already as productive as you can be. :)

But for those of you who don't, this is quite a lesser known feature in Visual Studio not many people are familiar with. I feel it can really help out when you need to create your C# POCO (Plain Old CLR Object) classes for serilializing/deserializing your JSON API payloads - without having to type them all out by hand. Let the computers do the boring, repetitive stuff. Plus, this tip does not require the use of any extensions or third-party utilities.

For example, let's assume we get the following response from an API (taken from the Working with JSON page from MDN):

{
  "squadName": "Super hero squad",
  "homeTown": "Metro City",
  "formed": 2016,
  "secretBase": "Super tower",
  "active": true,
  "members": [
    {
      "name": "Molecule Man",
      "age": 29,
      "secretIdentity": "Dan Jukes",
      "powers": [
        "Radiation resistance",
        "Turning tiny",
        "Radiation blast"
      ]
    },
    {
      "name": "Madame Uppercut",
      "age": 39,
      "secretIdentity": "Jane Wilson",
      "powers": [
        "Million tonne punch",
        "Damage resistance",
        "Superhuman reflexes"
      ]
    },
    {
      "name": "Eternal Flame",
      "age": 1000000,
      "secretIdentity": "Unknown",
      "powers": [
        "Immortality",
        "Heat Immunity",
        "Inferno",
        "Teleportation",
        "Interdimensional travel"
      ]
    }
  ]
}

Now, in order to be able to use this data in our C# application, we'd need to create a class with properties that correspond to the properties in the JSON response object.

For a simple response like this, it might not take too long. But for an object with 50 properties, different data types & multiple levels of nesting it can get quite tedious.

To quickly scaffold this JSON payload's POCO in Visual Studio, firstly create a new class & remove the class declaration so that only the namespace declaration remains:

Alt Text

Then, copy the entire JSON response object to your clipboard, then back in Visual Studio, place your cursor at the location you want to paste, but instead of using a normal paste, navigate to Edit > Paste Special > Paste JSON as Classes.

Alt Text

This will then generate the following classes:
Alt Text

The scaffolding tool will then parse the JSON and create properties for each of the objects & try to guess the correct data type for each one. In this case, you can see that it set some properties to string, int & bool. Sometimes it may trip up on things like dates where the format might be a bit weird.

By default, Visual Studio will give the root class a name of Rootobject & name the properties the same as the keys in the JSON object, but you are free to change the class & property names to match your naming convention. Just be aware that you may need to configure your JSON serializer appropriately, depending on your case convention.

For the Visual Studio Code users among us, there is an awesome extension which provides much of the same (and more) functionality than the built-in Visual Studio one. Check it out here: Paste JSON as Code.

That's pretty much it! It's quite a simple little thing, but I've seen quite a few people that didn't know about this handy little feature. I hope it helps you out & shaves off a bit of your precious time.

Happy coding!

Posted on Jun 1 by:

Discussion

markdown guide