Cannot put an Object other than of instance String as key of Mapper

52 views
Skip to first unread message

Alex Chevrier

unread,
Feb 12, 2016, 11:30:08 AM2/12/16
to Couchbase Mobile
Hi,

I worked on a small project in order to discover how things work for couchbase-lite-java-1.2.0 on
Windows 10.
For this project I created a view and then put an object which was not an instance of String as a key of the mapper here is the code snippet :

public void createEventsByDateView() {
View eventsByDateView = this.getView("eventsByDate");
if (eventsByDateView != null) {
eventsByDateView.setMap(
new Mapper(){
public void map(Map<String, Object> document,
Emitter emitter) {
if(document.get("location") != null){
emitter.emit(
new SimplePojo("location"), document.get("location"));
}
else{
emitter.emit(new SimplePojo("mock"), document.get("mock"));
}
}
}, "3"
);
}
printQueryToLog(eventsByDateView);
}

and here is the the SimplePojo :

private class SimplePojo{

SimplePojo(String string){
this.setString(string);
}
public String getString() {
return string;
}

public void setString(String string) {
this.string = string;
}

String string;

}

After running the app I've had this exception :

Exception in thread "main" java.lang.Error: invalid class for JSON
    at com.couchbase.cbforest.View.keyAdd(View.java:163)
    at com.couchbase.cbforest.View.objectToKey(View.java:124)
    at com.couchbase.cbforest.Indexer.emit(Indexer.java:33)
    at com.couchbase.lite.store.ForestDBViewStore.emit(ForestDBViewStore.java:336)
    at com.couchbase.lite.store.ForestDBViewStore.updateIndexes(ForestDBViewStore.java:276)
    at com.couchbase.lite.View.updateIndexes(View.java:321)
    at com.couchbase.lite.View.updateIndex(View.java:294)
    at com.couchbase.lite.Database.queryViewNamed(Database.java:2092)
    at com.couchbase.lite.Query.run(Query.java:433)
    at Client.printQueryToLog(Client.java:285)
    at Client.createEventsByDateView(Client.java:261)
    at Client.helloCBL(Client.java:73)
    at Client.run(Client.java:38)
    at StartApp.main(StartApp.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144

Must I use only use String as the key of the mapper on Windows ?

Alex

Jens Alfke

unread,
Feb 13, 2016, 12:21:46 PM2/13/16
to mobile-c...@googlegroups.com
Keys in a view don't have to be strings, but they do have to be JSON compatible. So no custom classes.

--Jens

Alex Chevrier

unread,
Feb 15, 2016, 8:06:15 AM2/15/16
to Couchbase Mobile
Hi,

I believe that in Couchbase lite 1.1.0 a jackson XML library was attached to it, mapper at that time didn't need  JSON compatible key and custom class were compatible.
Do you confirm that using forestDB and Couchbase lite 1.2.0 it is not possible anymore ? If yes, is there any custom way to replicate the behavior of Couchbase lite 1.1.0.

Alex
Reply all
Reply to author
Forward
0 new messages