Json2CSharp / Json2CSharpCodeGenerator

Microsoft Reciprocal License
292 stars 81 forks source link

Better Options #102

Open TonyValenti opened 1 year ago

TonyValenti commented 1 year ago

Hi All, I would like the JSON that is generated to be different than what is currently output. This ChatGPT prompt demonstrates an example of input and desired output:

Input

Generate C-sharp classes from this json.
Use nullable reference types, get and init accessors, and immutable lists.
Do not use primary constructors.
Replace the word class with record.
Use long instead of int.
Use DateTimeOffset instead of DateTime.
Append "Json" to the end of each name.

{"widget": {
    "debug": "on",
   "date": "2023-02-01",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },

    "images": [{ 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    }, { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    }]
}}  

Output

public record WidgetJson
{
    public string? Debug { get; init; }
    public DateTimeOffset? Date { get; init; }
    public WindowJson? Window { get; init; }
    public ImmutableList<ImageJson>? Images { get; init; }
}

public record WindowJson
{
    public string? Title { get; init; }
    public string? Name { get; init; }
    public long Width { get; init; }
    public long Height { get; init; }
}

public record ImageJson
{
    public string? Src { get; init; }
    public string? Name { get; init; }
    public long HOffset { get; init; }
    public long VOffset { get; init; }
    public string? Alignment { get; init; }
}