org.apache.tinkerpop.gremlin.structure.io.graphson.TraversalSerializersV2d0 Maven / Gradle / Ivy
Show all versions of driver-cql-shaded Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tinkerpop.gremlin.structure.io.graphson;
import org.apache.commons.configuration.ConfigurationConverter;
import org.apache.commons.configuration.MapConfiguration;
import org.apache.tinkerpop.gremlin.process.remote.traversal.DefaultRemoteTraverser;
import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.TextP;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.TraversalStrategyProxy;
import org.apache.tinkerpop.gremlin.process.traversal.util.AndP;
import org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP;
import org.apache.tinkerpop.gremlin.process.traversal.util.OrP;
import org.apache.tinkerpop.gremlin.util.function.Lambda;
import org.apache.tinkerpop.shaded.jackson.core.JsonGenerator;
import org.apache.tinkerpop.shaded.jackson.core.JsonParser;
import org.apache.tinkerpop.shaded.jackson.core.JsonProcessingException;
import org.apache.tinkerpop.shaded.jackson.core.JsonToken;
import org.apache.tinkerpop.shaded.jackson.databind.DeserializationContext;
import org.apache.tinkerpop.shaded.jackson.databind.SerializerProvider;
import org.apache.tinkerpop.shaded.jackson.databind.deser.std.StdDeserializer;
import org.apache.tinkerpop.shaded.jackson.databind.jsontype.TypeSerializer;
import org.apache.tinkerpop.shaded.jackson.databind.ser.std.StdScalarSerializer;
import org.apache.tinkerpop.shaded.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
final class TraversalSerializersV2d0 {
private TraversalSerializersV2d0() {
}
/////////////////
// SERIALIZERS //
////////////////
final static class TraversalJacksonSerializer extends StdSerializer {
public TraversalJacksonSerializer() {
super(Traversal.class);
}
@Override
public void serialize(final Traversal traversal, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeObject(traversal.asAdmin().getBytecode());
}
@Override
public void serializeWithType(final Traversal traversal, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider, final TypeSerializer typeSerializer)
throws IOException {
serialize(traversal, jsonGenerator, serializerProvider);
}
}
final static class BytecodeJacksonSerializer extends StdScalarSerializer {
public BytecodeJacksonSerializer() {
super(Bytecode.class);
}
@Override
public void serialize(final Bytecode bytecode, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
if (bytecode.getSourceInstructions().iterator().hasNext()) {
jsonGenerator.writeArrayFieldStart(GraphSONTokens.SOURCE);
for (final Bytecode.Instruction instruction : bytecode.getSourceInstructions()) {
jsonGenerator.writeStartArray();
jsonGenerator.writeString(instruction.getOperator());
for (final Object argument : instruction.getArguments()) {
jsonGenerator.writeObject(argument);
}
jsonGenerator.writeEndArray();
}
jsonGenerator.writeEndArray();
}
if (bytecode.getStepInstructions().iterator().hasNext()) {
jsonGenerator.writeArrayFieldStart(GraphSONTokens.STEP);
for (final Bytecode.Instruction instruction : bytecode.getStepInstructions()) {
jsonGenerator.writeStartArray();
jsonGenerator.writeString(instruction.getOperator());
for (final Object argument : instruction.getArguments()) {
jsonGenerator.writeObject(argument);
}
jsonGenerator.writeEndArray();
}
jsonGenerator.writeEndArray();
}
jsonGenerator.writeEndObject();
}
}
static class EnumJacksonSerializer extends StdScalarSerializer {
public EnumJacksonSerializer() {
super(Enum.class);
}
@Override
public void serialize(final Enum enumInstance, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeString(enumInstance.name());
}
}
final static class PJacksonSerializer extends StdScalarSerializer {
public PJacksonSerializer() {
super(P.class);
}
@Override
public void serialize(final P p, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(GraphSONTokens.PREDICATE,
p instanceof ConnectiveP ?
p instanceof AndP ?
GraphSONTokens.AND :
GraphSONTokens.OR :
p.getBiPredicate().toString());
if (p instanceof ConnectiveP) {
jsonGenerator.writeArrayFieldStart(GraphSONTokens.VALUE);
for (final P> predicate : ((ConnectiveP>) p).getPredicates()) {
jsonGenerator.writeObject(predicate);
}
jsonGenerator.writeEndArray();
} else {
if (p.getValue() instanceof Collection) {
jsonGenerator.writeArrayFieldStart(GraphSONTokens.VALUE);
for (final Object object : (Collection) p.getValue()) {
jsonGenerator.writeObject(object);
}
jsonGenerator.writeEndArray();
} else
jsonGenerator.writeObjectField(GraphSONTokens.VALUE, p.getValue());
}
jsonGenerator.writeEndObject();
}
}
final static class LambdaJacksonSerializer extends StdScalarSerializer {
public LambdaJacksonSerializer() {
super(Lambda.class);
}
@Override
public void serialize(final Lambda lambda, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(GraphSONTokens.SCRIPT, lambda.getLambdaScript());
jsonGenerator.writeStringField(GraphSONTokens.LANGUAGE, lambda.getLambdaLanguage());
jsonGenerator.writeNumberField(GraphSONTokens.ARGUMENTS, lambda.getLambdaArguments());
jsonGenerator.writeEndObject();
}
}
final static class BindingJacksonSerializer extends StdScalarSerializer {
public BindingJacksonSerializer() {
super(Bytecode.Binding.class);
}
@Override
public void serialize(final Bytecode.Binding binding, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(GraphSONTokens.KEY, binding.variable());
jsonGenerator.writeObjectField(GraphSONTokens.VALUE, binding.value());
jsonGenerator.writeEndObject();
}
}
final static class TraverserJacksonSerializer extends StdScalarSerializer {
public TraverserJacksonSerializer() {
super(Traverser.class);
}
@Override
public void serialize(final Traverser traverserInstance, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectField(GraphSONTokens.BULK, traverserInstance.bulk());
jsonGenerator.writeObjectField(GraphSONTokens.VALUE, traverserInstance.get());
jsonGenerator.writeEndObject();
}
}
final static class TraversalStrategyJacksonSerializer extends StdScalarSerializer {
public TraversalStrategyJacksonSerializer() {
super(TraversalStrategy.class);
}
@Override
public void serialize(final TraversalStrategy traversalStrategy, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
for (final Map.Entry