Skip to content
This repository was archived by the owner on May 7, 2022. It is now read-only.

Commit 3c0a8cd

Browse files
committed
refractor: changed some method names
1 parent 6fd508c commit 3c0a8cd

5 files changed

Lines changed: 84 additions & 110 deletions

File tree

example/lib/home.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class _HomePageState extends State<HomePage> {
2828
@override
2929
void initState() {
3030
super.initState();
31-
jsonObject = JsonObject().fromString(jsonStr);
31+
jsonObject = JsonObject.fromString(jsonStr);
3232
}
3333

3434
@override

lib/json_object.dart

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,36 @@ part 'src/exception.dart';
1010
part 'src/util.dart';
1111

1212
class 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
}

lib/src/list_extension.dart

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
part of json_object;
22

33
extension _ListExtension on JsonObject {
4-
bool get _isGetListValue =>
4+
bool get isGettingListValue =>
55
_invocation.isMethod && !_invocation.memberName.toString().contains(r'=');
6-
dynamic get _listValue {
7-
if (isMap) {
6+
dynamic getListValue() {
7+
if (getValue() is Map) {
88
throw _JsonObjectException.getValueException(
99
innerDataType: "Map",
1010
expectedType: "List",
@@ -20,17 +20,14 @@ extension _ListExtension on JsonObject {
2020
}
2121

2222
var valueStr = json.encode(_list[index]);
23-
var child = JsonObject()
24-
.._listen = (newValue) {
25-
_list[index] = newValue;
26-
};
27-
return child.fromString(valueStr);
23+
return JsonObject.fromString(valueStr)
24+
.._listen = (newValue) => _list[index] = newValue;
2825
}
2926

30-
bool get _isSetListValue =>
27+
bool get isSettingListValue =>
3128
_invocation.isMethod && _invocation.memberName.toString().contains(r'=');
32-
void _setListValue() {
33-
if (isMap) {
29+
void setListValue() {
30+
if (getValue() is Map) {
3431
throw _JsonObjectException.setValueException(
3532
innerDataType: "Map",
3633
expectedType: "List",
@@ -47,20 +44,19 @@ extension _ListExtension on JsonObject {
4744
}
4845

4946
_list[index] = value;
50-
_listen?.call(_list);
47+
_notify(_list);
5148
}
5249

53-
void _listAdd(Object key, Object value) {
50+
void _listAdd(dynamic key, dynamic value) {
5451
if (key is! int) {
5552
throw _JsonObjectException.methodInvokeException(
5653
methodName: "add",
57-
innerDataType: _innerDataType,
54+
innerDataType: _valueType,
5855
expectedType: "Map",
5956
);
6057
}
6158

6259
_list.add(value);
63-
_listen?.call(_list);
64-
return;
60+
_notify(_list);
6561
}
6662
}

lib/src/map_extension.dart

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
part of json_object;
22

33
extension _MapExtension on JsonObject {
4-
bool get _isGetMapValue => _invocation.isAccessor && _invocation.isGetter;
5-
dynamic get _mapValue {
6-
if (isList) {
4+
bool get isGettingMapValue => _invocation.isAccessor && _invocation.isGetter;
5+
dynamic getMapValue() {
6+
if (getValue() is List) {
77
throw _JsonObjectException.getValueException(
88
innerDataType: "List",
99
expectedType: "Map",
@@ -16,16 +16,13 @@ extension _MapExtension on JsonObject {
1616
}
1717

1818
var valueStr = json.encode(_map[key]);
19-
var child = JsonObject()
20-
.._listen = (newValue) {
21-
_map[key] = newValue;
22-
};
23-
return child.fromString(valueStr);
19+
return JsonObject.fromString(valueStr)
20+
.._listen = (newValue) => _map[key] = newValue;
2421
}
2522

26-
bool get _isSetMapValue => _invocation.isAccessor && _invocation.isSetter;
27-
void _setMapValue() {
28-
if (isList) {
23+
bool get isSettingMapValue => _invocation.isAccessor && _invocation.isSetter;
24+
void setMapValue() {
25+
if (getValue() is List) {
2926
throw _JsonObjectException.setValueException(
3027
innerDataType: "List",
3128
expectedType: "Map",
@@ -35,14 +32,14 @@ extension _MapExtension on JsonObject {
3532
var key = _memberNameStr;
3633
var value = _invocation.positionalArguments.first;
3734
_map[key] = value;
38-
_listen?.call(_map);
35+
_notify(_map);
3936
}
4037

41-
void _mapAdd(Object key, Object value) {
38+
void _mapAdd(dynamic key, dynamic value) {
4239
if (key is int) {
4340
throw _JsonObjectException.methodInvokeException(
4441
methodName: "add",
45-
innerDataType: _innerDataType,
42+
innerDataType: _valueType,
4643
expectedType: "List",
4744
);
4845
}
@@ -59,6 +56,6 @@ extension _MapExtension on JsonObject {
5956
}
6057

6158
_map[key] = value;
62-
_listen?.call(_map);
59+
_notify(_map);
6360
}
6461
}

lib/src/util.dart

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,41 @@ extension _JsonObjectUtil on JsonObject {
44
String encodePretty({int indent = 2}) {
55
var spaces = ' ' * indent;
66
var encoder = JsonEncoder.withIndent(spaces);
7-
if (isMap) return encoder.convert(_map);
8-
if (isList) return encoder.convert(_list);
9-
return encoder.convert(_normalValue);
7+
if (getValue() is Map) return encoder.convert(_map);
8+
if (getValue() is List) return encoder.convert(_list);
9+
return encoder.convert(_other);
1010
}
1111

1212
String encode() {
13-
if (isMap) return json.encode(_map);
14-
if (isList) return json.encode(_list);
15-
return json.encode(_normalValue);
13+
if (getValue() is Map) return json.encode(_map);
14+
if (getValue() is List) return json.encode(_list);
15+
return json.encode(_other);
1616
}
1717

18-
Object getValue() {
19-
if (isMap) return _map;
20-
if (isList) return _list;
21-
return _normalValue;
22-
}
18+
dynamic getValue() => _map ?? _list ?? _other;
2319

2420
Iterable get keys {
25-
if (isMap) return _map.keys;
26-
if (isList) return List.generate(_list.length, (index) => index);
27-
if (isNormalValue) return _normalValue.toString().split("");
28-
throw _JsonObjectException.uncaughtException();
21+
if (getValue() is Map) return _map.keys;
22+
if (getValue() is List)
23+
return List.generate(_list.length, (index) => index);
24+
return _other.toString().split("");
2925
}
3026

3127
int get length {
32-
if (isList) return _list.length;
33-
if (isMap) return _map.keys.length;
34-
if (isNormalValue) return _normalValue.toString().length;
35-
throw _JsonObjectException.uncaughtException();
28+
if (getValue() is List) return _list.length;
29+
if (getValue() is Map) return _map.keys.length;
30+
return _other.toString().length;
3631
}
3732

38-
void add(Object key, Object value) {
39-
if (isNormalValue) {
33+
void add(dynamic key, dynamic value) {
34+
if (_other != null) {
4035
throw _JsonObjectException.methodInvokeException(
4136
methodName: "add",
42-
innerDataType: _innerDataType,
37+
innerDataType: _valueType,
4338
expectedType: "Map or List",
4439
);
4540
}
46-
if (isMap) return _mapAdd(key, value);
47-
if (isList) return _listAdd(key, value);
41+
if (getValue() is Map) return _mapAdd(key, value);
42+
if (getValue() is List) return _listAdd(key, value);
4843
}
4944
}

0 commit comments

Comments
 (0)