When implementing a Couchbase Light database as local database for one of my Android apps, I put a POJO object in one of the properties of a document.
When retrieving the object back with Document.getProperties() I got back a LinkedHashMap (with the POJO variables as key/value pairs) when accessing the document after the creation of the first revision or after opening the database but got the POJO object back after the document had been updated.
The following code shows this behaviour:
private static String testDocumentId;
class A {
public String value;
public A (String value){
this.value = value; }
}
public void updateTest() throws CouchbaseLiteException{
Document document = database.createDocument(); HashMap <String, Object> properties = new HashMap <String, Object>(); properties.put("key", new A("myValue"));
document.putProperties(properties);
Object retrievedValue = document.getProperties().get("key"); Log.d("Couchbase Update Test", "retrievedValue.getClass() = " + retrievedValue.getClass().getSimpleName());
properties.put("_rev", document.getCurrentRevisionId()); document.putProperties(properties);
Object retrievedValue2 = document.getProperties().get("key"); Log.d("Couchbase Update Test", "retrievedValue2.getClass() = " + retrievedValue2.getClass().getSimpleName());
testDocumentId = (String) document.getProperty("_id");
}
public void updateTest2(){
Document document = database.getDocument(testDocumentId); Object retrievedValue = document.getProperties().get("key");
Log.d("Couchbase Update Test 2", "retrievedValue.getClass() = " + retrievedValue.getClass().getSimpleName());
}
Calling updateTest and updateTest2 one after the other results in
03-22 19:43:25.870: D/Couchbase Update Test(29376): retrievedValue.getClass() = LinkedHashMap
03-22 19:43:25.900: D/Couchbase Update Test(29376): retrievedValue2.getClass() = A
03-22 19:43:25.900: D/Couchbase Update Test 2(29376): retrievedValue.getClass() = A
while closing the database after Update Test 1 and reopening it before Update Test 2 results in
03-22 19:39:29.710: D/Couchbase Update Test(29171): retrievedValue.getClass() = LinkedHashMap
03-22 19:39:29.730: D/Couchbase Update Test(29171): retrievedValue2.getClass() = A
03-22 19:39:29.750: D/Couchbase Update Test 2(29171): retrievedValue.getClass() = LinkedHashMap
Analyzing the source code a bit I found out that Couchbase Light stores the body of the document either in JSON format (during creation or when reading the document from the database) or as key/value hashmap after an update of the document properties. Document.getProperties() seems to just return what is currently available.
My questions are:
- Is this the expected behaviour of Couchbase Light or is this a bug?
- Is there any workaround to get independent of the document state
always the same object type back for properties containing objects? - Especially, is there any way to apply Jackson annotations so that the
objects get properly reconstructed by Jacksonâs object mapper? I
tried the âusual thingsâ using @JsonTypeInfo etc. but did not have
much luck so far.
Any help highly appreciated.
Thanks
Joerg