forked from simdjson/simdjson-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectParsingTest.java
More file actions
97 lines (78 loc) · 2.99 KB
/
ObjectParsingTest.java
File metadata and controls
97 lines (78 loc) · 2.99 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
package org.simdjson;
import org.junit.jupiter.api.Test;
import java.util.Iterator;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.simdjson.JsonValueAssert.assertThat;
import static org.simdjson.TestUtils.toUtf8;
public class ObjectParsingTest {
@Test
public void emptyObject() {
// given
SimdJsonParser parser = new SimdJsonParser();
byte[] json = toUtf8("{}");
// when
JsonValue jsonValue = parser.parse(json, json.length);
// then
assertThat(jsonValue.isObject()).isTrue();
Iterator<JsonValue> it = jsonValue.arrayIterator();
assertThat(it.hasNext()).isFalse();
}
@Test
public void objectIterator() {
// given
SimdJsonParser parser = new SimdJsonParser();
byte[] json = toUtf8("{\"a\": 1, \"b\": 2, \"c\": 3}");
// when
JsonValue jsonValue = parser.parse(json, json.length);
// then
assertThat(jsonValue.isObject()).isTrue();
String[] expectedKeys = new String[]{"a", "b", "c"};
int[] expectedValue = new int[]{1, 2, 3};
int counter = 0;
Iterator<Map.Entry<String, JsonValue>> it = jsonValue.objectIterator();
while (it.hasNext()) {
Map.Entry<String, JsonValue> field = it.next();
assertThat(field.getKey()).isEqualTo(expectedKeys[counter]);
assertThat(field.getValue()).isEqualTo(expectedValue[counter]);
counter++;
}
assertThat(counter).isEqualTo(expectedKeys.length);
}
@Test
public void objectSize() {
// given
SimdJsonParser parser = new SimdJsonParser();
byte[] json = toUtf8("{\"1\": 1, \"2\": 1, \"3\": 1}");
// when
JsonValue jsonValue = parser.parse(json, json.length);
// then
assertThat(jsonValue.isObject()).isTrue();
assertThat(jsonValue.getSize()).isEqualTo(3);
}
@Test
public void fieldNamesWithNonAsciiCharacters() {
// given
SimdJsonParser parser = new SimdJsonParser();
byte[] json = toUtf8("{\"ąćśńźż\": 1, \"\\u20A9\\u0E3F\": 2, \"αβγ\": 3, \"😀abc😀\": 4}");
// when
JsonValue jsonValue = parser.parse(json, json.length);
// then
assertThat(jsonValue.get("ąćśńźż")).isEqualTo(1);
assertThat(jsonValue.get("\u20A9\u0E3F")).isEqualTo(2);
assertThat(jsonValue.get("αβγ")).isEqualTo(3);
assertThat(jsonValue.get("😀abc😀")).isEqualTo(4);
}
@Test
public void nonexistentField() {
// given
SimdJsonParser parser = new SimdJsonParser();
byte[] json = toUtf8("{\"ąćśńźż\": 1, \"\\u20A9\\u0E3F\": 2, \"αβγ\": 3}");
// when
JsonValue jsonValue = parser.parse(json, json.length);
// then
assertThat(jsonValue.get("acsnz")).isNull();
assertThat(jsonValue.get("\\u20A9\\u0E3F")).isNull();
assertThat(jsonValue.get("αβ")).isNull();
}
}