json_parser 0.1.6

  • README.md
  • Installing
  • Versions
  • 75

JSON Parser for Flutter

Build Status Stars Coverage Status License

Flutter does not provide support for auto mapping JSON to object instances. This project makes 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:

  json_parser: ^0.1.6
  build_runner: ^0.10.1+1

Every Flutter/Dart application has a main() entry point. In that method you need to add call the following method:

void main() {

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:

          - lib/main.dart
          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';

class DataClass {
  String name = "";
  int age = 0;
  String car = "";

Note the usage of @reflectable. All your classes which will be used for JSON parsing need to use this annotation. All the properties in a reflectable class needs to be initialized with a default value to let the parser know the instance types during runtime.

Lists have a different approach. You need to declare a get and set body for them. Since Dart can't set a value of type List<dynamic> to a List<YourClass>, you need to cast it manually in your class. Make sure all your lists look like the following example:

List<Mark> _marks = [];
List<Mark> get marks => _marks;
set marks(List list) {
  _marks = list.cast<Mark>();

If you have binary data, think of a byte[] in C# or Java, you need to use Uint8List. You can initialize it like the following:

Uint8List data = new Uint8List(0);

When the parser detects a Uint8List instance, it will expect data in your json to be encoded as Base64. It will be parsed automatically.

If you have set all the properties correctly, you are able to start the parsing. You can parse a JSON string using the following method:

JsonParser parser = new JsonParser();
DataClass instance = parser.parseJson<DataClass>(json);

Note that you MUST specify the object type when calling the parse method. If you are expecting a list as return type, you will need to declare it as the following example:

JsonParser parser = new JsonParser();
List instance = parser.parseJson<DataClass>(json);

After the whole parsing process, instance will contain all the values from your json input.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  json_parser: ^0.1.6

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:json_parser/json_parser.dart';
Version Uploaded Documentation Archive
0.1.6 Sep 6, 2018 Go to the documentation of json_parser 0.1.6 Download json_parser 0.1.6 archive
0.1.5 Sep 5, 2018 Go to the documentation of json_parser 0.1.5 Download json_parser 0.1.5 archive
0.1.4 Aug 29, 2018 Go to the documentation of json_parser 0.1.4 Download json_parser 0.1.4 archive
0.1.3 Aug 25, 2018 Go to the documentation of json_parser 0.1.3 Download json_parser 0.1.3 archive
0.1.2 Aug 24, 2018 Go to the documentation of json_parser 0.1.2 Download json_parser 0.1.2 archive
0.1.1 Aug 24, 2018 Go to the documentation of json_parser 0.1.1 Download json_parser 0.1.1 archive
0.1.0 Aug 24, 2018 Go to the documentation of json_parser 0.1.0 Download json_parser 0.1.0 archive
0.0.3 Aug 23, 2018 Go to the documentation of json_parser 0.0.3 Download json_parser 0.0.3 archive
0.0.2 Aug 23, 2018 Go to the documentation of json_parser 0.0.2 Download json_parser 0.0.2 archive
0.0.1 Aug 23, 2018 Go to the documentation of json_parser 0.0.1 Download json_parser 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Nov 14, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.6
  • Flutter: 0.11.3


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance issues and suggestions

Maintain CHANGELOG.md. (-20 points)

Changelog entries help clients to follow the progress in your code.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use json_parser.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
build_runner ^0.10.1+1 0.10.3 1.1.2
flutter 0.0.0
reflectable ^2.0.5 2.0.7 2.0.8
Transitive dependencies
analyzer 0.32.6 0.33.3+1
args 1.5.1
async 2.0.8
build 0.12.8 1.0.1
build_config 0.3.1+4
build_resolvers 0.2.2+6
build_runner_core 1.0.0 1.1.0
built_collection 4.0.0
built_value 6.1.6
charcode 1.1.2
code_builder 3.1.3
collection 1.14.11
convert 2.0.2
crypto 2.0.6
csslib 0.14.6
dart_style 1.1.3 1.2.0
fixnum 0.10.9
front_end 0.1.4+2 0.1.6+4
glob 1.1.7
graphs 0.1.3+1
html 0.13.3+3
http 0.12.0
http_multi_server 2.0.5
http_parser 3.1.3
io 0.3.3
js 0.6.1+1
json_annotation 1.2.0 2.0.0
kernel 0.3.4+2 0.3.6+4
logging 0.11.3+2
matcher 0.12.4
meta 1.1.6
mime 0.9.6+2
package_config 1.0.5
package_resolver 1.0.6
path 1.6.2
pedantic 1.3.0
plugin 0.2.0+3
pool 1.3.6
pub_semver 1.4.2
pubspec_parse 0.1.2+3
quiver 2.0.1
shelf 0.7.3+3
shelf_web_socket 0.2.2+4
sky_engine 0.0.99
source_span 1.4.1
stack_trace 1.9.3
stream_channel 1.6.8
stream_transform 0.0.14+1
string_scanner 1.0.4
timing 0.1.1+1
typed_data 1.1.6
utf 0.9.0+5
vector_math 2.0.8
watcher 0.9.7+10
web_socket_channel 1.0.9
yaml 2.1.15
Dev dependencies