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.


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';
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 Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/src/global_instance.dart. (-1 points)

Analysis of lib/src/global_instance.dart reported 2 hints:

line 106 col 13: Unnecessary new keyword.

line 125 col 13: Unnecessary new keyword.

Format lib/mirror_json.dart.

Run dartfmt to format lib/mirror_json.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