json_reflectable

Creator: coderz1093

Last updated:

Add to Cart

Description:

json reflectable

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
copied to clipboard
Or run command in Terminal
flutter pub add json_reflectable
copied to clipboard
Usage #
Add into your main.dart
import 'main.reflectable.dart';

void main() {
initializeReflectable();
//...
}
copied to clipboard
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);
}
copied to clipboard
Using instance mirror
final testJson = jsonDecode('''
{
"float": 1.0,
"string": "str"
}
''');
TextEntity instance = TextEntity.fromJson(testJson);
print(jsonReflector.toJson(instance));
copied to clipboard
Using class mirror
TextEntity instance = jsonReflector.formJson<TextEntity>(testJson);
print(instance.toJson());
copied to clipboard
Using iterable
final iterable = Iterable.generate(3, (index) => jsonMap);
final list = jsonReflector.formIterable<Text01Entity>(iterable).toList();
print("list ${list.runtimeType}");
print(jsonEncode(list));
copied to clipboard
Additional information #
where to find more information #
how to contribute to the package #
how to file issues #

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.