class MockSerializer(object): GRAPHSON_PREFIX = "janusgraph" GRAPHSON_BASE_TYPE = "MOCK" GRAPHSON_TYPE = GraphSONUtil.formatType(GRAPHSON_PREFIX, GRAPHSON_BASE_TYPE) serializedJSON = None @classmethod def dictify(cls, obj, writer): """ Args: obj (X): writer: Returns: """ value = obj.objectify() serializedJSON = GraphSONUtil.typedValue(cls.GRAPHSON_BASE_TYPE, value, cls.GRAPHSON_PREFIX) cls.serializedJSON = serializedJSON return serializedJSON def serialize(self, obj): value = obj.objectify() serializedJSON = GraphSONUtil.typedValue(self.GRAPHSON_BASE_TYPE, value, self.GRAPHSON_PREFIX) self.serializedJSON = serializedJSON return self def get_serialized_json(self): return self.serializedJSON
class CircleSerializer(object): """ Serialize a GeoShape Circle object so that the same can be passed to Gremlin Server """ GRAPHSON_PREFIX = "janusgraph" GRAPHSON_BASE_TYPE = "Geoshape" GRAPHSON_TYPE = GraphSONUtil.formatType(GRAPHSON_PREFIX, GRAPHSON_BASE_TYPE) @classmethod def dictify(cls, circle, writer): """ This is serializer method for Circle class. Args: circle (Circle): The GeoShape Circle object to serialize writer : The Gremlin GraphSON writer object to used during serializing. Returns: json """ geometryJSON = toGeoJSON(circle).convert() serializedJSON = GraphSONUtil.typedValue(cls.GRAPHSON_BASE_TYPE, geometryJSON, cls.GRAPHSON_PREFIX) return serializedJSON
def register_deserializer(self, typeClass, deserializer): """ This method is used to registering any additional JanusGraph de-serializers. Args: typeClass (str): The identifier to be used with underlaying graph to register the De-serializer against. serializer: The De-serializer class. Returns: """ objectIdentifier = GraphSONUtil.formatType(self.GRAPHSON_PREFIX, typeClass) self.deserializers[objectIdentifier] = deserializer
def dictify(cls, obj, writer): """ Args: obj (X): writer: Returns: """ value = obj.objectify() serializedJSON = GraphSONUtil.typedValue(cls.GRAPHSON_BASE_TYPE, value, cls.GRAPHSON_PREFIX) cls.serializedJSON = serializedJSON return serializedJSON
def dictify(cls, relationID, writer): """ Serializes RelationIdentifier object. Args: relationID (RelationIdentifier): The RelationID to serialize. writer: Returns: json """ relationJSON = cls.__relationID_to_dict(relationID) serializedJSON = GraphSONUtil.typedValue(cls.GRAPHSON_BASE_TYPE, relationJSON, cls.GRAPHSON_PREFIX) return serializedJSON
def dictify(cls, circle, writer): """ This is serializer method for Circle class. Args: circle (Circle): The GeoShape Circle object to serialize writer : The Gremlin GraphSON writer object to used during serializing. Returns: json """ geometryJSON = toGeoJSON(circle).convert() serializedJSON = GraphSONUtil.typedValue(cls.GRAPHSON_BASE_TYPE, geometryJSON, cls.GRAPHSON_PREFIX) return serializedJSON
def dictify(cls, point, writer): """ This is serializer method for Point class. Args: point (Point): The GeoShape Point class to serialize into. writer: Returns: json """ geometryJSON = toGeoJSON(point).convert() serializedJSON = GraphSONUtil.typedValue(cls.GRAPHSON_BASE_TYPE, geometryJSON, cls.GRAPHSON_PREFIX) return serializedJSON
def serialize(self, obj): value = obj.objectify() serializedJSON = GraphSONUtil.typedValue(self.GRAPHSON_BASE_TYPE, value, self.GRAPHSON_PREFIX) self.serializedJSON = serializedJSON return self
class JanusGraphSONReader(object): """ This class registers JanusGraph specific de-serializers so that objects like GeoShape, RelationIdentifier can be interpreted on Python client side. """ GRAPHSON_PREFIX = "janusgraph" GEO_GRAPHSON_BASE_TYPE = "Geoshape" RELATIONID_BASE_TYPE = "RelationIdentifier" GeoShape_GRAPHSON_TYPE = GraphSONUtil.formatType(GRAPHSON_PREFIX, GEO_GRAPHSON_BASE_TYPE) RelationID_GRAPHSON_TYPE = GraphSONUtil.formatType(GRAPHSON_PREFIX, RELATIONID_BASE_TYPE) deserializers = dict() def __init__(self): self.reader = None def __register_default_deserializers(self): """ This method is used to register the Default de-serializers for JanusGraph's python client. Currently the deserializer registers GeoShape and RelationIdentifier classes. Returns: """ janusDeSerializers = self.__build_deserializers() self.deserializers.update(janusDeSerializers) def __build_deserializers(self): """ The actual method which takes care of adding JanusGraph specific de-serializers. Returns: dict """ # Currently the default de-serializers registered. janusDeSerializers = { self.GeoShape_GRAPHSON_TYPE: GeoShapeDeserializer, self.RelationID_GRAPHSON_TYPE: RelationIdentifierDeserializer } return janusDeSerializers def build(self): """ The method registers JanusGraph specific de-serializers into Gremlin GraphSON Reader class. Returns: GraphSONReader """ self.__register_default_deserializers() self.reader = GraphSONReader(self.deserializers) return self.reader def register_deserializer(self, typeClass, deserializer): """ This method is used to registering any additional JanusGraph de-serializers. Args: typeClass (str): The identifier to be used with underlaying graph to register the De-serializer against. serializer: The De-serializer class. Returns: """ objectIdentifier = GraphSONUtil.formatType(self.GRAPHSON_PREFIX, typeClass) self.deserializers[objectIdentifier] = deserializer