forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMap.java
More file actions
123 lines (108 loc) · 4.14 KB
/
TestMap.java
File metadata and controls
123 lines (108 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.jsoniter.output;
import com.jsoniter.spi.Config;
import com.jsoniter.spi.JsoniterSpi;
import com.jsoniter.spi.MapKeyEncoder;
import com.jsoniter.spi.TypeLiteral;
import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class TestMap extends TestCase {
static {
// JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
}
private ByteArrayOutputStream baos;
private JsonStream stream;
public void setUp() {
baos = new ByteArrayOutputStream();
stream = new JsonStream(baos, 4096);
}
public void test() throws IOException {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("hello", "world");
stream.writeVal(map);
stream.close();
assertEquals("{'hello':'world'}".replace('\'', '"'), baos.toString());
}
public void test_empty() throws IOException {
HashMap<String, Object> map = new HashMap<String, Object>();
stream.writeVal(map);
stream.close();
assertEquals("{}".replace('\'', '"'), baos.toString());
}
public void test_null() throws IOException {
stream.writeVal(new TypeLiteral<HashMap>() {
}, null);
stream.close();
assertEquals("null".replace('\'', '"'), baos.toString());
}
public void test_value_is_null() throws IOException {
HashMap<String, int[]> obj = new HashMap<String, int[]>();
obj.put("hello", null);
stream.writeVal(new TypeLiteral<Map<String, int[]>>() {
}, obj);
stream.close();
assertEquals("{\"hello\":null}", baos.toString());
}
public void test_integer_key() throws IOException {
HashMap<Integer, Object> obj = new HashMap<Integer, Object>();
obj.put(100, null);
stream.writeVal(new TypeLiteral<Map<Integer, Object>>() {
}, obj);
stream.close();
assertEquals("{\"100\":null}", baos.toString());
}
public static class TestObject1 {
public int Field;
}
public void test_MapKeyCodec() {
JsoniterSpi.registerMapKeyEncoder(TestObject1.class, new MapKeyEncoder() {
@Override
public String encode(Object mapKey) {
TestObject1 obj = (TestObject1) mapKey;
return String.valueOf(obj.Field);
}
});
HashMap<TestObject1, Object> obj = new HashMap<TestObject1, Object>();
obj.put(new TestObject1(), null);
String output = JsonStream.serialize(new TypeLiteral<Map<TestObject1, Object>>() {
}, obj);
assertEquals("{\"0\":null}", output);
}
public void skip_indention() {
Map<String, String> map = new HashMap<String, String>();
map.put("field1", "1");
map.put("field2", "2");
Config dynamicCfg = new Config.Builder()
.indentionStep(2)
.encodingMode(EncodingMode.DYNAMIC_MODE)
.build();
String output = JsonStream.serialize(dynamicCfg, map);
assertEquals("{\n" +
" \"field1\": \"1\",\n" +
" \"field2\": \"2\"\n" +
"}", output);
Config reflectionCfg = new Config.Builder()
.indentionStep(2)
.encodingMode(EncodingMode.REFLECTION_MODE)
.build();
output = JsonStream.serialize(reflectionCfg, map);
assertEquals("{\n" +
" \"field1\": \"1\",\n" +
" \"field2\": \"2\"\n" +
"}", output);
}
public void test_indention_with_empty_map() {
Config config = JsoniterSpi.getCurrentConfig().copyBuilder()
.indentionStep(2)
.encodingMode(EncodingMode.REFLECTION_MODE)
.build();
assertEquals("{}", JsonStream.serialize(config, new HashMap<String, String>()));
config = JsoniterSpi.getCurrentConfig().copyBuilder()
.indentionStep(2)
.encodingMode(EncodingMode.DYNAMIC_MODE)
.build();
assertEquals("{}", JsonStream.serialize(config, new HashMap<String, String>()));
}
}