com.datastax.driver.dse.graph.GraphSON2DseGraphDriverModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dse-java-driver-core Show documentation
Show all versions of dse-java-driver-core Show documentation
A driver for DataStax Enterprise (DSE)
and Apache Cassandra 1.2+ clusters that works exclusively with the
Cassandra Query Language version 3 (CQL3) and Cassandra's binary protocol,
supporting DSE-specific features such as geospatial types, DSE Graph and DSE authentication.
/*
* Copyright (C) 2012-2017 DataStax Inc.
*
* This software can be used solely with DataStax Enterprise. Please consult the license at
* http://www.datastax.com/terms/datastax-dse-driver-license-terms
*/
package com.datastax.driver.dse.graph;
import com.datastax.driver.dse.geometry.Distance;
import com.datastax.driver.dse.geometry.Geometry;
import com.datastax.driver.dse.geometry.LineString;
import com.datastax.driver.dse.geometry.Point;
import com.datastax.driver.dse.geometry.Polygon;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* A Jackson Module to use for TinkerPop serialization/deserialization. It extends
* {@link com.datastax.driver.dse.graph.GraphSON2JacksonModule} because of the specific typing format used in GraphSON.
*/
class GraphSON2DseGraphDriverModule extends GraphSON2JacksonModule {
GraphSON2DseGraphDriverModule() {
super("graph-graphson2dsegraph");
addSerializer(LineString.class, new LineStringGeometrySerializer());
addSerializer(Distance.class, new DistanceGeometrySerializer());
addSerializer(Point.class, new PointGeometrySerializer());
addSerializer(Polygon.class, new PolygonGeometrySerializer());
addDeserializer(LineString.class, new LineStringGeometryDeserializer());
addDeserializer(Point.class, new PointGeometryDeserializer());
addDeserializer(Polygon.class, new PolygonGeometryDeserializer());
addDeserializer(Distance.class, new DistanceGeometryDeserializer());
}
@Override
public Map, String> getTypeDefinitions() {
Map, String> definitions = new HashMap, String>();
definitions.put(LineString.class, "LineString");
definitions.put(Point.class, "Point");
definitions.put(Polygon.class, "Polygon");
definitions.put(Distance.class, "Distance");
definitions.put(byte[].class, "Blob");
return definitions;
}
@Override
public String getTypeNamespace() {
return "dse";
}
abstract static class AbstractGeometryJacksonDeserializer extends StdDeserializer {
protected AbstractGeometryJacksonDeserializer(final Class clazz) {
super(clazz);
}
protected abstract T parse(final String val);
@Override
public T deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
return parse(jsonParser.getText());
}
}
abstract static class AbstractGeometryJacksonSerializer extends StdScalarSerializer {
protected AbstractGeometryJacksonSerializer(final Class clazz) {
super(clazz);
}
@Override
public void serialize(final T value, final JsonGenerator gen,
final SerializerProvider serializerProvider) throws IOException {
gen.writeString(value.asWellKnownText());
}
}
static final class LineStringGeometrySerializer extends AbstractGeometryJacksonSerializer {
LineStringGeometrySerializer() {
super(LineString.class);
}
}
static final class LineStringGeometryDeserializer extends AbstractGeometryJacksonDeserializer {
LineStringGeometryDeserializer() {
super(LineString.class);
}
@Override
public LineString parse(final String val) {
return LineString.fromWellKnownText(val);
}
}
static final class PolygonGeometrySerializer extends AbstractGeometryJacksonSerializer {
PolygonGeometrySerializer() {
super(Polygon.class);
}
}
static final class PolygonGeometryDeserializer extends AbstractGeometryJacksonDeserializer {
PolygonGeometryDeserializer() {
super(Polygon.class);
}
@Override
public Polygon parse(final String val) {
return Polygon.fromWellKnownText(val);
}
}
static final class PointGeometrySerializer extends AbstractGeometryJacksonSerializer {
PointGeometrySerializer() {
super(Point.class);
}
}
static final class PointGeometryDeserializer extends AbstractGeometryJacksonDeserializer {
PointGeometryDeserializer() {
super(Point.class);
}
@Override
public Point parse(final String val) {
return Point.fromWellKnownText(val);
}
}
static final class DistanceGeometrySerializer extends AbstractGeometryJacksonSerializer {
DistanceGeometrySerializer() {
super(Distance.class);
}
}
static final class DistanceGeometryDeserializer extends AbstractGeometryJacksonDeserializer {
DistanceGeometryDeserializer() {
super(Distance.class);
}
@Override
public Distance parse(final String val) {
return Distance.fromWellKnownText(val);
}
}
}