How to convert a Java Object to JsonObject type of couch base?

I have a list of Java objects which i would like to insert into Couchbase using a bulk insert. The example i found at “http://docs.couchbase.com/developer/java-2.0/documents-bulk.html” is going though each property in java class and adding it to JsonObject. is there a way that we can convert a Java object into JsonObject without traversing through all properties manually and adding it to the JsonObject. I am looking for something similar to what we have in Gson library to convert a Java object to Json and vice versa by just supplying the Json string and the corresponding Java class type? is it mandatory to to add the key value pairs explicitly in order to create a JsonObject?

I am using Latest Couchbase Java SDK.

Hi @madhu_garimilla1

We are working on POJO mapping support for the upcoming 2.2.0 release, but in the meantime, if you don’t want to use the JsonObject you can just use the RawJsonDocument, which will accept a JSON string that you can pull out of Jackson or GSON. so Instead of doing bucket.insert(JsonDocument.create()) you do bucket.insert(RawJsonDocument.create(“id”, yourGsonjsonString))

my example follows

Using FlexJson and RawJsonDocument

import com.company.domain.user.User;
import com.company.backend.persistance.CouchBaseManager;
import com.company.backend.persistance.dao.UserDao;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.JsonLongDocument;
import com.couchbase.client.java.document.RawJsonDocument;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserDaoImpl implements UserDao {

private static Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);

Bucket db = CouchBaseManager.getSingleton().mybucket;
// Cluster cluster  = CouchBaseManager.getSingleton().cluster;

JSONSerializer serializer = new JSONSerializer();

private String getNextUid(String className ) {
    String counterName = className + "Counter";
    JsonLongDocument val = db.counter(counterName, 1, 1);
    String keyPrefix = className;
    String nextKey = keyPrefix + val.content().longValue();
    return nextKey;
}

@Override
public User insert(User entity) {
    String key = getNextUid( entity.getClassName() );
    entity.setObjectId(key);
    String jsonString = serializer.deepSerialize(entity);
    RawJsonDocument jsonDoc = RawJsonDocument.create(key, jsonString);
    RawJsonDocument retJsonDoc = db.insert(jsonDoc);
    User newUser = new JSONDeserializer<User>().deserialize( retJsonDoc.content() , User.class );
    return newUser;
}

@Override
public User replace(User entity) {
    String jsonString = serializer.deepSerialize(entity);
    RawJsonDocument jsonDocument = RawJsonDocument.create(entity.getObjectId(), jsonString);
    RawJsonDocument replacedJsonDocument = db.replace(jsonDocument);
    User replacedUser = new JSONDeserializer<User>().deserialize(replacedJsonDocument.content(), User.class);
    return replacedUser;
}


@Override
public User remove(User entity) {
    String jsonString = serializer.deepSerialize(entity);
    RawJsonDocument jsonDocument = RawJsonDocument.create(entity.getObjectId(), jsonString);
    RawJsonDocument removedJsonDocument = db.remove(jsonDocument);
    User removedUser = new JSONDeserializer<User>().deserialize(removedJsonDocument.content(), User.class);
    return removedUser;
}


@Override
public User get(User entity) {
    String jsonString = serializer.deepSerialize(entity);
    RawJsonDocument jsonDocument = RawJsonDocument.create(entity.getObjectId(), jsonString);
    RawJsonDocument retrievedJsonDocument = db.get(jsonDocument);
    User retrievedUser = new JSONDeserializer<User>().deserialize(retrievedJsonDocument.content(), User.class);
    return retrievedUser;
}

@Override
public User get(String uid) {
    User  retrievedUser = new User();
    retrievedUser.setObjectId(uid);
    retrievedUser = get( retrievedUser );
    return retrievedUser;
}



@Override
public boolean exists( String key) {
    boolean exists = false;
    JsonDocument jsonDocument = db.get(key);
    if(jsonDocument.id().equals(key)) {
        exists = true;
    }
    return exists;
}

}

1 Like

@GeorgeLeon thanks for sharing!

Is the POJO mapping support available in 2.2.0? If so, could you provide some pointers/links? Thanks.

any news on POJO mapping

There’s an experimental POJO mapping support since 2.2.0. This blog post describes it a little, along with the current limitations.

If your needs go further than that, I’d suggest looking at Spring Data Couchbase 2.x instead.