Closed
Description
spac-valentin opened DATACOUCH-560 and commented
I have an entity without a strict structure (user can add custom fields/json objects)
@Document
public class MyClass {
@Id
private String id;
@Field
private List<Map<String, Object>> fields;
@Field
private List<Map<String, Object>> configuration;
MappingCouchbaseConverter converts this into a mix of CouchbaseDocuments & CouchbaseLists and sends it to couchbase Transcoder which encodes it and stores it into DB.
Issue: JacksonJsonSerializer used by JsonTranscoder serialises the above object into
{
"configuration":{
"empty":false
},
"_class":"org.myorg.MyEntity",
"fields":{
"empty":false
}
}
Workaround:
In your CouchbaseConfiguration, override configureEnvironment and provide a custom object mapper to handle this scenario
@Override
protected void configureEnvironment(ClusterEnvironment.Builder builder) {
Jackson2ObjectMapperBuilder jacksonBuilder = new Jackson2ObjectMapperBuilder();
jacksonBuilder.serializationInclusion(JsonInclude.Include.NON_NULL);
jacksonBuilder.modules(
new CouchbaseJacksonModule(),
new JsonValueModule() // Don't forget to add this module provided by couchbase client
);
builder.jsonSerializer(JacksonJsonSerializer.create(objectMapper()));
super.configureEnvironment(builder);
}
CouchbaseJacksonModule
public class CouchbaseJacksonModule extends SimpleModule {
public CouchbaseJacksonModule() {
super(new Version(1, 0, 0, null, "org.myorg", "CouchbaseWorkaround"));
addSerializer(CouchbaseDocument.class, new CouchbaseDocumentSerializer());
addSerializer(CouchbaseList.class, new CouchbaseListSerializer());
}
public static class CouchbaseDocumentSerializer extends JsonSerializer<CouchbaseDocument> {
@Override
public void serialize(CouchbaseDocument value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
Map<String, Object> valueContent = value.getContent();
JsonSerializer<Object> serializer = serializers.findValueSerializer(valueContent.getClass());
serializer.serialize(valueContent, gen, serializers);
}
}
public static class CouchbaseListSerializer extends JsonSerializer<CouchbaseList> {
@Override
public void serialize(CouchbaseList value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
List<Object> objects = value.export();
JsonSerializer<Object> serializer = serializers.findValueSerializer(objects.getClass());
serializer.serialize(objects, gen, serializers);
}
}
}
I was expecting this to work OOTB
Affects: 4.0 GA (Neumann)
Referenced from: pull request runnerway@fed0e5f
1 votes, 2 watchers