in gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGraphSONSerializerV1.java [121:171]
private void writeEdges(final DirectionalStarGraph directionalStarGraph, final JsonGenerator jsonGenerator,
final SerializerProvider serializerProvider,
final TypeSerializer typeSerializer,
final Direction direction) throws IOException, JsonProcessingException {
// only write edges if there are some AND if the user requested them to be serialized AND if they match
// the direction being serialized by the format
final StarGraph starGraph = directionalStarGraph.getStarGraphToSerialize();
final Direction edgeDirectionToSerialize = directionalStarGraph.getDirection();
final Map<String, List<Edge>> starEdges = direction.equals(Direction.OUT) ? starGraph.starVertex.outEdges : starGraph.starVertex.inEdges;
final boolean writeEdges = null != starEdges && edgeDirectionToSerialize != null
&& (edgeDirectionToSerialize == direction || edgeDirectionToSerialize == Direction.BOTH);
if (writeEdges) {
jsonGenerator.writeObjectFieldStart(direction == Direction.IN ? GraphSONTokens.IN_E : GraphSONTokens.OUT_E);
if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName());
final Set<String> keys = normalize ? new TreeSet<>(starEdges.keySet()) : starEdges.keySet();
for (final String k : keys) {
final List<Edge> edges = starEdges.get(k);
jsonGenerator.writeArrayFieldStart(k);
if (typeSerializer != null) {
jsonGenerator.writeString(ArrayList.class.getName());
jsonGenerator.writeStartArray();
}
final List<Edge> edgesToWrite = normalize ? sort(edges, Comparators.EDGE_COMPARATOR) : edges;
for (final Edge edge : edgesToWrite) {
jsonGenerator.writeStartObject();
if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName());
GraphSONUtil.writeWithType(GraphSONTokens.ID, edge.id(), jsonGenerator, serializerProvider, typeSerializer);
GraphSONUtil.writeWithType(direction.equals(Direction.OUT) ? GraphSONTokens.IN : GraphSONTokens.OUT,
direction.equals(Direction.OUT) ? edge.inVertex().id() : edge.outVertex().id(),
jsonGenerator, serializerProvider, typeSerializer);
final Iterator<Property<Object>> edgeProperties = normalize ?
IteratorUtils.list(edge.properties(), Comparators.PROPERTY_COMPARATOR).iterator() : edge.properties();
if (edgeProperties.hasNext()) {
jsonGenerator.writeObjectFieldStart(GraphSONTokens.PROPERTIES);
if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName());
while (edgeProperties.hasNext()) {
final Property<Object> meta = edgeProperties.next();
GraphSONUtil.writeWithType(meta.key(), meta.value(), jsonGenerator, serializerProvider, typeSerializer);
}
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndArray();
if (typeSerializer != null) jsonGenerator.writeEndArray();
}
jsonGenerator.writeEndObject();
}
}