json_extension 0.4.1
json_extension: ^0.4.1

Dart native

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

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
likes
80
pub points
4%
popularity

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

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

svarzee@gmail.com

License

BSD (LICENSE)

Dependencies

analyzer, build, class_extensions, json_extension_annotations, source_gen

More

Packages that depend on json_extension