1717
1818import com .couchbase .client .java .Bucket ;
1919import com .couchbase .client .java .document .JsonDocument ;
20+ import com .couchbase .client .java .document .RawJsonDocument ;
21+ import com .couchbase .client .java .document .json .JsonArray ;
2022import com .couchbase .client .java .document .json .JsonObject ;
23+ import com .couchbase .client .java .document .json .JsonValue ;
2124import com .couchbase .client .java .error .DocumentDoesNotExistException ;
2225import org .jnosql .diana .api .Value ;
2326import org .jnosql .diana .api .key .BucketManager ;
@@ -59,7 +62,13 @@ public class CouchbaseBucketManager implements BucketManager {
5962 public <K , V > void put (K key , V value ) {
6063 requireNonNull (key , "key is required" );
6164 requireNonNull (value , "value is required" );
62- bucket .upsert (JsonDocument .create (key .toString (), JsonObjectCouchbaseUtil .toJson (JSONB , value )));
65+
66+ if (JsonValue .checkType (value )) {
67+ bucket .upsert (RawJsonDocument .create (key .toString (), JSONB .toJson (value .toString ())));
68+ } else {
69+ bucket .upsert (JsonDocument .create (key .toString (), JsonObjectCouchbaseUtil .toJson (JSONB , value )));
70+ }
71+
6372 }
6473
6574 @ Override
@@ -69,19 +78,26 @@ public <K> void put(KeyValueEntity<K> entity) throws NullPointerException {
6978 }
7079
7180 @ Override
72- public <K > void put (KeyValueEntity <K > entity , Duration ttl ){
81+ public <K > void put (KeyValueEntity <K > entity , Duration ttl ) {
7382 requireNonNull (entity , "entity is required" );
7483 requireNonNull (ttl , "ttl is required" );
7584
7685
77- JsonObject jsonObject = JsonObjectCouchbaseUtil .toJson (JSONB , entity .get ());
86+ if (JsonValue .checkType (entity .get ())) {
87+ RawJsonDocument jsonDocument = RawJsonDocument .create (entity .getKey ().toString (), (int ) ttl .getSeconds (),
88+ JSONB .toJson (entity .get ().toString ()));
89+
90+ bucket .upsert (jsonDocument );
91+ } else {
92+ JsonObject jsonObject = JsonObjectCouchbaseUtil .toJson (JSONB , entity .get ());
93+ JsonDocument jsonDocument = JsonDocument .create (entity .getKey ().toString (), (int ) ttl .getSeconds (), jsonObject );
94+ bucket .upsert (jsonDocument );
95+ }
7896
79- JsonDocument jsonDocument = JsonDocument .create (entity .getKey ().toString (), (int ) ttl .getSeconds (), jsonObject );
80- bucket .upsert (jsonDocument , ttl .toMillis (), MILLISECONDS );
8197 }
8298
8399 @ Override
84- public <K > void put (Iterable <KeyValueEntity <K >> keyValueEntities ){
100+ public <K > void put (Iterable <KeyValueEntity <K >> keyValueEntities ) {
85101 requireNonNull (keyValueEntities , "keyValueEntities is required" );
86102 keyValueEntities .forEach (this ::put );
87103 }
@@ -96,7 +112,7 @@ public <K> void put(Iterable<KeyValueEntity<K>> keyValueEntities, Duration ttl)
96112 @ Override
97113 public <K > Optional <Value > get (K key ) throws NullPointerException {
98114 requireNonNull (key , "key is required" );
99- JsonDocument jsonDocument = bucket .get (key .toString ());
115+ RawJsonDocument jsonDocument = bucket .get (key .toString (), RawJsonDocument . class );
100116 if (Objects .isNull (jsonDocument )) {
101117 return Optional .empty ();
102118 }
@@ -105,7 +121,7 @@ public <K> Optional<Value> get(K key) throws NullPointerException {
105121 }
106122
107123 @ Override
108- public <K > Iterable <Value > get (Iterable <K > keys ){
124+ public <K > Iterable <Value > get (Iterable <K > keys ) {
109125 requireNonNull (keys , "keys is required" );
110126 return stream (keys .spliterator (), false )
111127 .map (this ::get )
@@ -115,7 +131,7 @@ public <K> Iterable<Value> get(Iterable<K> keys){
115131 }
116132
117133 @ Override
118- public <K > void remove (K key ){
134+ public <K > void remove (K key ) {
119135 requireNonNull (key , "key is required" );
120136 try {
121137 bucket .remove (key .toString ());
0 commit comments