json icon indicating copy to clipboard operation
json copied to clipboard

Add To Readme, JSON to Class Converter

Open ewwink opened this issue 1 year ago • 0 comments

This is not issue, I think it good idea if you can add https://app.quicktype.io to the Readme, it parse JSON and generate the Class, it useful for known Json and save times writing the class.

Short how to:

  • open the website
  • paste your json
  • set Output features to Just Types or other
  • set Other options if needed* (detect boolean/int to false), ex: {"IwantAsString" : "12345"} or this parser will throw error because the parser try to convert "12345" (with quote) to long. But you can also fix the parser on Line 163 by prepend the line json = json.Trim('"');
  • Copy generated code to you project

Example how it can be very useful

Json code

{
    "FirstKey": {
        "SecondKey": "val",
        "ThirdKey": [1, 2]
    }
}

Generated class

namespace projectNamespace
{

    public partial class MyJsonClass
    {
        public FirstKey FirstKey { get; set; }
    }

    public partial class FirstKey
    {
        public string SecondKey { get; set; }
        public List<long> ThirdKey { get; set; }
    }
}

Access it

var jsonClass = File.ReadAllText("file.json").FromJson<MyJsonClass>(); 
jsonClass.FirstKey.SecondKey = "test";
jsonClass.FirstKey.ThirdKey.Add(3);

// VS

var jsonClass = File.ReadAllText("file.json").FromJson<Dictionary<string, Dictionary<string, string>>>(); 
jsonClass["FirstKey"]["SecondKey "] = "test"
jsonClass["FirstKey"]["ThirdKey"]... problematic

ewwink avatar Jul 22 '22 15:07 ewwink