private void addCustomClasses(final Map<String, Object> config, final GryoMapper.Builder builder) { final List<String> classNameList = getListStringFromConfig(TOKEN_CUSTOM, config); classNameList.stream().forEach(serializerDefinition -> { String className; Optional<String> serializerName; if (serializerDefinition.contains(";")) { final String[] split = serializerDefinition.split(";"); if (split.length != 2) throw new IllegalStateException(String.format("Invalid format for serializer definition [%s] - expected <class>;<serializer-class>", serializerDefinition)); className = split[0]; serializerName = Optional.of(split[1]); } else { serializerName = Optional.empty(); className = serializerDefinition; } try { final Class clazz = Class.forName(className); final Serializer serializer; if (serializerName.isPresent()) { final Class serializerClazz = Class.forName(serializerName.get()); serializer = (Serializer) serializerClazz.newInstance(); builder.addCustom(clazz, kryo -> serializer); } else builder.addCustom(clazz); } catch (Exception ex) { throw new IllegalStateException("Class could not be found", ex); } }); }
private void addCustomClasses(final Map<String, Object> config, final GryoMapper.Builder builder) { final List<String> classNameList = getListStringFromConfig(TOKEN_CUSTOM, config); classNameList.stream().forEach(serializerDefinition -> { String className; Optional<String> serializerName; if (serializerDefinition.contains(";")) { final String[] split = serializerDefinition.split(";"); if (split.length != 2) throw new IllegalStateException(String.format("Invalid format for serializer definition [%s] - expected <class>;<serializer-class>", serializerDefinition)); className = split[0]; serializerName = Optional.of(split[1]); } else { serializerName = Optional.empty(); className = serializerDefinition; } try { final Class clazz = Class.forName(className); final Serializer serializer; if (serializerName.isPresent()) { final Class serializerClazz = Class.forName(serializerName.get()); serializer = (Serializer) serializerClazz.newInstance(); builder.addCustom(clazz, kryo -> serializer); } else builder.addCustom(clazz); } catch (Exception ex) { throw new IllegalStateException("Class could not be found", ex); } }); }
/** * Creates a {@code GryoMapper}. */ public GryoMapper create() { // consult the registry if provided and inject registry entries as custom classes. registries.forEach(registry -> { final List<Pair<Class, Object>> serializers = registry.find(GryoIo.class); serializers.forEach(p -> { if (null == p.getValue1()) addCustom(p.getValue0()); else if (p.getValue1() instanceof SerializerShim) addCustom(p.getValue0(), new ShadedSerializerAdapter((SerializerShim) p.getValue1())); else if (p.getValue1() instanceof Serializer) addCustom(p.getValue0(), (Serializer) p.getValue1()); else if (p.getValue1() instanceof Function) addCustom(p.getValue0(), (Function<Kryo, Serializer>) p.getValue1()); else throw new IllegalStateException(String.format( "Unexpected value provided by %s for serializable class %s - expected a parameter in [null, %s implementation or Function<%s, %s>], but received %s", registry.getClass().getSimpleName(), p.getValue0().getClass().getCanonicalName(), Serializer.class.getName(), Kryo.class.getSimpleName(), Serializer.class.getSimpleName(), p.getValue1())); }); }); return new GryoMapper(this); }
private void addCustomClasses(final Map<String, Object> config, final GryoMapper.Builder builder) { final List<String> classNameList = getListStringFromConfig(TOKEN_CUSTOM, config); classNameList.stream().forEach(serializerDefinition -> { String className; Optional<String> serializerName; if (serializerDefinition.contains(";")) { final String[] split = serializerDefinition.split(";"); if (split.length != 2) throw new IllegalStateException(String.format("Invalid format for serializer definition [%s] - expected <class>;<serializer-class>", serializerDefinition)); className = split[0]; serializerName = Optional.of(split[1]); } else { serializerName = Optional.empty(); className = serializerDefinition; } try { final Class clazz = Class.forName(className); final Serializer serializer; if (serializerName.isPresent()) { final Class serializerClazz = Class.forName(serializerName.get()); serializer = (Serializer) serializerClazz.newInstance(); builder.addCustom(clazz, kryo -> serializer); } else builder.addCustom(clazz); } catch (Exception ex) { throw new IllegalStateException("Class could not be found", ex); } }); }
private void addCustomClasses(final Map<String, Object> config, final GryoMapper.Builder builder) { final List<String> classNameList = getListStringFromConfig(TOKEN_CUSTOM, config); classNameList.stream().forEach(serializerDefinition -> { String className; Optional<String> serializerName; if (serializerDefinition.contains(";")) { final String[] split = serializerDefinition.split(";"); if (split.length != 2) throw new IllegalStateException(String.format("Invalid format for serializer definition [%s] - expected <class>;<serializer-class>", serializerDefinition)); className = split[0]; serializerName = Optional.of(split[1]); } else { serializerName = Optional.empty(); className = serializerDefinition; } try { final Class clazz = Class.forName(className); final Serializer serializer; if (serializerName.isPresent()) { final Class serializerClazz = Class.forName(serializerName.get()); serializer = (Serializer) serializerClazz.newInstance(); builder.addCustom(clazz, kryo -> serializer); } else builder.addCustom(clazz); } catch (Exception ex) { throw new IllegalStateException("Class could not be found", ex); } }); }
/** * Creates a {@code GryoMapper}. */ public GryoMapper create() { // consult the registry if provided and inject registry entries as custom classes. registries.forEach(registry -> { final List<Pair<Class, Object>> serializers = registry.find(GryoIo.class); serializers.forEach(p -> { if (null == p.getValue1()) addCustom(p.getValue0()); else if (p.getValue1() instanceof SerializerShim) addCustom(p.getValue0(), new ShadedSerializerAdapter((SerializerShim) p.getValue1())); else if (p.getValue1() instanceof Serializer) addCustom(p.getValue0(), (Serializer) p.getValue1()); else if (p.getValue1() instanceof Function) addCustom(p.getValue0(), (Function<Kryo, Serializer>) p.getValue1()); else throw new IllegalStateException(String.format( "Unexpected value provided by %s for serializable class %s - expected a parameter in [null, %s implementation or Function<%s, %s>], but received %s", registry.getClass().getSimpleName(), p.getValue0().getClass().getCanonicalName(), Serializer.class.getName(), Kryo.class.getSimpleName(), Serializer.class.getSimpleName(), p.getValue1())); }); }); return new GryoMapper(this); }
@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { final SimpleModule moduleV1d0 = new SimpleModule(); moduleV1d0.addSerializer(CustomId.class, new CustomId.CustomIdJacksonSerializerV1d0()); final SimpleModule moduleV2d0 = new CustomId.CustomIdTinkerPopJacksonModuleV2d0(); final SimpleModule modulev3d0 = new CustomId.CustomIdTinkerPopJacksonModuleV3d0(); return Arrays.asList(new Object[][]{ {"graphson-v1-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().addCustomModule(moduleV1d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().addCustomModule(moduleV1d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v2-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().addCustomModule(moduleV2d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().addCustomModule(moduleV2d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v3", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create()}, {"gryo-v1", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create()}, {"gryo-v3", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create()} }); }
private static GryoMapper.Builder overrideWithLite(final GryoMapper.Builder builder) { // override the core graph Elements so as to serialize with "reference" as opposed to "detached" builder.addCustom(Edge.class, new EdgeLiteSerializer()); builder.addCustom(Vertex.class, new VertexLiteSerializer()); builder.addCustom(VertexProperty.class, new VertexPropertyLiteSerializer()); builder.addCustom(Property.class, new PropertyLiteSerializer()); builder.addCustom(Path.class, new PathLiteSerializer()); return builder; }
@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { final SimpleModule moduleV1d0 = new SimpleModule(); moduleV1d0.addSerializer(CustomId.class, new CustomId.CustomIdJacksonSerializerV1d0()); final SimpleModule moduleV2d0 = new CustomId.CustomIdTinkerPopJacksonModuleV2d0(); final SimpleModule modulev3d0 = new CustomId.CustomIdTinkerPopJacksonModuleV3d0(); return Arrays.asList(new Object[][]{ {"graphson-v1-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().addCustomModule(moduleV1d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().addCustomModule(moduleV1d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v2-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().addCustomModule(moduleV2d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().addCustomModule(moduleV2d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v3", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create()}, {"gryo-v1", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create()}, {"gryo-v3", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create()} }); }
private static GryoMapper.Builder overrideWithLite(final GryoMapper.Builder builder) { // override the core graph Elements so as to serialize with "reference" as opposed to "detached" builder.addCustom(Edge.class, new EdgeLiteSerializer()); builder.addCustom(Vertex.class, new VertexLiteSerializer()); builder.addCustom(VertexProperty.class, new VertexPropertyLiteSerializer()); builder.addCustom(Property.class, new PropertyLiteSerializer()); builder.addCustom(Path.class, new PathLiteSerializer()); return builder; }