8000 vpack: vpack-json parser · anderick/arangodb-java-driver@6a9680e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a9680e

Browse files
author
Mark
committed
vpack: vpack-json parser
1 parent f96afdf commit 6a9680e

File tree

3 files changed

+232
-1
lines changed

3 files changed

+232
-1
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.arangodb.velocypack;
2+
3+
import java.text.DateFormat;
4+
import java.util.Locale;
5+
6+
/**
7+
* @author Mark - mark@arangodb.com
8+
*
9+
*/
10+
public class VPackParser {
11+
12+
private static final String OBJECT_OPEN = "{";
13+
private static final String OBJECT_CLOSE = "}";
14+
private static final String ARRAY_OPEN = "[";
15+
private static final String ARRAY_CLOSE = "]";
16+
private static final String STRING = "\"";
17+
private static final String FIELD = ":";
18+
private static final String SEPERATOR = ",";
19+
private static final DateFormat DATEFORMAT = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT,
20+
Locale.US);
21+
22+
public static String toJson(final VPackSlice vpack) {
23+
final StringBuilder json = new StringBuilder();
24+
parse(null, vpack, json);
25+
return json.toString();
26+
}
27+
28+
private static void parse(final VPackSlice attribute, final VPackSlice value, final StringBuilder json) {
29+
if (attribute != null && attribute.isString()) {
30+
appendField(attribute, json);
31+
}
32+
if (value.isObject()) {
33+
parseObject(value, json);
34+
} else if (value.isArray()) {
35+
parseArray(value, json);
36+
} else if (value.isBoolean()) {
37+
json.append(value.getAsBoolean());
38+
} else if (value.isString()) {
39+
json.append(STRING);
40+
json.append(value.getAsString());
41+
json.append(STRING);
42+
} else if (value.isNumber()) {
43+
json.append(value.getAsNumber());
44+
} else if (value.isDate()) {
45+
json.append(STRING);
46+
json.append(DATEFORMAT.format(value.getAsDate()));
47+
json.append(STRING);
48+
}
49+
}
50+
51+
private static void appendField(final VPackSlice attribute, final StringBuilder json) {
52+
json.append(STRING);
53+
json.append(attribute.getAsString());
54+
json.append(STRING);
55+
json.append(FIELD);
56+
}
57+
58+
private static void parseObject(final VPackSlice value, final StringBuilder json) {
59+
json.append(OBJECT_OPEN);
60+
for (int i = 0; i < value.getLength(); i++) {
61+
if (i > 0) {
62+
json.append(SEPERATOR);
63+
}
64+
parse(value.keyAt(i), value.valueAt(i), json);
65+
}
66+
json.append(OBJECT_CLOSE);
67+
}
68+
69+
private static void parseArray(final VPackSlice value, final StringBuilder json) {
70+
json.append(ARRAY_OPEN);
71+
for (int i = 0; i < value.getLength(); i++) {
72+
if (i > 0) {
73+
json.append(SEPERATOR);
74+
}
75+
parse(null, value.at(i), json);
76+
}
77+
json.append(ARRAY_CLOSE);
78+
}
79+
80+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.arangodb.velocypack;
2+
3+
import java.util.Date;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import com.arangodb.velocypack.exception.VPackException;
9+
import com.arangodb.velocypack.util.Value;
10+
import com.arangodb.velocypack.util.ValueType;
11+
12+
/**
13+
* @author Mark - mark@arangodb.com
14+
*
15+
*/
16+
public class VPackParserTest {
17+
18+
@Test
19+
public void object1Field() throws VPackException {
20+
final VPackBuilder builder = new VPackBuilder();
21+
builder.add(new Value(ValueType.OBJECT));
22+
builder.add("a", new Value("test"));
23+
builder.close();
24+
final String json = VPackParser.toJson(builder.slice());
25+
Assert.assertEquals("{\"a\":\"test\"}", json);
26+
}
27+
28+
@Test
29+
public void object2Fields() throws VPackException {
30+
final VPackBuilder builder = new VPackBuilder();
31+
builder.add(new Value(ValueType.OBJECT));
32+
builder.add("a", new Value("test"));
33+
builder.add("b", new Value(true));
34+
builder.close();
35+
final String json = VPackParser.toJson(builder.slice());
36+
Assert.assertEquals("{\"a\":\"test\",\"b\":true}", json);
37+
}
38+
39+
@Test
40+
public void objectStringField() throws VPackException {
41+
final VPackBuilder builder = new VPackBuilder();
42+
builder.add(new Value(ValueType.OBJECT));
43+
builder.add("a", new Value("test"));
44+
builder.add("b", new Value("test"));
45+
builder.close();
46+
final String json = VPackParser.toJson(builder.slice());
47+
Assert.assertEquals("{\"a\":\"test\",\"b\":\"test\"}", json);
48+
}
49+
50+
@Test
51+
public void objectBooleanField() throws VPackException {
52+
final VPackBuilder builder = new VPackBuilder();
53+
builder.add(new Value(ValueType.OBJECT));
54+
builder.add("a", new Value(true));
55+
builder.add("b", new Value(false));
56+
builder.close();
57+
final String json = VPackParser.toJson(builder.slice());
58+
Assert.assertEquals("{\"a\":true,\"b\":false}", json);
59+
}
60+
61+
@Test
62+
public void objectNumberField() throws VPackException {
63+
final VPackBuilder builder = new VPackBuilder();
64+
builder.add(new Value(ValueType.OBJECT));
65+
builder.add("a", new Value(5));
66+
builder.add("b", new Value(5.5));
67+
builder.close();
68+
final String json = VPackParser.toJson(builder.slice());
69+
Assert.assertEquals("{\"a\":5,\"b\":5.5}", json);
70+
}
71+
72+
@Test
73+
public void objectDateField() throws VPackException {
74+
final VPackBuilder builder = new VPackBuilder();
75+
builder.add(new Value(ValueType.OBJECT));
76+
builder.add("a", new Value(new Date(946681200000L)));// 2000-01-01
77+
builder.close();
78+
final String json = VPackParser.toJson(builder.slice());
79+
Assert.assertEquals("{\"a\":\"Jan 1, 2000 12:00:00 AM\"}", json);
80+
}
81+
82+
@Test
83+
public void arrayInObject() throws VPackException {
84+
final VPackBuilder builder = new VPackBuilder();
85+
builder.add(new Value(ValueType.OBJECT));
86+
builder.add("a", new Value(ValueType.ARRAY));
87+
builder.add(new Value(1));
88+
builder.add(new Value(2));
89+
builder.add(new Value(3));
90+
builder.close();
91+
builder.add("b", new Value(ValueType.ARRAY));
92+
builder.add(new Value("a"));
93+
builder.add(new Value("b"));
94+
builder.add(new Value("c"));
95+
builder.close();
96+
builder.close();
97+
final String json = VPackParser.toJson(builder.slice());
98+
Assert.assertEquals("{\"a\":[1,2,3],\"b\":[\"a\",\"b\",\"c\"]}", json);
99+
}
100+
101+
@Test
102+
public void objectInObject() throws VPackException {
103+
final VPackBuilder builder = new VPackBuilder();
104+
builder.add(new Value(ValueType.OBJECT));
105+
builder.add("a", new Value(ValueType.OBJECT));
106+
builder.add("aa", new Value("test"));
107+
builder.add("ab", new Value(true));
108+
builder.close();
109+
builder.add("b", new Value(ValueType.OBJECT));
110+
builder.add("ba", new Value("test"));
111+
builder.add("bb", new Value(5.5));
112+
builder.close();
113+
builder.close();
114+
final String json = VPackParser.toJson(builder.slice());
115+
Assert.assertEquals("{\"a\":{\"aa\":\"test\",\"ab\":true},\"b\":{\"ba\":\"test\",\"bb\":5.5}}", json);
116+
}
117+
118+
@Test
119+
public void objectInArray() throws VPackException {
120+
final VPackBuilder builder = new VPackBuilder();
121+
builder.add(new Value(ValueType.ARRAY));
122+
builder.add(new Value(ValueType.OBJECT));
123+
builder.add("a", new Value("test"));
124+
builder.close();
125+
builder.add(new Value(ValueType.OBJECT));
126+
builder.add("a", new Value("test"));
127+
builder.close();
128+
builder.close();
129+
final String json = VPackParser.toJson(builder.slice());
130+
Assert.assertEquals("[{\"a\":\"test\"},{\"a\":\"test\"}]", json);
131+
}
132+
133+
@Test
134+
public void arrayInArray() throws VPackException {
135+
final VPackBuilder builder = new VPackBuilder();
136+
builder.add(new Value(ValueType.ARRAY));
137+
builder.add(new Value(ValueType.ARRAY));
138+
builder.add(new Value(1));
139+
builder.add(new Value(2));
140+
builder.add(new Value(3));
141+
builder.close();
142+
builder.add(new Value(ValueType.ARRAY));
143+
builder.add(new Value("a"));
144+
builder.add(new Value("b"));
145+
builder.add(new Value("c"));
146+
builder.close();
147+
builder.close();
148+
final String json = VPackParser.toJson(builder.slice());
149+
Assert.assertEquals("[[1,2,3],[\"a\",\"b\",\"c\"]]", json);
150+
}
151+
}

src/test/java/com/arangodb/velocypack/VPackTestSuite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
@RunWith(Suite.class)
1515
@SuiteClasses({ VPackSerializeDeserializeTest.class, VPackBuilderTest.class, VPackSliceTest.class, ValueTest.class,
16-
SliceIteratorTest.class })
16+
SliceIteratorTest.class, VPackParserTest.class })
1717
public class VPackTestSuite {
1818

1919
}

0 commit comments

Comments
 (0)
0