Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add To Readme, JSON to Class Converter #52

Open
ewwink opened this issue Jul 22, 2022 · 0 comments
Open

Add To Readme, JSON to Class Converter #52

ewwink opened this issue Jul 22, 2022 · 0 comments

Comments

@ewwink
Copy link

ewwink commented Jul 22, 2022

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant