Rawjsondocument - Migrate SDK 2 to SDK 3

Hi,
Our applications are writing RawJsonDocument in to couchbase using SDK2. and I am trying to migrate them to SDK3.

Our RawJsonDocument can contain one JsonObject or Collection of JsonObject

I have following question:

  1. If I read using SDK3, what transcoder should I pass
  2. while reading object from getResult, what should I do contentAs(JsonObject.class) or contentAs(JsonArray.class) ? Given that I can have either of these while migration is happening, how do I inspect and correctly deseralize the value? or should I just contentAs(String.class) while reading?

There is a RawJsonTranscoder in SDK 3. I’m not sure if it would handle an array of json objects.

Thanks.
yes, I checked the same, so that should handle transcoder part but what about deserialization? second question, how to handle .contentAs()?

but what about deserialization?

I assume the same as you were handling it with the RawJsonTranscoder in SDK 2 (?) - which would have been outside of the transcoder (?).

You can also define your own transcoder.

yes, that is done but my question is more about converting GetResult/GetAnyReplicaResult to final object, is that going to be String/JsonObject/Jsonarry or what exactly?

GetAnyReplicaOptions.getAnyReplicaOptions()
.transcoder(RawJsonTranscoder.INSTANCE)
.timeout(Duration.ofMillis(timeoutMs)))).map(result → result.contentAs(String.class)

Question is because SDK 2 would have done writes as RawJsonDocument and if we switch to SDK3 then how old writes with new reads work out?

The example shows the RawJsonTranscoder returning a String. It can also return byte.

got it. So if my understanding is correct -

In SDK 2, we used String type to create RawJsonDocument and store in couchbase
using RawJsonDocument.create(id, expiryInSeconds.intValue(), jsonByteString)

So with SDK 3 migration,
If I do result.contentAs(String.class) then it should work fine with old write and new read patterns.

Also, for inserts, we will do _collection.insert(id, jsonPayloadAsString) with SDK 3 and that should also be compatible with result.contentAs(String.class) as in new write and new read.

In all the cases of read and write we will use RawJsonTranscoder.INSTANCE.

Is this understanding correct?

Hi Tusharkumar!

You’ve got the right idea. Here’s some code you can use to validate your assumptions.

String id = "myDocument";
String jsonPayloadAsString = "{\"foo\":123}";

collection.upsert(
  id,
  jsonPayloadAsString,
  UpsertOptions.upsertOptions()
    // Tell the SDK we're giving it pre-serialized JSON.
    // Payload can be either String or byte[].
    .transcoder(RawJsonTranscoder.INSTANCE)
);

GetResult result = collection.get(
  id,
  GetOptions.getOptions()
    // Tell the SDK we will deserialize the JSON ourselves.
    .transcoder(RawJsonTranscoder.INSTANCE)
);

// This transcoder supports content as String.class or byte[].class
String afterRead = result.contentAs(String.class);
System.out.println(jsonPayloadAsString.equals(afterRead));
// prints: true

Thanks,
David

Thanks for confirmation!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.