Created
February 17, 2019 22:04
-
-
Save lrhn/a8a0487f37513900c201b26a37a4bf5c to your computer and use it in GitHub Desktop.
JSON object wrapper.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:core"; | |
import "dart:core" as c; | |
abstract class _JsonBase<Key> { | |
Object _get(Key key, c.bool allowNull); | |
List<T> listOf<T>(Key key) => (_get(key, false) as List<Object>).cast<T>(); | |
Map<String, T> mapOf<T>(Key key) => | |
(_get(key, false) as Map<String, Object>).cast<String, T>(); | |
JsonObject object(Key key) => | |
JsonObject(_get(key, false) as Map<String, Object>); | |
JsonObject objectOrNull(Key key) => | |
JsonObject(_get(key, true) as Map<String, Object>); | |
JsonArray array(Key key) => JsonArray(_get(key, false) as List<Object>); | |
JsonArray arrayOrNull(Key key) => JsonArray(_get(key, true) as List<Object>); | |
String string(Key key) => _get(key, false) as String; | |
String stringOrNull(Key key) => _get(key, true) as String; | |
c.int int(Key key) => _get(key, false) as c.int; | |
c.int intOrNull(Key key) => _get(key, true) as c.int; | |
c.double double(Key key) => _get(key, false) as c.double; | |
c.double doubleOrNull(Key key) => _get(key, true) as c.double; | |
c.num num(Key key) => _get(key, false) as c.num; | |
c.num numOrNull(Key key) => _get(key, true) as c.num; | |
c.bool bool(Key key) => _get(key, false) as c.bool; | |
c.bool boolOrNull(Key key) => _get(key, true) as c.bool; | |
} | |
class JsonObject extends _JsonBase<String> { | |
final Map<String, Object> map; | |
JsonObject(this.map); | |
Object operator [](String key) => map[key]; | |
Object _get(String key, bool allowNull) { | |
var object = map[key]; | |
if (object == null) { | |
if (map.containsKey(key)) { | |
if (!allowNull) throw CastError("Value of key $key is null"); | |
} else { | |
throw ArgumentError.value(key, "key", "Not in the map"); | |
} | |
} | |
return object; | |
} | |
} | |
class JsonArray extends _JsonBase<int> { | |
final List<Object> list; | |
JsonArray(this.list); | |
int get length => list.length; | |
Object operator [](int key) => list[key]; | |
Object _get(int key, bool allowNull) { | |
RangeError.checkValidIndex(key, list, "key", list.length); | |
var object = list[key]; | |
if (object == null && !allowNull) { | |
throw CastError("Value at index $key is null"); | |
} | |
return object; | |
} | |
} | |
class CastError extends c.CastError { | |
String _message; | |
CastError(String message) : _message = message; | |
String toString() => "CastError: $_message"; | |
} | |
// Example | |
main() { | |
var obj = {"a": [1, 2.3, true, "s", null, [4], {"x": 5}], | |
"b": {"x": 6}, | |
"c": 7, | |
"d": 8.9, | |
"e": false, | |
"f": "s", | |
"g": null}; | |
var js = JsonObject(obj); | |
print(js.array("a").int(0)); | |
print(js.array("a").double(1)); | |
print(js.array("a").bool(2)); | |
print(js.array("a").string(3)); | |
print(js.array("a").stringOrNull(4)); | |
print(js.array("a").array(5).int(0)); | |
print(js.array("a").object(6).int("x")); | |
print(js.object("b").int("x")); | |
print(js.int("c")); | |
print(js.double("d")); | |
print(js.bool("e")); | |
print(js.string("f")); | |
print(js.stringOrNull("g")); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment