Skip to content

Commit 0b2b175

Browse files
committedDec 17, 2016
upgrade api to v2.1.0, update tests.
1 parent feae14c commit 0b2b175

File tree

8 files changed

+91
-63
lines changed

8 files changed

+91
-63
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:v2.0.0'
17+
compile 'com.github.webee:fastjson-json-api-android:v2.1.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:v2.0.0'
35+
compile 'com.github.webee:java-json-api:v2.1.0'
3636
testCompile 'junit:junit:4.12'
3737
}
3838

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,35 @@
33
import com.github.webee.json.JSONType;
44
import com.github.webee.json.Utils;
55

6-
import java.util.Map;
7-
86
/**
97
* Created by webee on 16/11/27.
108
*/
119

1210
public final class Commons {
1311
public static Object resolveValue(Object value) {
14-
JSONType t = Utils.getType(value);
15-
if (t != null) {
16-
return Utils.resolveValue(value, t);
17-
}
1812
if (value instanceof com.alibaba.fastjson.JSONObject) {
1913
return Utils.objectToMap(new JSONObject((com.alibaba.fastjson.JSONObject) value));
2014
} else if (value instanceof com.alibaba.fastjson.JSONArray) {
2115
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);
2616
}
27-
return null;
28-
}
2917

30-
public static JSONType getType(Object value) {
3118
JSONType t = Utils.getType(value);
3219
if (t != null) {
33-
return t;
20+
return Utils.resolveValue(value, t);
3421
}
22+
return null;
23+
}
24+
25+
public static JSONType getType(Object value) {
3526
if (value instanceof com.alibaba.fastjson.JSONObject) {
3627
return JSONType.Object;
3728
} else if (value instanceof com.alibaba.fastjson.JSONArray) {
3829
return JSONType.Array;
39-
} else if (value instanceof Map) {
40-
return JSONType.Object;
41-
} else if (value instanceof Object[]) {
42-
return JSONType.Array;
30+
}
31+
32+
JSONType t = Utils.getType(value);
33+
if (t != null) {
34+
return t;
4335
}
4436
return null;
4537
}

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

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

3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.serializer.JSONSerializer;
5+
import com.alibaba.fastjson.serializer.SerializeConfig;
6+
import com.alibaba.fastjson.serializer.SerializeWriter;
7+
import com.alibaba.fastjson.serializer.SerializerFeature;
38
import com.github.webee.json.JSONType;
49
import com.github.webee.json.Utils;
510

11+
import java.io.Writer;
612
import java.math.BigDecimal;
713
import java.math.BigInteger;
814

@@ -92,6 +98,12 @@ public com.github.webee.json.JSONObject getObject(int index) {
9298

9399
@Override
94100
public String toJSONString() {
95-
return jsonArray.toJSONString();
101+
SerializeWriter out = new SerializeWriter((Writer) null, JSON.DEFAULT_GENERATE_FEATURE, new SerializerFeature[]{SerializerFeature.WriteMapNullValue});
102+
try {
103+
new JSONSerializer(out, SerializeConfig.globalInstance).write(jsonArray);
104+
return out.toString();
105+
} finally {
106+
out.close();
107+
}
96108
}
97109
}

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

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

3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.serializer.JSONSerializer;
5+
import com.alibaba.fastjson.serializer.SerializeConfig;
6+
import com.alibaba.fastjson.serializer.SerializeWriter;
7+
import com.alibaba.fastjson.serializer.SerializerFeature;
38
import com.github.webee.json.JSONType;
49
import com.github.webee.json.Utils;
510

11+
import java.io.Writer;
612
import java.math.BigDecimal;
713
import java.math.BigInteger;
814
import java.util.Map;
@@ -95,6 +101,12 @@ public com.github.webee.json.JSONObject getObject(String key) {
95101

96102
@Override
97103
public String toJSONString() {
98-
return jsonObject.toJSONString();
104+
SerializeWriter out = new SerializeWriter((Writer) null, JSON.DEFAULT_GENERATE_FEATURE, new SerializerFeature[]{SerializerFeature.WriteMapNullValue});
105+
try {
106+
new JSONSerializer(out, SerializeConfig.globalInstance).write(jsonObject);
107+
return out.toString();
108+
} finally {
109+
out.close();
110+
}
99111
}
100112
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public boolean push(Object value) {
1818

1919
@Override
2020
public Object set(int index, Object value) {
21-
return jsonArray.set(index, Utils.resolveValue(value));
21+
return Commons.resolveValue(jsonArray.set(index, Utils.resolveValue(value)));
2222
}
2323

2424
@Override
2525
public Object remove(int index) {
26-
return jsonArray.remove(index);
26+
return Commons.resolveValue(jsonArray.remove(index));
2727
}
2828
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public WritableJSONObject(com.alibaba.fastjson.JSONObject jsonObject) {
1313

1414
@Override
1515
public Object set(String key, Object value) {
16-
return jsonObject.put(key, Utils.resolveValue(value));
16+
return Commons.resolveValue(jsonObject.put(key, Utils.resolveValue(value)));
1717
}
1818

1919
@Override
2020
public Object remove(String key) {
21-
return jsonObject.remove(key);
21+
return Commons.resolveValue(jsonObject.remove(key));
2222
}
2323
}

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

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
import com.github.webee.json.JSONObject;
55
import com.github.webee.json.WritableJSONObject;
66

7+
import org.junit.Assert;
78
import org.junit.Test;
89

9-
import java.util.Map;
10-
1110
/**
1211
* Created by webee on 16/11/25.
1312
*/
@@ -22,68 +21,81 @@ public void testEncoding() {
2221
jsonObject.set("address", null);
2322
jsonObject.set("height", 1.74);
2423
jsonObject.set("graduated", true);
25-
jsonObject.set("languages", new Object[]{"java", "python", "golang"});
24+
jsonObject.set("languages", new Object[]{"java", "python", "golang", null});
2625

2726
WritableJSONObject scores = json.newObject();
2827
scores.set("java", 80);
2928
scores.set("python", 85.0);
3029
scores.set("golang", 82.5);
30+
scores.set("xxx", null);
3131
jsonObject.set("scores", scores);
3232

33-
System.out.println(jsonObject.get("languages").getClass());
34-
System.out.println(jsonObject.get("scores").getClass());
33+
Assert.assertEquals(jsonObject.getString("name"), "webee.易");
34+
Assert.assertEquals(jsonObject.getInteger("age"), Integer.valueOf(27));
35+
Assert.assertEquals(jsonObject.getDouble("height"), Double.valueOf(1.74));
36+
Assert.assertEquals(jsonObject.getString("address"), null);
37+
Assert.assertEquals(jsonObject.isNull("address"), true);
38+
Assert.assertEquals(jsonObject.getBoolean("graduated"), true);
39+
Assert.assertEquals(jsonObject.getArray("languages").getString(0), "java");
40+
Assert.assertEquals(jsonObject.getArray("languages").getString(3), null);
41+
Assert.assertEquals(jsonObject.getArray("languages").isNull(3), true);
42+
Assert.assertEquals(jsonObject.getObject("scores").getDouble("java"), Double.valueOf(80));
43+
Assert.assertEquals(jsonObject.getObject("scores").getDouble("xxx"), null);
44+
Assert.assertEquals(jsonObject.getObject("scores").isNull("xxx"), true);
45+
46+
//System.out.println(jsonObject.get("languages").getClass());
47+
//System.out.println(jsonObject.get("scores").getClass());
3548
System.out.println(jsonObject.toJSONString());
3649
}
3750

3851
@Test
3952
public void testDecoding() {
40-
String text = "{\"age\":27,\"graduated\":true,\"height\":1.74,\"languages\":[\"java\",\"python\",\"golang\"],\"name\":\"webee.易\",\"scores\":{\"golang\":82.5,\"java\":80,\"python\":85}}";
53+
String text = "{\"address\":null,\"age\":27,\"graduated\":true,\"height\":1.74,\"languages\":[\"java\",\"python\",\"golang\",null],\"name\":\"webee.易\",\"scores\":{\"golang\":82.5,\"java\":80,\"python\":85,\"xxx\":null}}";
4154
JSONObject jsonObject = json.parseObject(text);
4255

43-
System.out.println(jsonObject.isNull("address"));
44-
System.out.println(jsonObject.get("languages").getClass());
45-
System.out.println(jsonObject.get("scores").getClass());
46-
System.out.println(jsonObject.toJSONString());
47-
}
48-
49-
@Test
50-
public void test() {
51-
WritableJSONObject jsonObject = json.newObject();
52-
53-
jsonObject.set("key", "中国\uD83D\uDE00");
54-
System.out.println(jsonObject.toJSONString());
56+
Assert.assertEquals(jsonObject.getString("name"), "webee.易");
57+
Assert.assertEquals(jsonObject.getInteger("age"), Integer.valueOf(27));
58+
Assert.assertEquals(jsonObject.getDouble("height"), Double.valueOf(1.74));
59+
Assert.assertEquals(jsonObject.isNull("address"), true);
60+
Assert.assertEquals(jsonObject.getString("address"), null);
61+
Assert.assertEquals(jsonObject.getBoolean("graduated"), true);
62+
Assert.assertEquals(jsonObject.getArray("languages").getString(0), "java");
63+
Assert.assertEquals(jsonObject.getArray("languages").getString(3), null);
64+
Assert.assertEquals(jsonObject.getArray("languages").isNull(3), true);
65+
Assert.assertEquals(jsonObject.getObject("scores").getDouble("java"), Double.valueOf(80));
66+
Assert.assertEquals(jsonObject.getObject("scores").getDouble("xxx"), null);
67+
Assert.assertEquals(jsonObject.getObject("scores").isNull("xxx"), true);
5568
}
5669

5770
@Test
5871
public void testParse() {
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());
72+
Assert.assertEquals(json.parse("null"), null);
73+
Assert.assertEquals(json.parse("true"), true);
74+
Assert.assertEquals(json.parse("\"abc\""), "abc");
75+
Assert.assertEquals(json.parse("0"), 0);
76+
Assert.assertEquals(json.parse("123456789"), 123456789);
77+
Assert.assertEquals(json.parse("1234567890123456"), 1234567890123456L);
78+
Assert.assertEquals(json.parse("[]") instanceof com.github.webee.json.JSONArray, true);
79+
Assert.assertArrayEquals(json.parseArray("[]").get(), new Object[0]);
80+
Assert.assertEquals(json.parse("{}") instanceof com.github.webee.json.JSONObject, true);
81+
Assert.assertEquals(json.parseObject("{}") instanceof com.github.webee.json.JSONObject, true);
82+
/*
7183
Map<String, Object> a = (Map<String, Object>) json.parseObject("{\"a\":{\"b\":[1,2.3,{},999999999999999999999999999999999999999999999]}}").get("a");
7284
System.out.println(a.get("b").getClass());
7385
Object[] b = (Object[]) a.get("b");
7486
System.out.println(b[0].getClass());
7587
System.out.println(b[1].getClass());
7688
System.out.println(b[2].getClass());
7789
System.out.println(b[3].getClass());
90+
*/
7891
}
7992

8093
@Test
81-
public void testParseMsg() {
82-
String msg = "{\"messageType\":0,\"text\":\"txt2\"}";
83-
JSONObject value = json.parseObject(msg);
84-
Map<String, Object> map = value.get();
85-
Integer t = value.getInteger("messageType");
86-
System.out.println(map);
87-
System.out.println(t);
94+
public void testEmoji() {
95+
WritableJSONObject jsonObject = json.newObject();
96+
97+
String value = "中国\uD83D\uDE00";
98+
jsonObject.set("key", value);
99+
Assert.assertEquals(jsonObject.getString("key"), value);
88100
}
89101
}

0 commit comments

Comments
 (0)