Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a JsonDocument to its mapped entity

I use CouchbaseRepository in my project but sometimes I use lower level couchbase sdk methods to retrieve JsonDocument. Is there a way I can use spring-data-couchbase to convert a JsonDocument to lets say a User?

This is all done internally in CouchbaseTemplate in the method private <T> T mapToEntity(String id, Document<String> data, Class<T> entityClass)

But as you can see it's private so I can't call it myself.

like image 649
prettyvoid Avatar asked Sep 20 '26 01:09

prettyvoid


1 Answers

You can create a wrapper which uses Jackson to convert the JsonDocument to any object you want.

mapper.readValue(doc.content().toString(), SomeClass.class);

The first argument is the JsonDocument, the content method is what contains the actual Json.

like image 191
Justin ross Avatar answered Sep 22 '26 23:09

Justin ross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!