Skip to content

Commit 8b0af3d

Browse files
include:: (devguide) fixes
1 parent 7e9d6b3 commit 8b0af3d

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

modules/howtos/pages/transcoders-nonjson.adoc

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ It may use a serializer for this.
2020
NOTE: Many applications will not need to be aware of transcoders and serializers, as the defaults support most standard JSON use cases.
2121
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.
2222

23+
2324
== Default Behaviour
25+
2426
The `ClusterEnvironment` contains a global transcoder and serializer, which by default are `JsonTranscoder` and `DefaultJsonSerializer`.
2527

2628
`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
5456
|JSON
5557
|===
5658

59+
5760
== Using Custom Jackson
61+
5862
As described above, the default serializer (`DefaultJsonSerializer`) uses Jackson for serializing objects.
5963
(This Jackson dependency is shaded into a different namespace, so that it does not clash with any Jackson used by your application.)
6064

@@ -80,7 +84,9 @@ Cluster cluster = Cluster.connect(
8084
);
8185
----
8286

87+
8388
== RawJsonTranscoder
89+
8490
The RawJsonTranscoder provides the ability for the application to explicitly specify that the data they are storing or retrieving is JSON.
8591
This transcoder does not accept a serializer, and always performs straight pass through of the data to the server.
8692
This enables the application to avoid unnecessary parsing costs when they are certain they are using JSON data.
@@ -111,7 +117,7 @@ Here we want to use https://github.com/google/gson[Google Gson] for serializatio
111117

112118
[source,java]
113119
----
114-
include::example$Transcoding.java[tag=gson-encode,indent=0]
120+
include::devguide:example$java/Transcoding.java[tag=gson-encode,indent=0]
115121
----
116122

117123
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.
122128

123129
[source,java]
124130
----
125-
include::example$Transcoding.java[tag=gson-decode,indent=0]
131+
include::devguide:example$java/Transcoding.java[tag=gson-decode,indent=0]
126132
----
127133

134+
128135
== Non-JSON Transcoders
136+
129137
It is most common to store JSON with Couchbase.
130138
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.
131139

@@ -135,6 +143,7 @@ Also note that some simple data types can be stored directly as JSON, without re
135143
A valid JSON document can be a simple integer (`42`), string (`"hello"`), array (`[1,2,3]`), boolean (`true`, `false`) and the JSON `null` value.
136144

137145
=== RawStringTranscoder
146+
138147
The RawStringTranscoder provides the ability for the user to explicitly store and retrieve raw string data with Couchbase.
139148
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.
140149

@@ -163,10 +172,11 @@ Here’s an example of using the `RawStringTranscoder`:
163172

164173
[source,java]
165174
----
166-
include::example$Transcoding.java[tag=string,indent=0]
175+
include::devguide:example$java/Transcoding.java[tag=string,indent=0]
167176
----
168177

169178
=== RawBinaryTranscoder
179+
170180
The RawBinaryTranscoder provides the ability for the user to explicitly store and retrieve raw byte data to Couchbase.
171181
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.
172182

@@ -193,20 +203,22 @@ Here’s an example of using the `RawBinaryTranscoder`:
193203

194204
[source,java]
195205
----
196-
include::example$Transcoding.java[tag=binary,indent=0]
206+
include::devguide:example$java/Transcoding.java[tag=binary,indent=0]
197207
----
198208

199209
== Custom Transcoders and Serializers
210+
200211
More advanced transcoding needs can be accomplished if the application implements their own transcoders and serializers.
201212

202213
=== Creating a Custom Serializer
214+
203215
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.
204216
By creating a custom Gson serializer, we can avoid this.
205217

206218
It’s easy to create a serializer. Simply implement the `JsonSerializer` interface’s three methods:
207219
[source,java]
208220
----
209-
include::example$Transcoding.java[tag=gson-serializer,indent=0]
221+
include::devguide:example$java/Transcoding.java[tag=gson-serializer,indent=0]
210222
----
211223

212224
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:
218230

219231
[source,java]
220232
----
221-
include::example$Transcoding.java[tag=gson-custom-encode,indent=0]
233+
include::devguide:example$java/Transcoding.java[tag=gson-custom-encode,indent=0]
222234
----
223235

224236
And for decoding:
225237

226238
[source,java]
227239
----
228-
include::example$Transcoding.java[tag=gson-custom-decode,indent=0]
240+
include::devguide:example$java/Transcoding.java[tag=gson-custom-decode,indent=0]
229241
----
230242

231243
If you want to use Gson for all JSON serialization, you can register it as the global JSON serializer when creating a `ClusterEnvironment`:
232244

233245
[source,java]
234246
----
235-
include::example$Transcoding.java[tag=gson-register-1,indent=0]
247+
include::devguide:example$java/Transcoding.java[tag=gson-register-1,indent=0]
236248
----
237249

238250
The default global `JsonTranscoder` will be initialized with this serializer.
239251
Now our GsonSerializer will be automatically used by all operations, without needing to provide a transcoder each time.
240252

253+
241254
=== Creating a Custom Transcoder
255+
242256
Let’s look at a more complex example: encoding the JSON alternative, https://msgpack.org[MessagePack].
257+
243258
MessagePack is a compact binary data representation, so it should be stored with the binary Common Flag.
244259
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).
245260
So we need to write one.
@@ -248,14 +263,14 @@ Start by creating a new serializer for MessagePack. This is similar to the Gson
248263

249264
[source,java]
250265
----
251-
include::example$Transcoding.java[tag=msgpack-serializer,indent=0]
266+
include::devguide:example$java/Transcoding.java[tag=msgpack-serializer,indent=0]
252267
----
253268

254269
And now create a transcoder that uses the `MsgPackSerializer`, and sets the binary Common Flag when storing the data:
255270

256271
[source,java]
257272
----
258-
include::example$Transcoding.java[tag=msgpack-transcoder,indent=0]
273+
include::devguide:example$java/Transcoding.java[tag=msgpack-transcoder,indent=0]
259274
----
260275

261276
Note the use of `BINARY_COMPAT_FLAGS`. The other Common Flags that can be used are `JSON_COMPAT_FLAGS` and `STRING_COMPAT_FLAGS`.
@@ -265,9 +280,10 @@ Now we can use the new transcoder to seamlessly store MessagePack data in Couchb
265280

266281
[source,java]
267282
----
268-
include::example$Transcoding.java[tag=msgpack-encode,indent=0]
283+
include::devguide:example$java/Transcoding.java[tag=msgpack-encode,indent=0]
269284
----
270285

286+
271287
== Further reading
272288

273289
// * For _Common flags_, setting the data format used, see the xref:ref:data-structures.adoc#common-flags[Data Structures reference].

0 commit comments

Comments
 (0)