json_parser 0.1.0 json_parser: ^0.1.0 copied to clipboard
A generic JSON parser for Flutter using reflection.
JSON Parser for Flutter #
Flutter does not provide support for auto mapping JSON to object instances. This project is an attempt to make reflection work on Flutter. Using reflection we are able to parse a JSON string and map it's values to an instance of a Dart object.
Getting started #
First of all add the following dependencies to your pubspec.yaml
:
dependencies:
json_parser: 0.1.0
build_runner: 0.8.3
Every Flutter/Dart application has a main()
entry point. In that method
you need to add at least the following:
void main() {
initializeReflectable();
}
You will see that the method initializeReflectable()
is not declared. That is
fine, since you need to generate a code file first.
First create a build.yaml
file in your Flutter application project. Then add the
following content:
targets:
$default:
builders:
reflectable:
generate_for:
- lib/main.dart
options:
formatted: true
lib/main.dart
points to the location of the Dart class containing the main()
method entry of your application.
Then open up your terminal in your project root and type the following:
flutter packages pub run build_runner build
Do this every time you make a change in some of the reflectable classes.
As mentioned before, lib/main.dart
specifies the folder name of the location of
the class containing the main()
entry. Usually in Flutter applications this is
the /lib
folder. If all goes well, you will see a generated .reflectable.dart
file. Import the generated class in your main()
entry class.
In order to make the mapping work, you need to create a new Dart object which has the same property names as your JSON. We are using the following example:
{ "name":"John", "age":30, "car":null }
Then we will use the following Dart class:
import 'package:json_parser/reflectable.dart';
@reflectable
class DataClass {
String name;
int age;
String car;
List<Mark> _marks = [];
List<Mark> get marks => _marks;
set marks(List list) {
_marks = list.cast<Mark>();
}
}
@reflectable
class Mark {
int mark;
}
Note the usage of @reflectable
. All your classes which will be used for JSON
parsing need to use this annotation. Also take a look at how lists are
initialized. It's important to make an instance of the list and to add the cast
method to let the parser work.
Then you are all set and able to start the parsing. You can parse a JSON string using the following method:
DataClass instance = JsonParser.parseJson<DataClass>(json);
Note that you MUST specify a type when calling the parse method.
If all goes well, instance
will automatically contain all your values specified
in the JSON.