mirror_json 0.1.1

  • README.md
  • Example
  • Installing
  • Versions
  • new60

✨ 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.


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';

class MyClass {

void main() {
    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


import 'package:mirror_json/mirror_json.dart';

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

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

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.

  // 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


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

  // 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.

Use this package as a library

1. Depend on it

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

  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';
Version Uploaded Documentation Archive
0.1.1 Mar 31, 2019 Go to the documentation of mirror_json 0.1.1 Download mirror_json 0.1.1 archive
0.1.0 Mar 29, 2019 Go to the documentation of mirror_json 0.1.0 Download mirror_json 0.1.0 archive
0.0.3 Mar 29, 2019 Go to the documentation of mirror_json 0.0.3 Download mirror_json 0.0.3 archive
0.0.2 Mar 29, 2019 Go to the documentation of mirror_json 0.0.2 Download mirror_json 0.0.2 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 Apr 12, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14


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.


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