riorio
riorio

Reputation: 6816

How to put Java object in Couchbase's JsonObject in one operation

With Couchbase's jsonObject we can create object with certain fields:

import    com.couchbase.client.java.document.json.JsonObject
...
JsonObject content = JsonObject.create().put("some", "value");

The put function has several String & value options such as double, int, String etc, as can be seen here.

I was looking for a way to put a whole object in it. Something like:

Cat cat = new Cat(.....)
JsonObject content = JsonObject.create().put(cat);

Is there a way to do it and not to iterate over all of Cat's fields?

Upvotes: 1

Views: 588

Answers (1)

riorio
riorio

Reputation: 6816

So here is how it can be done:

Cat cat = new Cat(...);
String asJson = gson.toJson(cat);
JsonObject.fromJson(asJson);

Upvotes: 2

Related Questions