@@ -10,50 +10,36 @@ part 'src/exception.dart';
1010part 'src/util.dart' ;
1111
1212class JsonObject {
13- Invocation _invocation ;
13+ JsonObject ._() ;
1414
15- Map _map;
16- bool get isMap => _map != null ;
17- List _list;
18- bool get isList => _list != null ;
19- Object _normalValue;
20- bool get isNormalValue => _normalValue != null ;
21- String get _innerDataType {
22- if (isMap) return _map.runtimeType.toString ();
23- if (isList) return _list.runtimeType.toString ();
24- return _normalValue.runtimeType.toString ();
15+ /// Creates a dynamic(actually a JsonObject) from json string.
16+ static dynamic fromString (String jsonStr) {
17+ final decodeResult = json.decode (jsonStr);
18+ final jsonObject = JsonObject ._();
19+
20+ if (decodeResult is Map ) return jsonObject.._map = decodeResult;
21+ if (decodeResult is List ) return jsonObject.._list = decodeResult;
22+ return jsonObject.._other = decodeResult;
2523 }
2624
27- void Function (Object newValue) _listen;
25+ /// Creates a dynamic(actually a JsonObject) that contains
26+ /// all the values of the [otherJsonObject] .
27+ static dynamic from (dynamic otherJsonObject) =>
28+ fromString (otherJsonObject.encode ());
2829
29- static bool isEmpty (JsonObject jsonObject) {
30- return jsonObject == null ||
31- jsonObject._map == null &&
32- jsonObject._list == null &&
33- jsonObject._normalValue == null ;
34- }
30+ Map _map;
31+ List _list;
32+ dynamic _other;
33+ String get _valueType => getValue ().runtimeType.toString ();
3534
36- static bool isNotEmpty (JsonObject jsonObject) => ! isEmpty (jsonObject);
35+ void Function (dynamic newValue) _listen;
36+ void _notify (dynamic newValue) => _listen (newValue);
3737
38- /// Creates a dynamic(actually a JsonObject) from json string.
39- dynamic fromString (String jsonStr) {
40- var decodeResult = json.decode (jsonStr);
41-
42- if (decodeResult is Map ) {
43- _map = decodeResult;
44- return this ;
45- }
46- if (decodeResult is List ) {
47- _list = decodeResult;
48- return this ;
49- }
50- _normalValue = decodeResult;
51- return this ;
52- }
38+ static bool isEmpty (JsonObject jsonObject) =>
39+ jsonObject == null || jsonObject.getValue () == null ;
40+ static bool isNotEmpty (JsonObject jsonObject) => ! isEmpty (jsonObject);
5341
54- /// Creates a dynamic(actually a JsonObject) that contains
55- /// all the values of the [otherObject] .
56- dynamic from (JsonObject otherObject) => fromString (otherObject.encode ());
42+ Invocation _invocation;
5743
5844 /// This is the core magic of json_object.
5945 /// It uses [invocation] in [noSuchMethod]
@@ -62,11 +48,11 @@ class JsonObject {
6248 dynamic noSuchMethod (Invocation invocation) {
6349 _invocation = invocation;
6450
65- if (_isGetMapValue ) return _mapValue ;
66- if (_isSetMapValue ) return _setMapValue ();
51+ if (isGettingMapValue ) return getMapValue () ;
52+ if (isSettingMapValue ) return setMapValue ();
6753
68- if (_isGetListValue ) return _listValue ;
69- if (_isSetListValue ) return _setListValue ();
54+ if (isGettingListValue ) return getListValue () ;
55+ if (isSettingListValue ) return setListValue ();
7056
7157 throw _JsonObjectException .uncaughtException ();
7258 }
@@ -124,47 +110,47 @@ class JsonObject {
124110 /// [jsonObject.name] . Also note that you only need to call [getValue()]
125111 /// at the end of the value, which means you can't use
126112 /// [jsonObject.getValue().name.getValue()] to get the value.
127- Object getValue () => _JsonObjectUtil (this ).getValue ();
113+ dynamic getValue () => _JsonObjectUtil (this ).getValue ();
128114
129- /// Return index list of [_list] if JsonObject inner value [isList ] .
115+ /// Return index list of [_list] if JsonObject inner value [getValue() is List ] .
130116 /// For example, if inner value is `["a", "b", "c"]` , then this method
131117 /// returns `[0, 1, 2]` .
132118 ///
133- /// Return [_map.keys] if JsonObject inner value [isMap ] .
119+ /// Return [_map.keys] if JsonObject inner value [getValue() is Map ] .
134120 /// For example, if inner value is `{"a": 1, "b": 2, "c": 3}` , then
135121 /// this method returns `["a", "b", "c"]` .
136122 ///
137- /// Return [_normalValue .toString().split("")] if
138- /// JsonObject inner value [isNormalValue ] .
123+ /// Return [_other .toString().split("")] if
124+ /// JsonObject inner value [_other != null ] .
139125 /// For example, if inner value is `"love you"` , then this
140126 /// method returns `["l", "o", "v", "e", " ", "y", "o", "u"]`
141127 Iterable get keys => _JsonObjectUtil (this ).keys;
142128
143- /// Return [_list.length] if JsonObject inner value [isList ] .
129+ /// Return [_list.length] if JsonObject inner value [getValue() is List ] .
144130 /// For example, if inner value is `["a", "b", "c"]` , then this method
145131 /// returns 3.
146132 ///
147- /// Return [_map.keys.length] if JsonObject inner value [isMap ] .
133+ /// Return [_map.keys.length] if JsonObject inner value [getValue() is Map ] .
148134 /// For example, if inner value is `{"a": 1, "b": 2, "c": 3}` , then
149135 /// this method returns 3, which is the length of [_map.keys] ,
150136 /// known as `["a", "b", "c"]` .
151137 ///
152- /// Return [_normalValue .toString().length] if
153- /// JsonObject inner value [isNormalValue ] .
138+ /// Return [_other .toString().length] if
139+ /// JsonObject inner value [_other != null ] .
154140 /// For example, if inner value is `"love you"` , then this
155141 /// method returns 8, which is the length of String `"love you"` .
156142 int get length => _JsonObjectUtil (this ).length;
157143
158144 /// Add a (key, value) pair in [JsonObject] .
159145 ///
160- /// If the inner value [isMap ] , it adds
146+ /// If the inner value [getValue() is Map ] , it adds
161147 /// a (key, value) pair in [JsonObject] .
162148 ///
163- /// If the inner value [isList ] , it adds
149+ /// If the inner value [getValue() is List ] , it adds
164150 /// the value to `[_list[key]]` , this [key]
165151 /// here represents [index] .
166152 ///
167- /// If the inner value [isNormalValue ] , it
153+ /// If the inner value [_other != null ] , it
168154 /// throws an Exception.
169- void add (Object key, Object value) => _JsonObjectUtil (this ).add (key, value);
155+ void add (dynamic key, dynamic value) => _JsonObjectUtil (this ).add (key, value);
170156}
0 commit comments