json_reflectable 1.0.1 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));