4

Consider this JSON:

{
    "firstName": "John",
    "lastName": "Doe",
    "homeAddress": {
        "streetAddress": "123 Main St",
        "city": "Boston",
        "state": "MA",
        "postalCode": "02110"
    },
    "employmentInfo": {
        "employerName": "ABC, Co.",
        "phoneNumber": "617-555-5555"
    }
}

In VS I can use the "Paste JSON as Classes" from "Edit > Paste Special". This is what results:

public class Rootobject
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public Homeaddress homeAddress { get; set; }
    public Employmentinfo employmentInfo { get; set; }
}

public class Homeaddress
{
    public string streetAddress { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postalCode { get; set; }
}

public class Employmentinfo
{
    public string employerName { get; set; }
    public string phoneNumber { get; set; }
}

Notice a couple things: 1.) the class names all have an initial uppercase character and then all lowercase, and 2.) the properties are all lowerCamelCase.

Number 1 doesn't make any sense. Why would it behave that way, is there an official reason? Number 2 makes sense, but I'd like to be able to change the behavior so my compiler doesn't complain about naming my properties wrong.

Is there a way to adjust this behavior?

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • don't think you can configure this in VS tbh, albeit this add-on might help you: https://marketplace.visualstudio.com/items?itemName=AlexanderTatarnikov.JsonFormatter – Ruslan Jan 15 '19 at 14:34
  • I don't see anything wrong with the output. The class HomeAddress is the default casing but the property homeAddress is as it is in the JSON. Why would the compiler complain? – toni Jan 15 '19 at 14:38
  • @toni -- no the class is "Homeaddress". The second word is lowercase. And about your second point: I understand that, but in C# property names are UpperCamelCase (i.e. Pascal Case). VS complains (based on the way I have it set) when it's not that way, so for large JSON, it's a pain to fix all those or pragma them out. – rory.ap Jan 15 '19 at 14:41
  • I guess I see your point. And I don't know how to fix. Although I had no idea this feature existed in VS but it will come in handy for me, more so if someone has an answer for your question. – toni Jan 15 '19 at 14:44

1 Answers1

3

I didn't come accross a way myself to configure this in Visual Studio.

If you don't mind opening a browser window and copying from there, QuickType is a HUGE time saver

https://app.quicktype.io/?l=csharp

Francisco Vilches
  • 3,426
  • 1
  • 15
  • 18