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:
If I read using SDK3, what transcoder should I pass
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?
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?
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.
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