I have a java couchbase client v2 that saves documents to database
JsonObject content = ...
JsonDocument newDocument = JsonDocument.create(key, expiration, content);
bucket.upsert(newDocument);
When content
is null
it saves a 'null-value' document that looks in the couchbase console like this
I'm migrating to couchbase java client v3 and save the document next way
JsonObject content = ...
var upsertOptions = UpsertOptions.upsertOptions();
if (expiration != null) {
upsertOptions.expiry(expiration);
}
bucket.defaultCollection().upsert(key, content, upsertOptions);
JsonDocument
is not available in v3 and I put JsonObject
directly and it works OK when content
is not null
.
When I have content
as null
like a previous case it throws an exception com.couchbase.client.core.error.InvalidArgumentException
. If I try to use JsonObject.create()
instead of null
it creates an empty document but not 'null-value' document
Is it possible to create the same 'null-value' document with a client v3 for the sake of backward compatibility?
With Couchbase Java SDK 3.x, here's one way to store a JSON document with a null at the root:
collection.upsert(key, "null", UpsertOptions.upsertOptions()
.transcoder(RawJsonTranscoder.INSTANCE));
It uses RawJsonTranscoder which interprets the content as pre-serialized JSON.