json_extension 0.4.1

Description #

Uses class_extensions and json_serializable to generate methods for object serialization from/to json map/string.

See also withers_extension, to_string_extension

Tutorial #

  1. We need a class to generate json serialization methods for.

    ${PROJECT_ROOT}/lib/model.dart #
     import 'package:meta/meta.dart';
    
     @immutable
     class SomeValueClass {
       final String strVal;
       final int intVal;
     }
    
  2. For json serialization to work you need to add some boiler plate:

    • import 'dart:convert';
    • part 'model.g.dart';
    • annotation @Json()
    • mixin _$SomeValueClass
    • all args constructor SomeValueClass(this.strVal, this.intVal);
    • factories SomeValueClass.fromJson(Map<String, dynamic> json) and SomeValueClass.fromJsonString(String json)
    ${PROJECT_ROOT}/lib/model.dart #
     import 'dart:convert';
    
     import 'package:json_extension_annotations/annotations.dart';
     import 'package:meta/meta.dart';
    
     part 'model.g.dart';
    
     @Json()
     @immutable
     class SomeValueClass with _$SomeValueClass {
       final String strVal;
       final int intVal;
    
       SomeValueClass(this.strVal, this.intVal);
    
       factory SomeValueClass.fromJson(Map<String, dynamic> json) =>
           _$SomeValueClass.fromJson(json);
    
       factory SomeValueClass.fromJsonString(String json) =>
           _$SomeValueClass.fromJsonString(json);
     }
    
  3. Configure code generation. More info at build.

    ${PROJECT_ROOT}/build.yaml #
     targets:
       $default:
         builders:
           class_extensions:
             generate_for:
             - lib/*.dart
    
  4. Add dependencies.

    ${PROJECT_ROOT}/pubspec.yaml #
     name: example
    
     dependencies:
       json_extension_annotations: ^0.2.0
    
     dev_dependencies:
       build_runner: ^1.0.0
       json_extension: ^0.2.0
       json_serializable: ^3.2.3
    
  5. Run code generation: pub run build_runner build. File ${PROJECT_ROOT}/lib/model.g.dart should be created.

  6. If everything goes well you should be able to use json.

    ${PROJECT_ROOT}/bin/main.dart #
     import 'package:example/model.dart';
    
     void main() {
       var some = SomeValueClass("some", 0);
    
       var jsonMap = some.toJson();
       var someFromMap = SomeValueClass.fromJson(jsonMap);
    
       var jsonString = some.toJsonString();
       var someFromString = SomeValueClass.fromJsonString(jsonString);
     }
    
  7. You can also see the example.

0.4.1 #

  • Update example

0.4.0 #

  • Upgrade dependencies

0.3.0 #

  • Upgrade dependencies

0.2.0 #

  • Upgrade class_extensions dependency

0.1.0 #

  • Initialize package

example/README.md

Description #

The tutorial example.

Use this package as a library

1. Depend on it

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


dependencies:
  json_extension: ^0.4.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support 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_extension/json_extension.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
15
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
57
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.5

Health issues and suggestions

Document public APIs. (-1 points)

2 out of 2 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
analyzer ^0.39.0 0.39.4
build >=0.12.6 <2.0.0 1.2.2
class_extensions ^0.10.0 0.10.2
json_extension_annotations ^0.3.0 0.3.0
source_gen ^0.9.0 0.9.4+7
Transitive dependencies
_fe_analyzer_shared 1.0.3
args 1.5.2
async 2.4.0
charcode 1.1.3
class_extensions_annotations 0.2.0
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
json_annotation 3.0.1
logging 0.11.4
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0 2.0.0
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.3
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
build_runner ^1.0.0