hiranthaR / Json-to-Dart-Model

Json to Dart Model extension can convert JSON objects into Dart data classes. It supports pure Dart class conversion, Flutter-recommended JSON serialization using annotations, Freezed support, Effective Dart:Style, and many more features. Currently, it has more than 135,000 installs.
https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart
MIT License
93 stars 17 forks source link

Best practice for configuring JSON objects in the `models.jsonc` file #65

Open iamarnas opened 3 years ago

iamarnas commented 3 years ago

Proposal

Right now JSON to Dart model allows an override path for every JSON object by working with models.jsonc file and default global configuration reads from the extension settings. To add more options and make more customizable outputs my recommended method is 👇

Recommended options:

"__options": {
    "codeGenerator": "Default", // ['Default', 'JSON', 'Freezed']
    "immutable": false,
    "toString": "Default", // ['Default', 'Auto', 'Stringify', 'Dart']
    "copyWith": false,
    "equality": "Default", // ['Default', 'Equatable', 'Dart']
    "targetDirectory": "/lib/src/models", // <- override path here
    "runBuilder": false,
    "sortConstructorsFirst": false,
    "includeIfNull": false
},

Current options:

{
    "__className": "user", 
    "__path": "/lib/src/models",
    "name": "Name",
    "id": 1010
}

We'd have:

{
    "__className": "user",
    "__options": {
        "toString": "Dart", 
        "equality": "Dart", 
        "targetDirectory": "/lib/src/models"
    },
    "name": "Name",
    "id": 1010
}

If someone has a better solution please suggest it in the comment with examples that other users would vote for the best one. I would be very grateful for any suggestion.

Prn-Ice commented 3 years ago

This looks good, but if it's at all possible, I'd rather have separate models.jsonc files for each module. I'm currently working on a medium-sized project and my models.jsonc file is already getting pretty long.

iamarnas commented 3 years ago

I'm currently working on a medium-sized project and my models.jsonc file is already getting pretty long.

I understand 😄 I personally comment every class and use hotkeys to fold everything.

This looks good, but if it's at all possible, I'd rather have separate models.jsonc files for each module.

Everything is possible, only time and work are required. I have created models.jsonc file for easier and faster edit JSON objects and automate the work as much as possible. By splitting models file you will be forced to build your own builder options to tell the generator how you want to generate. That needs just finds a method how to do it with less work as possible and simple as possible.

@Prn-Ice Personal question. It would be good to run the generator by saving the file models.jsonc after edit. (build on save)? Or is it not useful maybe?

Prn-Ice commented 3 years ago

@iamarnas that would be very useful, but only if I don't have to delete all the previously generated models first.

iamarnas commented 3 years ago

@iamarnas that would be very useful, but only if I don't have to delete all the previously generated models first.

@Prn-Ice It would not override generated models it would add only the new models. Json to Dart Model generator not override models and not remove generated models.

Prn-Ice commented 3 years ago

Currently, whenever I add a new model to my models.jsonc and run build models, I get a pop-up warning me that one of the old models already exists.

I have to delete all the previous models and re-run just to see the new one.

iamarnas commented 3 years ago

@Prn-Ice It would add only new object from the jsonc file. You get warning only when dublicates found. Looks like be changing path to the every json object not work as aspected.

Prn-Ice commented 3 years ago

@iamarnas I see, it reads the models.jsonc file from top to bottom thats why. I didnt add the new model to the top of the list.