mirror_json 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

✨ mirror_json #

star this repo fork this repo Build Pub

A Dart library for parsing JSON objects using dart:mirrors.

Important: This library does not work in Flutter.

🔨 Installation #

Add mirror_json to your dependencies.

dependencies:
    ...
    mirror_json:

Now get the packages.

pub get

🔮 Getting started #

mirror_json uses a concept of Parsers. There are a bunch of parsers for simple types (int, double, String) already in the package, but in order to parse a class, you have to make a ClassParser instance.

import 'package:mirror_json/mirror_json.dart';

@JsonParseable()
class MyClass {
    ...
}

void main() {
    GlobalJsonParserInstance.initialize();
    var parser = ClassParser<MyClass>();

    // From json
    var myObject = Json.fromJson({...});

    // To json
    var json = Json.toJson(myObject);
}

Woah, what just happened?

First, we initialized mirror_json's main class, GlobalJsonParserInstance. This automatically adds parsers for simple types like int, double, List, etc. Then, we created a parser for MyClass. Now you can use the Json class to transform your JSON objects to MyClass instances or MyClass objects to JSON.

💡 Example #

See example under example folder.

📚 Docs #

See docs here

📭 Contact me #

E-Mail: kk.erzhan@gmail.com

Changelog #

0.0.3 #

  • WIP

0.0.2 #

  • First release

example/main.dart

import 'package:mirror_json/mirror_json.dart';

// Use this annotation to show that this class can be parsed.
@JsonParseable()
class Human {
  Name name;
  int age;

  Human({this.name, this.age});
}

@JsonParseable()
class Name {
  String first;
  String last;

  Name({this.first, this.last});
}

void main() {
  // Initialize the GlobalJsonParserInstance - this loads the default parsers for bool, int, etc.
  GlobalJsonParserInstance.initialize();

  // You can disable loading default parsers by initializing like so:
  // GlobalJsonParserInstance.initialize(includeBasicParsers: false);

  // Create a Parser for Human class. It will automatically create parsers for its children's classes
  // if they are marked with @JsonParseable().
  // In this case, it will automatically create a parser for Name.
  var humanParser = new ClassParser<Human>();

  // Create a Human instance from corresponding JSON.
  var human = humanParser.fromJson({
    "name": {"first": "Thomas", "last": "Jefferson"},
    "age": 40
  });

  print(human.age);

  // Turn a Human instance into JSON.
  var newHuman = Human(name: Name(first: "Teddy", last: "Roosevelt"), age: 30);
  print(humanParser.toJson(newHuman));

  // Suppose we need to parse Name. To get its parser, you can use GlobalJsonParserInstance.
  var nameParser = GlobalJsonParserInstance.getParser(#Name);

  // Now you can use it to serialize / deserialize JSON.
  print(nameParser.toJson(newHuman.name));
}

Use this package as a library

1. Depend on it

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


dependencies:
  mirror_json: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:mirror_json/mirror_json.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: web, other

Primary library: package:mirror_json/mirror_json.dart with components: mirrors.

Health suggestions

Format lib/mirror_json.dart.

Run dartfmt to format lib/mirror_json.dart.

Format lib/src/global_instance.dart.

Run dartfmt to format lib/src/global_instance.dart.

Format lib/src/parsers/class_parser.dart.

Run dartfmt to format lib/src/parsers/class_parser.dart.

Format lib/src/parsers/complex/list_parser.dart.

Run dartfmt to format lib/src/parsers/complex/list_parser.dart.

Format lib/src/parsers/parser.dart.

Run dartfmt to format lib/src/parsers/parser.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test any