Skip to content

Commit fce9b5b

Browse files
committed
support jsonobj to map
1 parent c64599b commit fce9b5b

4 files changed

Lines changed: 27 additions & 24 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public class RemoteModule implements IRouter {
195195
|List< A>||List< A>|`1.0.0+` Receiver must be defined as List<?> interface|
196196
|List< A>||List< B>|`1.0.0+`|
197197
|Json Object||Object|`1.0.0+`|
198-
|Json Object||Map< String,String>|Coming Soon|
198+
|Json Object||Map< String,String>|`2.0.1+`|
199199
|Json Array||List< ?>|`1.0.0+`|
200200

201201
**Step 2:Invoke**

README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public class RemoteModule implements IRouter {
195195
|List< A>||List< A>|`1.0.0+` 接收者定义必须是List<?>接口类型 eg:List< A>|
196196
|List< A>||List< B>|`1.0.0+`|
197197
|Json Object||Object|`1.0.0+`|
198-
|Json Object||Map< String,String>|即将支持|
198+
|Json Object||Map< String,String>|`2.0.1+`|
199199
|Json Array||List< ?>|`1.0.0+`|
200200

201201
**第二步:调用协议**

androidrouter/src/androidTest/java/com/tangxiaolv/router/utils/ValueParserTest.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,6 @@ public void check_Obj_Obj() throws ValueParseException {
7171
assertTrue(parse instanceof B);
7272
}
7373

74-
@Test
75-
public void check_Obj_Map() throws ValueParseException {
76-
// A a = ObjGenerator.getA();
77-
//
78-
// Object parse = ValueParser.parse(a, Map.class.getCanonicalName());
79-
// assertTrue(parse instanceof Map);
80-
}
81-
82-
@Test
83-
public void check_Map_Obj() {
84-
85-
86-
}
87-
8874
@Test
8975
public void check_Json_Obj() throws ValueParseException {
9076
A a = ObjGenerator.getA();
@@ -115,7 +101,6 @@ public void check_Json_List() throws ValueParseException {
115101
assertNotNull(parse);
116102
assertTrue(parse instanceof List);
117103
assertTrue(((List) parse).get(0) instanceof A);
118-
119104
}
120105

121106
@Test
@@ -127,6 +112,14 @@ public void check_Json_Map() throws ValueParseException {
127112
assertTrue(parse instanceof Map);
128113
}
129114

115+
@Test
116+
public void check_Obj_Map() throws ValueParseException {
117+
}
118+
119+
@Test
120+
public void check_Map_Obj() {
121+
}
122+
130123
@SuppressWarnings("all")
131124
public static class Mark<T> {
132125
}

androidrouter/src/main/java/com/tangxiaolv/router/utils/ValueParser.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static Object toArray(Object from, String expectedType) throws ValuePars
136136
} else {
137137
//checked string format
138138
if (!isJson(from.toString())) {
139-
throw new ValueParseException("Expected array type,The input string must jsonArray.");
139+
throw new ValueParseException("Expected " + expectedType + ",But The input string isn't json.");
140140
}
141141
jArray = new JSONArray((String) from);
142142
}
@@ -205,20 +205,30 @@ private static Object toList(Object from, String expectedType) throws ValueParse
205205
return from;
206206
}
207207

208-
private static Object toMap(Object from, String expectType) {
208+
private static Object toMap(Object from, String expectType) throws ValueParseException {
209209
try {
210210
if (from instanceof String || from instanceof JSONObject) {
211-
JSONObject jObj = from instanceof String ? new JSONObject((String) from) : (JSONObject) from;
212-
HashMap<String, Object> map = new HashMap<>(jObj.length());
211+
JSONObject jObj;
212+
if (from instanceof JSONObject) {
213+
jObj = (JSONObject) from;
214+
} else {
215+
//checked string format
216+
if (!isJson(from.toString())) {
217+
throw new ValueParseException("Expected " + expectType + ",But The input string isn't json.");
218+
}
219+
jObj = new JSONObject((String) from);
220+
}
221+
222+
HashMap<String, String> map = new HashMap<>(jObj.length());
213223
Iterator<String> it = jObj.keys();
214224
while (it.hasNext()) {
215225
String key = it.next();
216-
Object v = jObj.get(key);
217-
map.put(key, v);
226+
map.put(key, jObj.get(key).toString());
218227
}
219228
from = map;
220229
}
221-
} catch (Exception ignored) {
230+
} catch (Exception e) {
231+
throw new ValueParseException("parse to " + expectType + " type fail.", e);
222232
}
223233
return from;
224234
}

0 commit comments

Comments
 (0)