You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/howtos/pages/transcoders-nonjson.adoc
+27-11Lines changed: 27 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,9 @@ It may use a serializer for this.
20
20
NOTE: Many applications will not need to be aware of transcoders and serializers, as the defaults support most standard JSON use cases.
21
21
The information in this page is only needed if the application has an advanced use-case, likely involving either non-JSON data, or a requirement for a particular JSON serialization library.
22
22
23
+
23
24
== Default Behaviour
25
+
24
26
The `ClusterEnvironment` contains a global transcoder and serializer, which by default are `JsonTranscoder` and `DefaultJsonSerializer`.
25
27
26
28
`DefaultJsonSerializer` uses the high-performance JSON library https://github.com/FasterXML/jackson[Jackson] for serializing and deserializing byte arrays to and from concrete objects.
@@ -54,7 +56,9 @@ This table summarizes that information, and this more concise form will be used
54
56
|JSON
55
57
|===
56
58
59
+
57
60
== Using Custom Jackson
61
+
58
62
As described above, the default serializer (`DefaultJsonSerializer`) uses Jackson for serializing objects.
59
63
(This Jackson dependency is shaded into a different namespace, so that it does not clash with any Jackson used by your application.)
Since Gson has already done the serialization work, we don't want to use the default `JsonTranscoder`, as this will run the provided String needlessly through `DefaultJsonSerializer` (Jackson).
@@ -122,10 +128,12 @@ Gson can then be used for the deserialization.
However, it is possible to store non-JSON documents, such as raw binary data, perhaps using an concise binary encoding like https://msgpack.org[MessagePack] or https://cbor.io/[CBOR], in the Key-Value store.
131
139
@@ -135,6 +143,7 @@ Also note that some simple data types can be stored directly as JSON, without re
135
143
A valid JSON document can be a simple integer (`42`), string (`"hello"`), array (`[1,2,3]`), boolean (`true`, `false`) and the JSON `null` value.
136
144
137
145
=== RawStringTranscoder
146
+
138
147
The RawStringTranscoder provides the ability for the user to explicitly store and retrieve raw string data with Couchbase.
139
148
It can be used to avoid the overhead of storing the string as JSON, which requires two bytes for double quotes, plus potentially more for escaping characters.
140
149
@@ -163,10 +172,11 @@ Here’s an example of using the `RawStringTranscoder`:
The RawBinaryTranscoder provides the ability for the user to explicitly store and retrieve raw byte data to Couchbase.
171
181
The transcoder does not perform any form of real transcoding, and does not take a serializer, but rather passes the data through and assigns the appropriate binary Common Flag.
172
182
@@ -193,20 +203,22 @@ Here’s an example of using the `RawBinaryTranscoder`:
More advanced transcoding needs can be accomplished if the application implements their own transcoders and serializers.
201
212
202
213
=== Creating a Custom Serializer
214
+
203
215
We saw above one example of using Google Gson with the `RawJsonTranscoder`, but it requires the application to explicitly serialize and deserialize objects each time.
204
216
By creating a custom Gson serializer, we can avoid this.
205
217
206
218
It’s easy to create a serializer. Simply implement the `JsonSerializer` interface’s three methods:
NOTE: The `TypeRef` overload is optional, and is only used if the application explicitly uses it with for example `result.contentAs(new TypeRef<String> {})`, which is an uncommon requirement.
@@ -218,28 +230,31 @@ All we need to do is change the serializer, as so:
The default global `JsonTranscoder` will be initialized with this serializer.
239
251
Now our GsonSerializer will be automatically used by all operations, without needing to provide a transcoder each time.
240
252
253
+
241
254
=== Creating a Custom Transcoder
255
+
242
256
Let’s look at a more complex example: encoding the JSON alternative, https://msgpack.org[MessagePack].
257
+
243
258
MessagePack is a compact binary data representation, so it should be stored with the binary Common Flag.
244
259
The Common Flag is chosen by the transcoder, and none of the existing transcoders matches our needs (`RawBinaryTranscoder` does set the binary flag, but it passes data through directly rather than using a serializer).
245
260
So we need to write one.
@@ -248,14 +263,14 @@ Start by creating a new serializer for MessagePack. This is similar to the Gson
0 commit comments