Content type of a value?

Hello everyone,

I had a question concerning the Java SDK (2.2.3) and I wanted to know how if it was possible to get the content type of a specific data in a bucket.

Let’s consider the following example: I have configured a bucket containing three types of data: JsonDocument, StringDocument and JsonBooleanDocument.

When I need to retrieve the value related to a specific key, so far the only way I found was to do the job like that:

  • bucket.get(key)
  • If I got an exception (not a JsonDocument) => bucket.get(key, StringDocument.class)
  • If I got an exception (not a StringDocument) => bucket.get(key, JsonBooleanDocument.class)

Is there any way to do it smarter? Is it possible to retrieve the value type in advance for instance?

Thanks for your help


So you are retrieving keys without knowing the type in advance (or even expecting any particular type)?

There isn’t really a way of retrieving the value type in advance. First, all the JsonXXXDocument are stored in couchbase using a common JSON flag, so you cannot distinguish between them using metadata only… StringDocument uses another flag which is incompatible.

Maybe for the JsonDocument and JsonBooleanDocument you can replace it with RawJsonDocument? It will then be up to you to decode the raw JSON representation to your expected value type (probably Object?). But you’d still have to manage the case where transcoding to RawJsonDocument cannot happen (in which case this would be a StringDocument).

If in your data modelling you prefix your keys with the logical type, this could help you decided which target class to use during retrieval. For instance you want to store cars as JsonDocument and comments as StringDocument, then have your keys prefixed with “car::” or “comment::”.

if (key.startsWith("car::") {
    JsonDocument carDoc = bucket.get(key, JsonDocument.class);
    //do something with car document
} else if (key.startsWith("comment::") {
    StringDocument commentDoc = bucket.get(key, StringDocument.class);
    //do something with comment document


Thanks for your reply.

Ok understood… It would have been great to have such kind of method in the Bucket class to retrieve the value type though. But I might imagine it’s not that simple to implement.