Code Review Videos > C# > How To: Create C# Classes From JSON In JetBrains Rider

How To: Create C# Classes From JSON In JetBrains Rider

Continuing with learning C#, lately I have been diving into interacting with the Chat GPT API from my C# command line applications.

This was my first experience with working with JSON in C#, and in particular I wanted to convert the JSON response from Chat GPT into one (or more?) C# objects.

This does, however, create a problem which is probably very specific to the Chat GPT response. But more on that in a moment.

For now, let’s start off with something really easy.

{ "hello": "world" }Code language: JSON / JSON with Comments (json)

If we have the above JSON as an example payload, using JetBrains Rider to create a C# class to represent that structure is brilliantly easy.

First, create a new C# file for your class where you want to hold the data. I’m not entirely sure my place is best, but I put mine in ./DTO/HelloWorldExample.cs

Copy the entire JSON string to your clipboard, and then:

And you should end up with something like:

namespace ChatGPT.DTO;

public class HelloWorldExample
{
    public class RootObject
    {
        public string hello { get; set; }
    }
}Code language: C# (cs)

It doesn’t seem that useful for simple objects, but most JSON payloads I work with are much larger and so this way I don’t have to spend a bunch of pointless wasted time manually declaring every property and an associated get / set method.

Pretty sweet.

When Should I Use A Record Vs A Class

Being a beginner, and being that there are two options for this paste operation, it led me to looking into the difference between when I should be using a record, versus when I ought to prefer a class.

Here’s what I found:

Use a record in C# when you want to define a value type with a small, immutable state, and a class when you need a reference type with mutable state. In general, consider using a record when:

  • You want to define an object that acts as a simple container of data.
  • You don’t need to add custom behavior to the object.
  • You don’t need to inherit from the object.
  • You want the object to be immutable by default.

Consider using a class when:

  • You need to add custom behavior to the object.
  • You need to inherit from the object.
  • You want the object to be mutable by default.

So in this instance, it looks like I should have preferred a record. And I guess, for most JSON I won’t need to do any set operations, so record would be the first choice.

If I use my original JSON string to create a record using Rider I get:

namespace ChatGPT.DTO;

public record RootObject(
    string hello
);Code language: C# (cs)

That seems really concise, but I wonder how that might scale to larger JSON objects. I think I prefer the slightly more verbose syntax:

namespace ChatGPT.DTO;

public record HelloWorldExampleRecord
{
    public string Hello { get; init; }
}Code language: C# (cs)

Where I learned a trick from a Nick Chapsas about using init to allow setting a property at instantiation, and after that it becomes immutable / read only after construction:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.