Skip to content

ElZombieIsra/Json-to-Dart-Model

 
 

Repository files navigation

JSON to DART

version install download Ask Me Anything !

Given a JSON string, this library will generate all the necessary Dart classes to parse and generate JSON

This library is designed to generate Flutter friendly model classes following the flutter's doc recommendation.Extention supports for both Serializing JSON manually and Serializing JSON using code generation libraries

  • When an empty array is given, it will create a List<Null>. Such weird behaviour should warn the user that there is no data to extract.
  • Equal structures are not detected yet (Equal classes are going to be created over and over).
  • Properties named with funky names (like "!breaks", "|breaks", etc) or keyword (like "this", "break", "class", etc) will produce syntax errors.
  • Array of arrays are not supported:
[[{ "isThisSupported": false }]]
[{ "thisSupported": [{ "cool": true }] }]

Features

Convert from clipboard to manual model classes

  • Convert json you copied in to dart model classes.

Convert from selection to manual model classes

  • Convert json you selected in to dart model classes.

Convert from clipboard to code generation libraries supported model classes

  • Convert json you copied in to code generarion libraries supported model classes. A terminal session run after convertion to generate rest parts.

Convert from selection to code generation libraries supported model classes

  • Convert json you selected in to code generarion libraries supported model classes. A terminal session run after convertion to generate rest parts.

Add code generation Libaries to pubspec.yaml file

  • Add serializing JSON using code generation libraries to pubspec.yaml.

    structure of the pubspec.yaml

    dependencies:
      # Your other regular dependencies here
      json_annotation: <latest_version>
    
    dev_dependencies:
      # Your other dev_dependencies here
      build_runner: <latest_version>
      json_serializable: <latest_version>

Add advanced equality comparability with Equatable

  • Add the ability to compare your generated models in a better way with Equatable. You can check if 2 classes, that are diferent instances, are equals without a single line of extra code.

Serializing JSON using code generation libraries

If you'd like to use Code Generation Libraries from Flutter, first of all I suggest you to add dependencies to the pubspec.yaml file. It also can be done with this extension. You don't need to worry about it 😉. After that you can convert your JSON to model classes. Then you need to run the flutter pub run build_runner build command to generate the missing code of the models, according to Flutter documentation. Fortunately the extension automatically opens a new terminal session and runs that command for you, yey 😄.

How to use

  1. Select a valid json. Press Ctrl + shift + P and search for Convert from Selection or Convert from Selection to Code Generation supported classes. Provide a Base class name and location to save.

  2. Copy a valid json. Press Ctrl +shift + P and search for Convert from Clipboard or Convert from Clipboard to Code Generation supported classes. Provide a Base class name and location to save.

  3. Press Ctrl + P and search for Add Code Generation Libraries to pubspec.yaml and hit enter.

  4. Using short cuts.

Code equality

You just have to select Yes when the process of parsing your JSON to Code has started and the extension will take care of setting up the advanced code equality check in your Dart models

Equality check menu

Key bindings

Convert from Clipboard (Shift + Ctrl + Alt + V)

Convert from Selection (Shift + Ctrl + Alt + S)

Convert from Clipboard to Code Generation supported classes (Shift + Ctrl + Alt + G)

Convert from Selection to Code Generation supported classes (Shift + Ctrl + Alt + H)

Converter

  • Array type merging (Huge deal)
  • Duplicate type prevention
  • Union types
  • Optional types
  • Array types

Known Issues

Command failed: xclip -selection clipboard -o


Solution:

    sudo apt-get install xclip

Happens when linux is missing clipboard packages

Links

Special thanks

Special thanks to Israel Ibarra for adding equatable support. ❤️

Contact me

Feel free to contact me anytime 😊