@@ -27,14 +27,14 @@ Let's assume we have a very simple JSON structure we want to insert:
2727
2828[source,json]
2929----
30- include::example$Json.java[tag=arthur,indent=0]
30+ include::devguide: example$java/ Json.java[tag=arthur,indent=0]
3131----
3232
3333Once we've connected to a Couchbase server and the bucket, scope, and collection that we want to write to, we know that inserting is as simple as:
3434
3535[source,java]
3636----
37- include::example$Json.java[tag=upsert,indent=0]
37+ include::devguide: example$java/ Json.java[tag=upsert,indent=0]
3838----
3939
4040But how do we model that JSON object?
@@ -43,12 +43,12 @@ We mentioned the simple JSON library that we ship with the SDK, and let's look a
4343
4444[source,java]
4545----
46- include::example$Json.java[tags=jsonobject-imports,indent=0]
46+ include::devguide: example$java/ Json.java[tags=jsonobject-imports,indent=0]
4747----
4848
4949[source,java]
5050----
51- include::example$Json.java[tags=jsonobject,indent=0]
51+ include::devguide: example$java/ Json.java[tags=jsonobject,indent=0]
5252----
5353
5454=== Retrieving
@@ -58,7 +58,7 @@ In this example, we print the JSON back, and access one of the fields:
5858
5959[source,java]
6060----
61- include::example$Json.java[tags=retrieve-json,indent=0]
61+ include::devguide: example$java/ Json.java[tags=retrieve-json,indent=0]
6262----
6363
6464=== Using Java's `Map` data structure
@@ -68,15 +68,15 @@ So we can easily create an object from an existing Map:
6868
6969[source,java]
7070----
71- include::example$Json.java[tags=map,indent=0]
71+ include::devguide: example$java/ Json.java[tags=map,indent=0]
7272----
7373
7474The resulting `JsonObject` can be inserted as normal.
7575But most JSON serializers can handle simple objects like a Map already, and ours (backed by Jackson) is no different. So you can simply:
7676
7777[source,java]
7878----
79- include::example$Json.java[tags=map-insert,indent=0]
79+ include::devguide: example$java/ Json.java[tags=map-insert,indent=0]
8080----
8181
8282Once the data is in Couchbase, it is stored in exactly the same way, that is to say, as the JSON representation we started this example with!
@@ -85,7 +85,7 @@ So whether we inserted a Map or a JsonObject, we could retrieve it as a JsonObje
8585
8686[source,java]
8787----
88- include::example$Json.java[tags=retrieve-map,indent=0]
88+ include::devguide: example$java/ Json.java[tags=retrieve-map,indent=0]
8989----
9090
9191While you are free to use Map, this is capable of storing any valid Java value, including ones that can't be represented as JSON. JsonObject offers validation to make sure that only the relevant xref:#datatypes[datatypes] are stored, which will give you greater diagnostics and robustness.
@@ -96,12 +96,12 @@ If we read the contents of a file to get a JSON string, we can also inflate a Js
9696
9797[source,java]
9898----
99- include::example$Json.java[tags=file-imports,indent=0]
99+ include::devguide: example$java/ Json.java[tags=file-imports,indent=0]
100100----
101101
102102[source,java]
103103----
104- include::example$Json.java[tags=file,indent=0]
104+ include::devguide: example$java/ Json.java[tags=file,indent=0]
105105----
106106
107107This might seem a little clumsy though:
@@ -111,12 +111,12 @@ We could instead simply pass it through with the Raw JSON Transcoder:
111111
112112[source,java]
113113----
114- include::example$Json.java[tags=file-raw-imports,indent=0]
114+ include::devguide: example$java/ Json.java[tags=file-raw-imports,indent=0]
115115----
116116
117117[source,java]
118118----
119- include::example$Json.java[tags=file-raw,indent=0]
119+ include::devguide: example$java/ Json.java[tags=file-raw,indent=0]
120120----
121121
122122This approach could also be used to handle JSON created by other libraries such as Gson.
@@ -130,21 +130,21 @@ Using this simple class:
130130
131131[source,java]
132132----
133- include::example$Json.java[tags=person-class,indent=0]
133+ include::devguide: example$java/ Json.java[tags=person-class,indent=0]
134134----
135135
136136We could insert the exact same JSON as before with:
137137
138138[source,java]
139139----
140- include::example$Json.java[tags=insert-person,indent=0]
140+ include::devguide: example$java/ Json.java[tags=insert-person,indent=0]
141141----
142142
143143It's now trivial to return the data either as a JsonObject, exactly as we've done before, or indeed as a Person object:
144144
145145[source,java]
146146----
147- include::example$Json.java[tags=get-person,indent=0]
147+ include::devguide: example$java/ Json.java[tags=get-person,indent=0]
148148----
149149
150150[#ObjectMapper]
@@ -155,12 +155,12 @@ Though the SDK automatically uses your version of Jackson when it finds the libr
155155
156156[source,java]
157157----
158- include::example$Json.java[tags=object-mapper-imports,indent=0]
158+ include::devguide: example$java/ Json.java[tags=object-mapper-imports,indent=0]
159159----
160160
161161[source,java]
162162----
163- include::example$Json.java[tags=object-mapper;!object-mapper-dates,indent=0]
163+ include::devguide: example$java/ Json.java[tags=object-mapper;!object-mapper-dates,indent=0]
164164----
165165
166166With this setup, we can then configure our classes as we wish.
@@ -169,7 +169,7 @@ But the generated JSON will have the same fields as before, for easy interoperab
169169
170170[source,java]
171171----
172- include::example$Json.java[tags=persona-class;!exclude,indent=0]
172+ include::devguide: example$java/ Json.java[tags=persona-class;!exclude,indent=0]
173173----
174174
175175[#datatypes]
@@ -193,7 +193,7 @@ This example shows a variety of data types, including a nested array ("possessio
193193
194194[source,java]
195195----
196- include::example$Json.java[tags=nested,indent=0]
196+ include::devguide: example$java/ Json.java[tags=nested,indent=0]
197197----
198198
199199=== Top level values other than JsonObject
@@ -204,14 +204,14 @@ For example, to insert a string value, we could simply:
204204
205205[source,java]
206206----
207- include::example$Json.java[tags=string,indent=0]
207+ include::devguide: example$java/ Json.java[tags=string,indent=0]
208208----
209209
210210We can retrieve this value as usual, with the appropriate `.contentAs()`:
211211
212212[source,java]
213213----
214- include::example$Json.java[tags=string-get,indent=0]
214+ include::devguide: example$java/ Json.java[tags=string-get,indent=0]
215215----
216216
217217=== Dates
@@ -221,37 +221,37 @@ JSON has no built-in representation of dates, so commonly they are represented a
221221* a string-formatted https://www.w3.org/TR/NOTE-datetime[ISO-8601^] date
222222* an offset in seconds or milliseconds from the Unix epoch 1 January 1970 UTC
223223
224- While it doesn't matter which you choose (as long as you serialize and deserialize your Date object consistently!) it may make sense to store the dates in Couchbase in a format that can be easily manipulated using the xref:7.1@ server:n1ql:n1ql-language-reference/datefun.adoc[date functions in {sqlpp}].
224+ While it doesn't matter which you choose (as long as you serialize and deserialize your Date object consistently!) it may make sense to store the dates in Couchbase in a format that can be easily manipulated using the xref:server:n1ql:n1ql-language-reference/datefun.adoc[date functions in {sqlpp}].
225225
226226Handily, as we can see on the same page, the
227- xref:7.1@ server:n1ql:n1ql-language-reference$datefun.adoc#date-formats[supported date formats] are the usual convention in JSON.
227+ xref:server:n1ql:n1ql-language-reference$datefun.adoc#date-formats[supported date formats] are the usual convention in JSON.
228228
229229Let's look at a brief example of how we might implement this, to serialize a new Event class:
230230
231231[source,java]
232232----
233- include::example$Json.java[tags=event-class;!exclude,indent=0]
233+ include::devguide: example$java/ Json.java[tags=event-class;!exclude,indent=0]
234234----
235235
236236We'll need a few extra imports:
237237
238238[source,java]
239239----
240- include::example$Json.java[tags=time-imports,indent=0]
240+ include::devguide: example$java/ Json.java[tags=time-imports,indent=0]
241241----
242242
243243As we are controlling the serialization carefully, we'll also want to register our own <<ObjectMapper,ObjectMapper>> as above, and additionally configure some of the date handling properties:
244244
245245[source,java]
246246----
247- include::example$Json.java[tags=object-mapper-dates,indent=0]
247+ include::devguide: example$java/ Json.java[tags=object-mapper-dates,indent=0]
248248----
249249
250250Now all this is set up we can simply serialize and deserialize our Event objects exactly as we've done before:
251251
252252[source,java]
253253----
254- include::example$Json.java[tags=event,indent=0]
254+ include::devguide: example$java/ Json.java[tags=event,indent=0]
255255----
256256
257257
@@ -262,24 +262,24 @@ So taking our original "Arthur" object above, we can insert a simple value:
262262
263263[source,java]
264264----
265- include::example$Json.java[tags=subdoc-simple,indent=0]
265+ include::devguide: example$java/ Json.java[tags=subdoc-simple,indent=0]
266266----
267267
268268[source,json]
269269----
270- include::example$Json.java[tags=subdoc-simple-json,indent=0]
270+ include::devguide: example$java/ Json.java[tags=subdoc-simple-json,indent=0]
271271----
272272
273273Or we could insert another nested object:
274274
275275[source,java]
276276----
277- include::example$Json.java[tags=subdoc-object,indent=0]
277+ include::devguide: example$java/ Json.java[tags=subdoc-object,indent=0]
278278----
279279
280280[source,json]
281281----
282- include::example$Json.java[tags=subdoc-object-json,indent=0]
282+ include::devguide: example$java/ Json.java[tags=subdoc-object-json,indent=0]
283283----
284284
285285NOTE: Currently, paths cannot exceed 1024 characters, and cannot be more than 32 levels deep.
@@ -296,15 +296,15 @@ We can now convert the result to Jackson's `JsonNode`, and use its rich set of m
296296
297297[source,java]
298298----
299- include::example$Json.java[tags=identify-imports,indent=0]
299+ include::devguide: example$java/ Json.java[tags=identify-imports,indent=0]
300300----
301301
302302[source,java]
303303----
304- include::example$Json.java[tags=identify,indent=0]
304+ include::devguide: example$java/ Json.java[tags=identify,indent=0]
305305----
306306
307307
308308== Additional Resources
309309
310- * Read more about the xref:7.1@ server:learn:data/data.adoc[Couchbase Data Model].
310+ * Read more about the xref:server:learn:data/data.adoc[Couchbase Data Model].
0 commit comments