json_reflectable 1.0.1 copy "json_reflectable: ^1.0.1" to clipboard
json_reflectable: ^1.0.1 copied to clipboard

This package allows programmers to annotate Dart objects for reflective invocation of the fromJson and toJson methods.

This package allows programmers to annotate Dart objects for reflective invocation of the fromJson and toJson methods.

Features #

This package allows programmers to annotate Dart objects for reflective invocation of the fromJson and toJson methods.

Getting started #

Add into your pubspec.yaml

dependencies:
  json_reflectable: ^1.0.0

Or run command in Terminal

flutter pub add json_reflectable

Usage #

Add into your main.dart

import 'main.reflectable.dart';

void main() {
  initializeReflectable();
  //...
}

Add @jsonReflector to the classes that require reflection

@jsonReflector
@JsonSerializable()
class TextEntity {
  double float;
  String string;

  TextEntity(this.float, this.string);

  factory TextEntity.fromJson(Map<String, dynamic> json) =>
      _$TextEntityEntityFromJson(json);

  Map<String, dynamic> toJson() => _$TextEntityEntityToJson(this);
}

Using instance mirror

final testJson = jsonDecode('''
{
  "float": 1.0,
  "string": "str"
}
''');
TextEntity instance = TextEntity.fromJson(testJson);
print(jsonReflector.toJson(instance));

Using class mirror

TextEntity instance = jsonReflector.formJson<TextEntity>(testJson);
print(instance.toJson());

Using iterable

final iterable = Iterable.generate(3, (index) => jsonMap);
final list = jsonReflector.formIterable<Text01Entity>(iterable).toList();
print("list ${list.runtimeType}");
print(jsonEncode(list));

Additional information #

where to find more information #

how to contribute to the package #

how to file issues #

0
likes
140
pub points
84%
popularity

Publisher

unverified uploader

This package allows programmers to annotate Dart objects for reflective invocation of the fromJson and toJson methods.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (LICENSE)

Dependencies

reflectable

More

Packages that depend on json_reflectable