json_object 1.0.18+2

JsonObject for DART (http://www.dartlang.org)

Version 1.0.18

Usage: Add to pubspec.yaml:

dependencies:
  json_object: any

Now M7 (Dart Beta) compatible.

All tests passing with SDK 0.8.10.3_r29803

See Changelog.

You can use JsonObject in two different ways.

1 Accessing JSON Maps in a class-based fashion

Read the article about using this on the dartlang website: http://www.dartlang.org/articles/json-web-service/

JsonObject takes a json string representation, and uses the dart:json library to parse it back into a map. JsonObject then takes the parsed output, and converts any maps (recursively) into JsonObjects, which allow use of dot notation for property access (via noSuchMethod).

Examples:

// create from existing JSON
var person = new JsonObject.fromJsonString('{"name":"Chris"}');
print(person.name);
person.name = "Chris B";
person.namz = "Bob"; //throws an exception, as it wasn't in the original json
                     //good for catching typos

person.isExtendable = true;
person.namz = "Bob" //this is allowed now
String jsonString = JSON.encode(person); // convert back to JSON

It implements Map, so you can convert it back to Json using JSON.encode():

// starting from an empty map
var animal = new JsonObject();  
animal.legs = 4;  // equivalent to animal["legs"] = 4;
animal.name = "Fido"; // equivalent to animal["name"] = "Fido";
String jsonString = JSON.encode(animal); // convert to JSON

Take a look at the unit tests to get an idea of how you can use it.


2. Experimental Using reflection to serialize from a real class instance to JSON

(Requires use of a the experimental mirrors branch)

Use objectToJson(myObj) to return a future containing the serialized string.

Example: import 'package:json_object/json_object.dart';

class Other {
    String name = "My Name";
}

class Basic {
   String myString = "foo";
   int myInt = 42;
   Other name = new Other();
}

main() {
  var basic = new Basic();
  objectToJson(basic).then((jsonStr) => print(jsonStr));
}

TODO: * I still feel that there aren't enough tests - let me know if it works for you.

Many of the unit tests are based around specific questions from users, either here or on stack overflow.

1.0.18+2 07/11/2013

Doc fixes.

1.0.18 07/11/2013

Changed stringify to encode Removed meta package requirement All tests passing with 0.8.10.3_r29803

1.0.17 17/Sept/2013

Added @proxy Migrated from dart:json to dart:convert library All tests passing with 0.7.2.1_r27268

1. Depend on it

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

dependencies:
  json_object: ">=1.0.18+2 <2.0.0"

If your package is an application package you should use any as the version constraint.

2. Install it

If you're using the Dart Editor, choose:

Menu > Tools > Pub Install

Or if you want to install from the command line, run:

$ pub install

3. Import it

Now in your Dart code, you can use:

import 'package:json_object/json_object.dart';

About

Allow use of JSON Maps in dot notation format

Author

Email chrisbuckett@gmail.com Chris Buckett

Homepage

github.com/chrisbu/dartwatch-JsonObject

Documentation

www.dartdocs.org/documentation/json_obje...

Uploader

chrisbuckett

Share