json_serializable_generator 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

json_serializable_generator #

Provides Dart Build System builders for handling JSON.

This library reads .model.json files and automatically generates the dart model classes (.model.dart) for them.

Right now the models should be in one directory because otherwise you would need to specify the paths explicitly as described later.

Usage #

simple case #

For the Types int, double, bool, String, List, Map the library relies on the default dart json serialization. For other Types it is assumed that a named constructor fromJson(Map<String, dynamic> json) exists.

Example #

{
  ...,
  "reference": "SomeGeneratedResource",
  "some_other_prop": "String"
}

complex types #

fieldis requiredexplanationdefault
typeYESThe Type Name (class name)-
readMethodNoMethod which is getting called with the json (Function(String)) and should return a class instancenamed constructor or static method fromJson(Map<String, dynamic> json)
writeMethodNoMethod to "write" the class to String, int, double, bool, Map or ListtoJson() method on an instance
importPathNopath from which the resulting model class file can import this class. Should be an "absolute" path.falls back to the generated .model.dart import

Example #

{
  ...,
  "externalResource": {
    "type": "ExternalResource",
    "writeMethod": "prettyWrite",
    "readMethod": "prettyRead",
    "importPath": "package:my_app/external_resources/ExternalResource.dart"
  }
}

Build configuration (build.yaml) #

build to cache (default; not working with Flutter) #

targets:
  $default:
    builders:
      json_serializable_generator:
        generate_for:
          include:
            - lib/**

build to source (necessary for Flutter) #

targets:
  $default:
    builders:
      _generator:
        generate_for:
          include:
            - lib/**
      json_serializable_generator:
        enabled: false

builders:
  _generator:
    import: "package:json_serializable_generator/json_serializable_generator.dart"
    builder_factories:
      - "jsonSerializableGenerator"
    build_extensions: {".model.json": [".model.dart"]}
    auto_apply: root_package
    build_to: source

CHANGELOG #

1.0.1 #

  • make generated member names lowerCamelCase to match dart analyzer needs
  • remove 'dart:mirrors' dependency to be flutter compatible
  • sort imports for generated resources
  • fix double and int type handling

1.0.0 #

  • added flutter configuration to readme
  • add copy method to modify instances

1.0.0-rc2 #

  • added example
  • format code
  • switch to pedantic analysis options
  • update package description

1.0.0-rc1 #

  • initial release
  • generation of model classes specified by a JSON schema

example/main.dart

import 'dart:convert';
import 'dart:io';
import 'package:json_serializable_generator/json_serializable.dart';
import './Example.model.dart';

void main() {
  String json = '''{
    "id": "abcdefg",
    "isRequired": true,
    "count": 42,
    "simpleSchema": {
      "id": 123456,
      "isRequired": false,
      "name": "Test Model Simple"
    },
    "someExternal": "some-id;string;false;123;",
    "anotherExternal": "another-id;string;false;123;",
    "someList": ["Adam", "Benjamin"],
    "someMapping": {
      "Adam": 1,
      "Benjamin": 2
    }
  }''';

  Example deserializedModelInstance = JsonSerializable.fromJson<Example>(json);
  String serializedModelJson = jsonEncode(deserializedModelInstance.toJson());
  print(serializedModelJson);

  exit(0);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  json_serializable_generator: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:json_serializable_generator/json_serializable_generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
52
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Analysis suggestions

Package not compatible with runtime flutter-web on web

Because:

  • package:json_serializable_generator/json_serializable_generator.dart that imports:
  • package:json_serializable_generator/src/serializable_model_builder.dart that imports:
  • package:build/build.dart that imports:
  • package:build/src/generate/run_post_process_builder.dart that imports:
  • package:build/src/builder/post_process_builder.dart that imports:
  • package:build/src/builder/builder.dart that imports:
  • package:build/src/builder/build_step.dart that imports:
  • package:build/src/asset/reader.dart that imports:
  • package:glob/glob.dart that imports:
  • package:glob/src/list_tree.dart that imports:
  • package:glob/src/io.dart that imports:
  • package:glob/src/io_export.dart that imports:
  • dart:io

Package not compatible with runtime js

Because:

  • package:json_serializable_generator/json_serializable_generator.dart that imports:
  • package:json_serializable_generator/src/serializable_model_builder.dart that imports:
  • package:build/build.dart that imports:
  • package:build/src/generate/run_post_process_builder.dart that imports:
  • package:build/src/builder/post_process_builder.dart that imports:
  • package:build/src/builder/builder.dart that imports:
  • package:build/src/builder/build_step.dart that imports:
  • package:build/src/asset/reader.dart that imports:
  • package:glob/glob.dart that imports:
  • package:glob/src/list_tree.dart that imports:
  • package:glob/src/io.dart that imports:
  • package:glob/src/io_export.dart that imports:
  • dart:io

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
build ^1.2.2 1.3.0
meta ^1.1.8 1.2.1
Transitive dependencies
_fe_analyzer_shared 5.0.0
analyzer 0.39.12
args 1.6.0
async 2.4.2
charcode 1.1.3
collection 1.14.13
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
glob 1.2.0
html 0.14.0+3
js 0.6.2
logging 0.11.4
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
path 1.7.0
pub_semver 1.4.4
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0
watcher 0.9.7+15
yaml 2.2.1
Dev dependencies
build_runner ^1.7.4
build_test ^0.10.12+1
pedantic ^1.8.0 1.9.1
test ^1.6.4