[
https://issues.apache.org/jira/browse/TINKERPOP3-581?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14490327#comment-14490327
]
stephen mallette commented on TINKERPOP3-581:
---------------------------------------------
[~pietermartin] I think i'm more or less done with this issue. i've done the
work in this branch:
https://github.com/apache/incubator-tinkerpop/tree/TINKERPOP3-581
I plan to merge it back to master early next week so I can close out this
issue. This issue ended up being a bit of a beast and touched the test suite
in many ways, up-ended TinkerGraph a bit, etc. If you have some time to check
it out in next few days, please feel free to leave your thoughts on the changes.
> GraphSONWriter and CustomId issue
> ---------------------------------
>
> Key: TINKERPOP3-581
> URL: https://issues.apache.org/jira/browse/TINKERPOP3-581
> Project: TinkerPop 3
> Issue Type: Bug
> Components: test-suite
> Reporter: pietermartin
> Assignee: stephen mallette
> Priority: Critical
> Fix For: 3.0.0.GA
>
>
> Hi,
> I have had to change sqlg to use a custom id field but alas am struggling to
> get the graphson IoTests to pass.
> Sqlg's id now is in json
> {code}
> {"id": {"schema":"public", "table":"Person","id"123}}
> {code}
> I have overriden the folowing Graph.Io methods.
> {code}
> @Override
> public GraphSONMapper.Builder graphSONMapper() {
> final SimpleModule module = new SimpleModule();
> module.addSerializer(RecordId.class, new
> RecordId.RecordIdJacksonSerializer());
> module.addDeserializer(RecordId.class, new
> RecordId.CustomIdJacksonDeserializer());
> //return GraphSONMapper.build().addCustomModule(module);
> return
> GraphSONMapper.build().addCustomModule(module).embedTypes(true);
> }
> {code}
> and in RecordId
> {code}
> static class RecordIdJacksonSerializer extends StdSerializer<RecordId> {
> public RecordIdJacksonSerializer() {
> super(RecordId.class);
> }
> @Override
> public void serialize(final RecordId customId, final JsonGenerator
> jsonGenerator, final SerializerProvider serializerProvider)
> throws IOException {
> ser(customId, jsonGenerator, false);
> }
> @Override
> public void serializeWithType(final RecordId customId, final
> JsonGenerator jsonGenerator,
> final SerializerProvider
> serializerProvider, final TypeSerializer typeSerializer) throws IOException {
> ser(customId, jsonGenerator, true);
> }
> private void ser(final RecordId recordId, final JsonGenerator
> jsonGenerator, final boolean includeType) throws IOException {
> jsonGenerator.writeStartObject();
> if (includeType)
> jsonGenerator.writeStringField(GraphSONTokens.CLASS,
> RecordId.class.getName());
> SchemaTable schemaTable = recordId.getSchemaTable();
> jsonGenerator.writeObjectField("schema", schemaTable.getSchema());
> jsonGenerator.writeObjectField("table", schemaTable.getTable());
> jsonGenerator.writeObjectField("id", recordId.getId().toString());
> jsonGenerator.writeEndObject();
> }
> }
> static class CustomIdJacksonDeserializer extends
> StdDeserializer<RecordId> {
> public CustomIdJacksonDeserializer() {
> super(RecordId.class);
> }
> @Override
> public RecordId deserialize(final JsonParser jsonParser, final
> DeserializationContext deserializationContext) throws IOException,
> JsonProcessingException {
> String schema = null;
> String table = null;
> Long id = null;
> while (!jsonParser.getCurrentToken().isStructEnd()) {
> if (jsonParser.getText().equals("schema")) {
> jsonParser.nextToken();
> schema = jsonParser.getText();
> } else if (jsonParser.getText().equals("table")) {
> jsonParser.nextToken();
> table = jsonParser.getText();
> } else if (jsonParser.getText().equals("id")) {
> jsonParser.nextToken();
> id = Long.valueOf(jsonParser.getText());
> } else
> jsonParser.nextToken();
> }
> if (!Optional.ofNullable(schema).isPresent())
> throw deserializationContext.mappingException("Could not
> deserialze RecordId: 'schema' is required");
> if (!Optional.ofNullable(table).isPresent())
> throw deserializationContext.mappingException("Could not
> deserialze RecordId: 'table' is required");
> if (!Optional.ofNullable(id).isPresent())
> throw deserializationContext.mappingException("Could not
> deserialze RecordId: 'id' is required");
> return new RecordId(SchemaTable.of(schema, table), id);
> }
> }
> {code}
> When using {{.embedTypes(true)}} the serialization works but the lossy tests
> fail as Jackson then does not upgrade floats to doubles.
> If I omit the {{embedTypes(true)}} then the tests fail as the id is never
> deserialized to my custom RecordId.
> The test I am currently testing on is
> {{IoTest.shouldReadWriteVertexWithOUTOUTEdgesToGraphSON}}
> With {{embedTypes(true)}} ommited the test fails with
> {code}
> org.junit.ComparisonFailure:
> Expected :public.person:::1
> Actual :{schema=public, table=person, id=1}
> {code}
> With {{embedTypes(true)}} included the test fails with
> {code}
> java.lang.ClassCastException: java.lang.Float cannot be cast to
> java.lang.Double
> {code}
> Any ideas as to what to do?
> Thanks
> Pieter
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)