Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4585506

Browse files
committed
improved serialize list of user data
1 parent 30d914b commit 4585506

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

‎core/src/main/java/com/arangodb/internal/serde/InternalDeserializers.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public RawJson deserialize(JsonParser p, DeserializationContext ctxt) throws IOE
3434
if (JsonFactory.FORMAT_NAME_JSON.equals(p.getCodec().getFactory().getFormatName())) {
3535
return RawJson.of(new String(SerdeUtils.extractBytes(p), StandardCharsets.UTF_8));
3636
} else {
37+
// TODO: compare perfs using ByteArrayOutputStream and StringWriter
3738
StringWriter w = new StringWriter();
38-
JsonGenerator gen = SerdeUtils.INSTANCE.getJsonMapper().createGenerator(w);
39-
gen.copyCurrentStructure(p);
40-
gen.close();
39+
try (JsonGenerator gen = SerdeUtils.INSTANCE.getJsonMapper().createGenerator(w)) {
40+
gen.copyCurrentStructure(p);
41+
gen.flush();
42+
}
4143
return RawJson.of(w.toString());
4244
}
4345
}

‎core/src/main/java/com/arangodb/internal/serde/InternalSerdeImpl.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.arangodb.util.RawJson;
88
import com.fasterxml.jackson.annotation.JsonInclude;
99
import com.fasterxml.jackson.core.JsonFactory;
10+
import com.fasterxml.jackson.core.JsonGenerator;
1011
import com.fasterxml.jackson.core.JsonParser;
1112
import com.fasterxml.jackson.core.JsonProcessingException;
1213
import com.fasterxml.jackson.core.JsonToken;
@@ -15,12 +16,10 @@
1516
import com.fasterxml.jackson.databind.Module;
1617
import com.fasterxml.jackson.databind.ObjectMapper;
1718

19+
import java.io.ByteArrayOutputStream;
1820
import java.io.IOException;
1921
import java.lang.reflect.Type;
2022
import java.nio.charset.StandardCharsets;
21-
import java.util.List;
22-
import java.util.stream.Collectors;
23-
import java.util.stream.StreamSupport;
2423

2524
import static com.arangodb.internal.serde.SerdeUtils.checkSupportedJacksonVersion;
2625
import static com.arangodb.internal.serde.SerdeUtils.extractBytes;
@@ -150,14 +149,20 @@ public byte[] serializeUserData(Object value) {
150149
}
151150
}
152151

153-
// TODO: review
154152
@Override
155153
public byte[] serializeCollectionUserData(Iterable<?> value) {
156-
List<JsonNode> jsonNodeCollection = StreamSupport.stream(value.spliterator(), false)
157-
.map(this::serializeUserData)
158-
.map(this::parse)
159-
.collect(Collectors.toList());
160-
return serialize(jsonNodeCollection);
154+
ByteArrayOutputStream os = new ByteArrayOutputStream();
155+
try (JsonGenerator gen = mapper.createGenerator(os)) {
156+
gen.writeStartArray();
157+
for (Object o : value) {
158+
gen.writeRawValue(new RawUserDataValue(serializeUserData(o)));
159+
}
160+
gen.writeEndArray();
161+
gen.flush();
162+
} catch (IOException e) {
163+
throw ArangoDBException.of(e);
164+
}
165+
return os.toByteArray();
161166
}
162167

163168
@Override

‎core/src/main/java/com/arangodb/internal/serde/SerdeUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public String writeJson(final JsonNode data) {
101101
public static byte[] extractBytes(JsonParser parser) throws IOException {
102102
JsonToken t = parser.currentToken();
103103
if (t.isStructEnd() || t == JsonToken.FIELD_NAME) {
104-
throw new RuntimeException("Unexpected token: " + t);
104+
throw new ArangoDBException("Unexpected token: " + t);
105105
}
106106
byte[] data = (byte[]) parser.currentTokenLocation().contentReference().getRawContent();
107107
int start = (int) parser.currentTokenLocation().getByteOffset();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /