Skip to content

Commit 62aebaa

Browse files
committed
upgrade to v1.4.0.
1 parent 3a7c691 commit 62aebaa

File tree

7 files changed

+128
-10
lines changed

7 files changed

+128
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ To install the library add:
1414
maven { url "https://jitpack.io" }
1515
}
1616
dependencies {
17-
compile 'com.github.webee:fastjson-json-api-android:v1.2.0'
17+
compile 'com.github.webee:fastjson-json-api-android:v1.4.0'
1818
}
1919
```

fastjson-json-api-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies {
3232
})
3333
compile 'com.android.support:appcompat-v7:24.2.1'
3434
compile 'com.alibaba:fastjson:1.1.55.android'
35-
compile 'com.github.webee:java-json-api:v1.2.0'
35+
compile 'com.github.webee:java-json-api:v1.4.0'
3636
testCompile 'junit:junit:4.12'
3737
}
3838

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.webee.fastjson;
2+
3+
import com.github.webee.json.JSONType;
4+
import com.github.webee.json.Utils;
5+
6+
import java.util.Map;
7+
8+
/**
9+
* Created by webee on 16/11/27.
10+
*/
11+
12+
public final class Commons {
13+
public static Object resolveValue(Object value) {
14+
JSONType t = Utils.getType(value);
15+
if (t != null) {
16+
return Utils.resolveValue(value, t);
17+
}
18+
if (value instanceof com.alibaba.fastjson.JSONObject) {
19+
return Utils.objectToMap(new JSONObject((com.alibaba.fastjson.JSONObject) value));
20+
} else if (value instanceof com.alibaba.fastjson.JSONArray) {
21+
return Utils.arrayToObjects(new JSONArray((com.alibaba.fastjson.JSONArray) value));
22+
} else if (value instanceof Map) {
23+
return Utils.objectToMap((Map<String, Object>) value);
24+
} else if (value instanceof Object[]) {
25+
return Utils.arrayToObjects((Object[]) value);
26+
}
27+
return null;
28+
}
29+
30+
public static JSONType getType(Object value) {
31+
JSONType t = Utils.getType(value);
32+
if (t != null) {
33+
return t;
34+
}
35+
if (value instanceof com.alibaba.fastjson.JSONObject) {
36+
return JSONType.Object;
37+
} else if (value instanceof com.alibaba.fastjson.JSONArray) {
38+
return JSONType.Array;
39+
} else if (value instanceof Map) {
40+
return JSONType.Object;
41+
} else if (value instanceof Object[]) {
42+
return JSONType.Array;
43+
}
44+
return null;
45+
}
46+
}

fastjson-json-api-android/src/main/java/com/github/webee/fastjson/JSONArray.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.github.webee.fastjson;
22

3-
import com.github.webee.json.*;
3+
import com.github.webee.json.JSONType;
4+
import com.github.webee.json.Utils;
5+
6+
import java.math.BigDecimal;
7+
import java.math.BigInteger;
48

59
/**
610
* Created by webee on 16/11/25.
@@ -23,25 +27,39 @@ public JSONType getType(int index) {
2327
if (index >= jsonArray.size()) {
2428
return null;
2529
}
26-
27-
return Utils.getType(jsonArray.get(index));
30+
return Commons.getType(jsonArray.get(index));
2831
}
2932

3033
@Override
3134
public boolean isNull(int index) {
3235
return jsonArray.get(index) == null;
3336
}
3437

38+
@Override
39+
public Object[] get() {
40+
return Utils.arrayToObjects(this);
41+
}
42+
3543
@Override
3644
public Object get(int index) {
37-
return jsonArray.get(index);
45+
return Commons.resolveValue(jsonArray.get(index));
3846
}
3947

4048
@Override
4149
public Boolean getBoolean(int index) {
4250
return jsonArray.getBoolean(index);
4351
}
4452

53+
@Override
54+
public BigDecimal getBigDecimal(int index) {
55+
return jsonArray.getBigDecimal(index);
56+
}
57+
58+
@Override
59+
public BigInteger getBigInteger(int index) {
60+
return jsonArray.getBigInteger(index);
61+
}
62+
4563
@Override
4664
public Integer getInteger(int index) {
4765
return jsonArray.getInteger(index);

fastjson-json-api-android/src/main/java/com/github/webee/fastjson/JSONObject.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.github.webee.json.JSONType;
44
import com.github.webee.json.Utils;
55

6+
import java.math.BigDecimal;
7+
import java.math.BigInteger;
8+
import java.util.Map;
69
import java.util.Set;
710

811
/**
@@ -25,22 +28,37 @@ public boolean hasKey(String key) {
2528
}
2629

2730
public JSONType getType(String key) {
28-
return Utils.getType(jsonObject.get(key));
31+
return Commons.getType(jsonObject.get(key));
2932
}
3033

3134
public boolean isNull(String key) {
3235
return jsonObject.get(key) == null;
3336
}
3437

38+
@Override
39+
public Map<String, Object> get() {
40+
return Utils.objectToMap(this);
41+
}
42+
3543
@Override
3644
public Object get(String key) {
37-
return jsonObject.get(key);
45+
return Commons.resolveValue(jsonObject.get(key));
3846
}
3947

4048
public Boolean getBoolean(String key) {
4149
return jsonObject.getBoolean(key);
4250
}
4351

52+
@Override
53+
public BigDecimal getBigDecimal(String key) {
54+
return jsonObject.getBigDecimal(key);
55+
}
56+
57+
@Override
58+
public BigInteger getBigInteger(String key) {
59+
return jsonObject.getBigInteger(key);
60+
}
61+
4462
@Override
4563
public Integer getInteger(String key) {
4664
return jsonObject.getInteger(key);

fastjson-json-api-android/src/main/java/com/github/webee/fastjson/fastjsonJSON.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ public com.github.webee.json.JSONArray newArray(Object[] array) {
3030

3131
@Override
3232
public Object parse(String text) {
33-
return com.alibaba.fastjson.JSON.parse(text);
33+
Object value = com.alibaba.fastjson.JSON.parse(text);
34+
if (value instanceof com.alibaba.fastjson.JSONObject) {
35+
return new JSONObject((com.alibaba.fastjson.JSONObject) value);
36+
} else if (value instanceof com.alibaba.fastjson.JSONArray) {
37+
return new JSONArray((com.alibaba.fastjson.JSONArray) value);
38+
}
39+
return value;
3440
}
3541

3642
@Override

fastjson-json-api-android/src/test/java/com/github/webee/fastjson/JSONTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.github.webee.fastjson;
22

3-
import com.github.webee.json.*;
3+
import com.github.webee.json.JSON;
44
import com.github.webee.json.JSONObject;
55

66
import org.junit.Test;
77

8+
import java.util.Map;
9+
810
/**
911
* Created by webee on 16/11/25.
1012
*/
@@ -26,6 +28,8 @@ public void testEncoding() {
2628
scores.set("golang", 82.5);
2729
jsonObject.set("scores", scores);
2830

31+
System.out.println(jsonObject.get("languages").getClass());
32+
System.out.println(jsonObject.get("scores").getClass());
2933
System.out.println(jsonObject.toJSONString());
3034
}
3135

@@ -35,6 +39,8 @@ public void testDecoding() {
3539
String text = "{\"age\":27,\"graduated\":true,\"height\":1.74,\"languages\":[\"java\",\"python\",\"golang\"],\"name\":\"webee\",\"scores\":{\"golang\":82.5,\"java\":80,\"python\":85}}";
3640
JSONObject jsonObject = json.parseObject(text);
3741

42+
System.out.println(jsonObject.get("languages").getClass());
43+
System.out.println(jsonObject.get("scores").getClass());
3844
System.out.println(jsonObject.toJSONString());
3945
}
4046

@@ -46,4 +52,28 @@ public void test() {
4652
jsonObject.set("key", "中国\uD83D\uDE00");
4753
System.out.println(jsonObject.toJSONString());
4854
}
55+
56+
@Test
57+
public void testParse() {
58+
JSON json = new fastjsonJSON();
59+
System.out.println(json.parse("null"));
60+
System.out.println(json.parse("true").getClass());
61+
System.out.println(json.parse("\"abc\"").getClass());
62+
System.out.println(json.parse("0").getClass());
63+
System.out.println(json.parse("123456789").getClass());
64+
System.out.println(json.parse("1234567890123456").getClass());
65+
System.out.println(json.parse("1234.0").getClass());
66+
System.out.println(json.parse("[]").getClass());
67+
System.out.println(json.parseArray("[]").get().getClass());
68+
System.out.println(json.parse("{}").getClass());
69+
System.out.println(json.parseObject("{}").get().getClass());
70+
System.out.println(json.parseObject("{\"a\":{}}").get("a").getClass());
71+
Map<String, Object> a = (Map<String, Object>) json.parseObject("{\"a\":{\"b\":[1,2.3,{},999999999999999999999999999999999999999999999]}}").get("a");
72+
System.out.println(a.get("b").getClass());
73+
Object[] b = (Object[]) a.get("b");
74+
System.out.println(b[0].getClass());
75+
System.out.println(b[1].getClass());
76+
System.out.println(b[2].getClass());
77+
System.out.println(b[3].getClass());
78+
}
4979
}

0 commit comments

Comments
 (0)