You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tinkerpop.apache.org by "stephen mallette (JIRA)" <ji...@apache.org> on 2015/04/14 15:10:12 UTC

[jira] [Closed] (TINKERPOP3-581) GraphSONWriter and CustomId issue

     [ https://issues.apache.org/jira/browse/TINKERPOP3-581?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

stephen mallette closed TINKERPOP3-581.
---------------------------------------
    Resolution: Implemented

The branch has been merged back to master at this point.  Added more tests to enforce proper behavior around {{Element}} equality and extra tests to the process suite to validate operations at that level.  It did uncover a problem or two (like has(id,"1") not being properly supported), which I've resolved.

> 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)