Skip to content

Commit

Permalink
DOC-8064-C2 -- JSON Results (post-feedback) (couchbase#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibsoln committed Aug 6, 2021
1 parent 512b10f commit 3e768a4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ To retrieve the results of your query, you need to execute it using `Query.exec
The output from the execution is an array, with each array element representing the data from a document that matched your search criteria.

To unpack the results you need to iterate through this array.
Alternatively, you can convert the result to a JSON string -- see:


[#lbl-acc-all]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
// Inclusion block
[#ex-json]
.Using JSON Results

.Caption
[#label]
:param-tags: query-access-json
:param-leader: pass:q,a[Use {url-api-method-result-toJSON} to transform your result string into a JSON string, which can easily be serialized or used as required in your application. See <<ex-json>> for a working example.]
include::{root-partials}_block_tabbed_code_example.adoc[]
:param-tags!:
<.> Get the Query result as a JSON string -- this example assumes your query SELECTed specific properties.
<.> Get the Query result as a JSON string -- this example assumes your query SELECTed ALL
<.> Get the Query result as a JSON string -- see <<ex-json-format>>
<.> Get a native object from the JSON string
<.> Populate your custom object from the dictionary created from JSON data

// end inclusion block
.JSON String Format
[#ex-json-format]
If your query selects ALL then the JSON format will be:

[source, JSON]
----
{
database-name: {
key1: "value1",
keyx: "valuex"
}
}
----

If your query selects a sub-set of available properties then the JSON format will be:

[source, JSON]
----
{
key1: "value1",
keyx: "valuex"
}
----
Original file line number Diff line number Diff line change
Expand Up @@ -2642,10 +2642,6 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException {

// Get result as JSON string
String thisJsonString = result.toJSON(); // <.>
// ALTERNATIVELY
String thisJsonString =
result.getDictionary(0).toJSON(); // <.>


// Get Java Hashmap from JSON string
HashMap<String, Object> dictFromJSONstring =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2493,9 +2493,6 @@ class ExamplesP2p(private val context: Context) {

// Get result as JSON string
val thisJsonString1: String = result.toJSON() // <.>
// ALTERNATIVELY
val thisJsonString = result.getDictionary(0)?.toJSON() // <.>


// Get Hashmap from JSON string
val dictFromJSONstring =
Expand Down
4 changes: 1 addition & 3 deletions modules/csharp/examples/code_snippets/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1717,9 +1717,7 @@ public static void testQuerySyntaxAll()
foreach (var result in query.Execute().AsEnumerable()) {

// get the result into a JSON String
var thisDocsJSONString = result.GetDictionary(dbName).ToJSON();// <.>
// ALTERNATIVELY
thisDocsJSONString = result.GetDictionary(0).ToJSON();// <.>
var thisDocsJSONString = result.ToJSON();// <.>

// Get a native dictionary object using the JSON string
var dictFromJSONstring =
Expand Down
6 changes: 1 addition & 5 deletions modules/java/examples/code_snippets/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -2980,12 +2980,8 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException {

// Get result as JSON string
String thisJsonString = result.toJSON(); // <.>
// ALTERNATIVELY
String thisJsonString =
result.getDictionary(0).toJSON(); // <.>


// Get Java Hashmap from JSON string
// Get Java Hashmap from JSON string
HashMap<String, Object> dictFromJSONstring =
mapper.readValue(thisJsonString, HashMap.class); // <.>

Expand Down
3 changes: 0 additions & 3 deletions modules/objc/examples/code_snippets/SampleCodeTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1642,9 +1642,6 @@ - (void) dontTestQuerySyntaxJson {

NSString* thisJsonString =
[result toJSON]; // <.>
// ALTERNATIVELY
NSString* thisJsonString =
[[result valueAtIndex:0 ] toJSON]; // <.>

// Get an native Obj-C object from the Json String
NSDictionary *thisDictFromJSON =
Expand Down
12 changes: 5 additions & 7 deletions modules/swift/examples/code_snippets/SampleCodeTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ class SampleCodeTest {
}



func dontTestToJson-Blob() throws {
database = self.db
// demonstrate use of JSON string
Expand Down Expand Up @@ -2275,11 +2276,8 @@ class Query {
var results = try! listQuery.execute()
for row in results {

// If Query selected ALL,
// unpack items from encompassing dictionary // <.>
let jsonString = row.dictionary(at: 0)!.toJSON()
// ALTERNATIVELY: If Query selected specific items
let jsonString = row.toJSON()
// get the result into a JSON String
let jsonString = row.toJSON() // <.>

let thisJsonObj:Dictionary =
try! (JSONSerialization.jsonObject(
Expand All @@ -2288,13 +2286,13 @@ class Query {
as? [String: Any])! // <.>

// Use Json Object to populate Native object
// Use Codable class to unpack JSON data to native object // <.>
// Use Codable class to unpack JSON data to native object
let this_hotel:Hotel =
(try JSONDecoder().decode(
Hotel.self,
from: jsonString.data(using: .utf8)!
)
)
) // <.>

// ALTERNATIVELY unpack in steps
this_hotel.id = thisJsonObj["id"] as! String
Expand Down

0 comments on commit 3e768a4

Please sign in to comment.