Raw json document with n1ql in java sdk

n1ql

#1

The K-V api in Java sdk supports raw json documents, so users can use their own json library. For example, I found the following code snippet from the docs

bucket.get("rawJsonDoc", RawJsonDocument.class);

However, I couldn’t find anything like that for the N1QL api. How do I use a custom json library with N1QL and Java sdk?

Thanks!


#2

There is currently no API for using a custom JSON library with N1QL. This has been asked in another forum post (Using Custom Transcoder for N1QL Query), so we’ll think about it. see JCBC-869


#3

@simonbasle Thanks for your reply. According to the ticket, fix version is 2.3. Does that mean that this feature will be added in 2.3, or is it still unclear? Thanks.


#4

it hasn’t been developed yet so this is still unclear actually


#5

@simonbasle I see. It would be nice to have byteValue() implemented. Thanks.


#6

the feature will be available in 2.2.4, out in February :smile: :thumbsup:


#7

@simonbasle Can you provide an example of how to use this new feature?


#8

@asarkar simply use the byteValue method on N1qlQueryRow/AsyncN1qlQueryRow to get the raw bytes and avoid internal deserialization of JSON. Then use your existing JSON deserializer to do that yourself.

For instance, if you just want to print the N1QL rows as Strings to the console:

//assuming we have person data in the default bucket
N1qlQuery query = N1qlQuery.simple("SELECT firstName, lastName FROM default WHERE type = 'person'");
N1qlResult result = bucket.query(query);
for (N1qlQueryRow row : result) {
    //we go straight for the raw bytes and turn into string
    String fullName = new String(row.byteValue());
    //no JsonObject conversion has taken place as long as you don't call "value()"
    System.out.println(fullName);
}