DevToys icon indicating copy to clipboard operation
DevToys copied to clipboard

JSON to Code

Open veler opened this issue 4 years ago • 10 comments

Is your feature request related to a problem? Please describe. As a web developer, it common to have to convert a large JSON to C# classes in order to consume certain web API.

Describe the solution you'd like Add a JSON to C#, TypeScript and Java tool.

veler avatar Sep 30 '21 16:09 veler

Could I work on this?

itsWindows11 avatar Nov 05 '21 17:11 itsWindows11

With pleasure!

Some guidance:

  1. Feel free to make your own solution for the conversion, or take example on existing implementation on GitHub. Just be mindful of the license. I'd like to keep DevToys under MIT license, so I'd avoid anything that has a license requiring us to publish DevToys under the same than their.
  2. Keep things simple, for now. I'd prefer shipping a converter with a minimal set of languages and options for it than delivering a lot options right at the beginning. For example, for C#, I think the following options to start should be enough:
    • Should array be treated as [] or List<T>
    • Should properties support nullable values or not
  3. Follow the coding style guidance here: https://github.com/veler/DevToys/blob/main/CONTRIBUTING.md#coding
  4. Let's start with JSON to C# and TypeScript first. Let's see in a future iteration for other languages like Java, Python or C++
  5. Let's support Smart Detection feature. Here is an example: https://github.com/veler/DevToys/blob/d6cf9414110d9fecf4c2c44af8df74074ea1def4/src/dev/impl/DevToys/ViewModels/Tools/JsonFormatter/JsonFormatterToolProvider.cs#L29-L32
  6. When designing the UI
    • Consider using the monaco editor like in Json Formatter or Json <> Yaml tool.
    • If you need more than 3 options in the UI, consider an expandable one, like in Regex Tester tool
    • Make sure to test the UI in compact mode, maximized window and small (width and height) window side.

Don't hesitate to ask questions here or ping me on Discord :) Thank you so much for wanting to participate :D

veler avatar Nov 05 '21 17:11 veler

Visual Studio already has an option for this under the Edit menu.

jaigak avatar Dec 04 '21 07:12 jaigak

@Jaiganeshkumaran , thank you :) What you're saying is true, but it lacks a few options to define, for example, whether we want PascalCase, camelCase, and more options that we can find on websites online. We're trying to bring the power of these online service into an offline app, not to limit ourselves to what Visual Studio already does.

veler avatar Dec 04 '21 07:12 veler

Hi guys! Is there any chance to see Class to JSON converter? I'd love to use it for API testing

nartalex avatar Jan 19 '22 20:01 nartalex

By Class to Json, to you mean generating mocked JSON value from a class definition?

veler avatar Jan 20 '22 05:01 veler

Exactly. I see this as a input window for my C# class and (maybe) a JSON constructor where I can pick values I need in my JSON. I mentioned that because I often test my APIs by Postman and I always struggle with converting my API's input model into JSON especially a large ones. That initial conversion can be done by Swagger, but Swagger generates model as full as it can be, so I have to manually clean it and that can be very time consuming

nartalex avatar Jan 21 '22 13:01 nartalex

I kinda implemented it with NJsonSchema on a sample console app but the output is messy, still have to port it to the app.

itsWindows11 avatar Aug 17 '22 17:08 itsWindows11

[December 2022 update]

This could be a potential paid feature for DevToys 2.0.

UX could be as simple as:

Input: JSON

Output: TypeScript, Python, Java, C#, PHP, C++, Ruby, Go, Dart, Rust, Swift

Settings: They would be specific to each output language selected. For example, for C#, we could allow to chose whether a [JSONProperty] attribute should be added.

veler avatar Dec 31 '22 21:12 veler

support added with Devtoys.JsonToCSharp extension for DevToys 2.0.1

cloudfy avatar Jun 17 '24 11:06 cloudfy