How to handle CB SDK 2 based JsonDocument format usage when migrating to CB SDK 3?

In order to handle the insertion functionality in Couchbase SDK 3, I believe can be handled as mentioned below.

public void insert(String id, String jsonString, Collection collection) {
        MutationResult upsertResult =null;
        JsonObject content = JsonObject.fromJson(jsonString);

     //Insert Document
     upsertResult = collection.upsert(id, content);
     }Catch (CasMismatchException e){
      //To Do: the caller is expected to re-do the whole "fetch-modify-update" cycle again

    //Retrieve the documentGetResult getResult = collection.get(id).contentAsObject();

But how can I return a document format similar to JsonDocument or an alternative format in CB SDK 3? The current project which was developed using CB SDK 2 uses the JsonDocument format extensively. How to handle this change without breaking the current code after the migration to Couchbase SDK 3?

offer an alternative to the above scenario. If so could you please indicate how it could be used in the above simple scenario?

Hi @gimhanas. JsonDocument is basically just the document’s contents, CAS and ID bundled together, so you can easily create your own class or record that does this. The GetResult returned from collection.get() contains the first 2, and you have the ID.

N.b. your code doesn’t look quite right. An upsert can’t return a CasMismatchException. Generally you would do something like this to perform optimistic locking:

GetResult result = collection.get(docId);
JsonObject content = result.contentAsObject();
JsonObject modifiedContent = ; // modify as you need
collection.replace(docId, modifiedContent, replaceOptions().cas(result.cas()));

And then potentially wrap that in a loop or recursive function to handle CasMismatchException.