Skip to content

Commit 0ecfa4e

Browse files
author
Mark
committed
fixed importDocuments, insertDocuments to work with raw Jsons (#91)
1 parent 407a70a commit 0ecfa4e

File tree

5 files changed

+87
-14
lines changed

5 files changed

+87
-14
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
v4.1.7 (2017-01-26)
2+
---------------------------
3+
* fixed importDocuments, insertDocuments to work with raw Jsons (issue #91)
4+
15
v4.1.6 (2017-01-18)
26
---------------------------
37
* added serializer support for enclosing types

src/main/java/com/arangodb/internal/InternalArangoCollection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected <T> Request insertDocumentsRequest(final Collection<T> values, final D
119119
executor.createPath(ArangoDBConstants.PATH_API_DOCUMENT, name));
120120
request.putQueryParam(ArangoDBConstants.WAIT_FOR_SYNC, params.getWaitForSync());
121121
request.putQueryParam(ArangoDBConstants.RETURN_NEW, params.getReturnNew());
122-
request.setBody(executor.serialize(values));
122+
request.setBody(util().serialize(values, false, true));
123123
return request;
124124
}
125125

@@ -168,7 +168,7 @@ protected Request importDocumentsRequest(final String values, final DocumentImpo
168168

169169
protected Request importDocumentsRequest(final Collection<?> values, final DocumentImportOptions options) {
170170
return importDocumentsRequest(options).putQueryParam(ArangoDBConstants.TYPE, ImportType.list)
171-
.setBody(executor.serialize(values));
171+
.setBody(util().serialize(values, false, true));
172172
}
173173

174174
protected Request importDocumentsRequest(final DocumentImportOptions options) {

src/main/java/com/arangodb/util/ArangoUtil.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.arangodb.util;
2222

2323
import java.lang.reflect.Type;
24+
import java.util.Iterator;
2425
import java.util.Map;
2526

2627
import com.arangodb.ArangoDBException;
@@ -81,17 +82,7 @@ public <T> T deserialize(final VPackSlice vpack, final Type type) throws ArangoD
8182
* @throws ArangoDBException
8283
*/
8384
public VPackSlice serialize(final Object entity) throws ArangoDBException {
84-
try {
85-
final VPackSlice vpack;
86-
if (String.class.isAssignableFrom(entity.getClass())) {
87-
vpack = vpackParser.fromJson((String) entity);
88-
} else {
89-
vpack = vpacker.serialize(entity);
90-
}
91-
return vpack;
92-
} catch (final VPackException e) {
93-
throw new ArangoDBException(e);
94-
}
85+
return serialize(entity, false);
9586
}
9687

9788
/**
@@ -105,10 +96,37 @@ public VPackSlice serialize(final Object entity) throws ArangoDBException {
10596
* @throws ArangoDBException
10697
*/
10798
public VPackSlice serialize(final Object entity, final boolean serializeNullValues) throws ArangoDBException {
99+
return serialize(entity, serializeNullValues, false);
100+
}
101+
102+
/**
103+
* Serialize a given Object to VelocyPack. If the Object is from type Iterable<String> the String will be
104+
* interpreted as Json
105+
*
106+
* @param entity
107+
* The Object to serialize. If it is from type String, it will be handled as a Json.
108+
* @param serializeNullValues
109+
* Whether or not null values should be excluded from serialization.
110+
* @param stringAsJson
111+
* @return the serialized VelocyPack
112+
* @throws ArangoDBException
113+
*/
114+
@SuppressWarnings("unchecked")
115+
public VPackSlice serialize(final Object entity, final boolean serializeNullValues, final boolean stringAsJson)
116+
throws ArangoDBException {
108117
try {
109118
final VPackSlice vpack;
110-
if (String.class.isAssignableFrom(entity.getClass())) {
119+
final Class<? extends Object> type = entity.getClass();
120+
if (String.class.isAssignableFrom(type)) {
111121
vpack = vpackParser.fromJson((String) entity, serializeNullValues);
122+
} else if (stringAsJson && Iterable.class.isAssignableFrom(type)) {
123+
final Iterator<?> iterator = Iterable.class.cast(entity).iterator();
124+
if (iterator.hasNext() && String.class.isAssignableFrom(iterator.next().getClass())) {
125+
vpack = vpackParser.fromJson((Iterable<String>) entity, serializeNullValues);
126+
} else {
127+
final VPack vp = serializeNullValues ? vpackerNull : vpacker;
128+
vpack = vp.serialize(entity);
129+
}
112130
} else {
113131
final VPack vp = serializeNullValues ? vpackerNull : vpacker;
114132
vpack = vp.serialize(entity);

src/main/java/com/arangodb/velocypack/VPackParser.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ public VPackSlice fromJson(final String json, final boolean includeNullValues) t
223223
return builder.slice();
224224
}
225225

226+
public VPackSlice fromJson(final Iterable<String> jsons) throws VPackException {
227+
return fromJson(jsons, false);
228+
}
229+
230+
public VPackSlice fromJson(final Iterable<String> jsons, final boolean includeNullValues) throws VPackException {
231+
final VPackBuilder builder = new VPackBuilder();
232+
final JSONParser parser = new JSONParser();
233+
final ContentHandler contentHandler = new VPackContentHandler(builder, includeNullValues, this);
234+
try {
235+
builder.add(ValueType.ARRAY);
236+
for (final String json : jsons) {
237+
parser.parse(json, contentHandler);
238+
}
239+
builder.close();
240+
} catch (final ParseException e) {
241+
throw new VPackBuilderException(e);
242+
}
243+
return builder.slice();
244+
}
245+
226246
private static class VPackContentHandler implements ContentHandler {
227247

228248
private final VPackBuilder builder;

src/test/java/com/arangodb/ArangoCollectionTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,21 @@ public void insertDocuments() {
889889
assertThat(docs.getErrors().size(), is(0));
890890
}
891891

892+
@Test
893+
public void insertDocumentsJson() {
894+
final Collection<String> values = new ArrayList<String>();
895+
values.add("{}");
896+
values.add("{}");
897+
values.add("{}");
898+
final MultiDocumentEntity<DocumentCreateEntity<String>> docs = db.collection(COLLECTION_NAME)
899+
.insertDocuments(values);
900+
assertThat(docs, is(notNullValue()));
901+
assertThat(docs.getDocuments(), is(notNullValue()));
902+
assertThat(docs.getDocuments().size(), is(3));
903+
assertThat(docs.getErrors(), is(notNullValue()));
904+
assertThat(docs.getErrors().size(), is(0));
905+
}
906+
892907
@Test
893908
public void insertDocumentsOne() {
894909
final Collection<BaseDocument> values = new ArrayList<BaseDocument>();
@@ -968,6 +983,22 @@ public void importDocuments() {
968983
assertThat(docs.getDetails(), is(empty()));
969984
}
970985

986+
@Test
987+
public void importDocumentsJsonList() {
988+
final Collection<String> values = new ArrayList<String>();
989+
values.add("{}");
990+
values.add("{}");
991+
values.add("{}");
992+
final DocumentImportEntity docs = db.collection(COLLECTION_NAME).importDocuments(values);
993+
assertThat(docs, is(notNullValue()));
994+
assertThat(docs.getCreated(), is(values.size()));
995+
assertThat(docs.getEmpty(), is(0));
996+
assertThat(docs.getErrors(), is(0));
997+
assertThat(docs.getIgnored(), is(0));
998+
assertThat(docs.getUpdated(), is(0));
999+
assertThat(docs.getDetails(), is(empty()));
1000+
}
1001+
9711002
@Test
9721003
public void importDocumentsDuplicateDefaultError() {
9731004
final Collection<BaseDocument> values = new ArrayList<BaseDocument>();

0 commit comments

Comments
 (0)