• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

chrisbu/dartwatch-JsonObject: JsonObject which allows dot notation access to JSO ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

chrisbu/dartwatch-JsonObject

开源软件地址:

https://github.com/chrisbu/dartwatch-JsonObject

开源编程语言:

Dart 100.0%

开源软件介绍:

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

Version 1.0.19

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Using JSON | Dart发布时间:2022-07-09
下一篇:
apgapg/json_table: Flutter package: Json Table Widget to create table from json ...发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap