npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-to-dart

v0.1.4

Published

Generate dart class from json data

Downloads

4

Readme

English | 中文简体

json-to-dart

Generate dart class from json data, used for serialization and deserialization. This is usefull for flutter to generate models of api. Moreover, the original JSON data can be reversed when the generated file is generated, or it can be rebuildable according to the modification.

Install

$ npm install -g json-to-dart

Usage

View usage help.

$ json2dart --help

Generate

Generate from stdin

Execute the following command, you will be prompted to enter JSON

$ json2dart --name Accounts --yes

You can copy json from anywhere, and then paste it into the console.

Generate from json file

$ json2dart --name Accounts --yes --from accounts.json

Generate from http

$ json2dart --name Accounts --yes --from http://url/of/accounts.json

Regenerate

If you want to regenerate with new data, you can directly use the command in the file comment (recommended, use the same parameter to ensure the consistency of the structure)

// GENERATED BY `json2dart --name Accounts --no-concat --no-suffix --from accounts.json -y`

Then modify the parameters that need to be changed(such as --from), and add -f/--force to save the newly generated code.

$ json2dart --name Accounts --no-concat --no-suffix --from new_accounts.json -y -f

By default, although the new data is used, the new data and the data existing in the file comments are combined and then generated. This is to avoid losing the data added directly to the file. If it is clear that the old data is not needed, you can use the --no-merge option to ignore the old data.

Modify and Rebuild

If your data structure has been adjusted, such as adding new fields, or you want to rename the generated class, you can choose to regenerate it with the complete JSON data. But at this time, you may have discarded the original JSON data. It is a bit troublesome to obtain the new JSON data. What should I do? Don’t worry, all valid configuration and JSON data have been included in the documentation comments {@tool json2dart } and {@end-tool}.

/// {@tool json2dart --name Accounts}
/// * items: `<AccountItem>[]`
/// * summary: `AccountSummary()`
/// * months: `<String, AccountsMonthItem>{"2020-08":{}}`
/// * count: `1`
/// {@end-tool}
...
/// {@tool json2dart --name AccountsMonthItem}
/// * blance: `38.9`
/// {@end-tool}

If you want to change AccountsMonthItem to AccMonthItem, and add a new field description, Just modify the names in all documentation comments, the code in the file does not need to be modified, and then rebuild.

/// {@tool json2dart --name Accounts}
/// * items: `<AccountItem>[]`
/// * summary: `AccountSummary()`
/// * months: `<String, AccMonthItem>{"2020-08":{}}`
/// * count: `1`
/// {@end-tool}
...
/// {@tool json2dart --name AccMonthItem}
/// * blance: `38.9`
/// * description: `"Description"`
/// {@end-tool}

Rebuild with --dry-run option to view and compare the output first

$ json2dart --name Accounts --rebuild lib/models/accounts.dart --dry-run

And then use -f or --force to save

$ json2dart --name Accounts --rebuild lib/models/accounts.dart -f

Configration

The json file .json2dart in project root directory provider global config.

{
  "picker": {
    "path.of.data": {
      "path.of.required.key1": true,
      "path.of.required.key2": true,
      "path.of.not.exist.key": false
    }
  },
  "array": ["list", "items"],
  "variables": ["json", "j", "d", "_json"],
  "dir": "lib/models", 
  "formatter": "dartfmt",
  "maxComment": 0,
  "fromOption": {// Can be an object or a file name
    "headers": {
      "Authorization": "Bearer token"
    }
  }
}
  • picker Pick up real object json from input json. This is usefull when json is wrapped.
  • array General array field, which will be ignored when naming the class by default.
  • variables List of available variables, used to solve the problem of naming conflicts in generated methods
  • dir The default directory for storing code
  • formatter code formatting tool command
  • maxComment The max commnet length of object, used to redundantly store JSON data in comments
  • fromOption The options used to fetch JSON data from file/url. Usually an option for http request, such as headers, etc.

For example, example/.json2dart

{
  "picker": {
    "api.data": {
      "api.success": true,
      "api.error": true,
      "api.error.code": false
    }
  },
  "array": ["list", "items"]
}

If the input json with name Test.

{
  "api": {
    "success": true,
    "data": {
      "count": 1,
      "items": [{
        "id": 1,
        "name": "name"
      }]
    },
    "error": null
  }
}

The real object json is

{
  "count": 1,
  "items": [{
    "id": 1,
    "name": "name"
  }]
}

And there two class: Test and TestItem, example/lib/models/test.dart