Skip to content

Commit 27af9da

Browse files
committed
Add tojson-document, tojson-dict, tojson-array for CBL-C
* Added tojson-document, tojson-dict, tojson-array for CBL-C * The code snippet in tojson-document and tojson-array are the proposed simplified version (DOC-9157 and DOC-9147) * Not providing the code snippet for tojson-blob as it’s not the main usecase and could lead to confusion (DOC-9149). I’m proposing the remove the entire section for this. # Conflicts: # modules/c/examples/code_snippets/main.c
1 parent 61c39c9 commit 27af9da

File tree

1 file changed

+113
-10
lines changed
  • modules/c/examples/code_snippets

1 file changed

+113
-10
lines changed

modules/c/examples/code_snippets/main.c

Lines changed: 113 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,108 @@ static void use_blob() {
524524
CBLBlob_Release(blob);
525525
}
526526

527+
static void doc_json() {
528+
CBLDatabase* db = kDatabase;
529+
530+
// tag::tojson-document[]
531+
// NOTE: No error handling, for brevity (see getting started)
532+
533+
FLString json = FLSTR("{\"id\":\"1002\",\"type\":\"hotel\",\"name\":\"Hotel Ned\",\"city\":\"Balmain\",\"country\":\"Australia\"}");
534+
535+
// Create a document and set the JSON data to the document
536+
CBLError err;
537+
CBLDocument* newDoc = CBLDocument_CreateWithID(FLSTR("hotel_1002"));
538+
CBLDocument_SetJSON(newDoc, json, &err);
539+
540+
// Save the document to the database
541+
CBLDatabase_SaveDocument(db, newDoc, &err);
542+
543+
// Release created doc after using it
544+
CBLDocument_Release(newDoc);
545+
546+
// Get the document from the database
547+
const CBLDocument* doc = CBLDatabase_GetDocument(db, FLSTR("hotel_1002"), &err);
548+
549+
// Get document body as JSON
550+
FLSliceResult docJson = CBLDocument_CreateJSON(doc);
551+
printf("Document in JSON :: %.*s\n", (int)docJson.size, (const char *)docJson.buf);
552+
553+
// Release JSON data after using it
554+
FLSliceResult_Release(docJson);
555+
556+
// Release doc read from the database after using it
557+
CBLDocument_Release(doc);
558+
// end::tojson-document[]
559+
}
560+
561+
static void dict_json() {
562+
// tag::tojson-dictionary[]
563+
// NOTE: No error handling, for brevity (see getting started)
564+
565+
FLString json = FLSTR("{\"id\":\"1002\",\"type\":\"hotel\",\"name\":\"Hotel Ned\",\"city\":\"Balmain\",\"country\":\"Australia\"}");
566+
567+
// Create a dictionary from the JSON string
568+
FLError err;
569+
FLDoc doc = FLDoc_FromJSON(json, &err);
570+
FLDict hotel = FLValue_AsDict(FLDoc_GetRoot(doc));
571+
572+
// Iterate through the dictionary
573+
FLDictIterator iter;
574+
FLDictIterator_Begin(hotel, &iter);
575+
FLValue value;
576+
while (NULL != (value = FLDictIterator_GetValue(&iter))) {
577+
FLString key = FLDictIterator_GetKeyString(&iter);
578+
FLString strValue = FLValue_AsString(value);
579+
printf("%.*s :: %.*s\n", (int)key.size, (const char*)key.buf, (int)strValue.size, (const char*)strValue.buf);
580+
FLDictIterator_Next(&iter);
581+
}
582+
583+
// Convert the dictionary to JSON
584+
FLSliceResult hotelJson = FLValue_ToJSON((FLValue)hotel);
585+
printf("Hotel in JSON :: %.*s\n", (int)hotelJson.size, (const char *)hotelJson.buf);
586+
587+
// Release JSON data after finish using it
588+
FLSliceResult_Release(hotelJson);
589+
590+
// Release doc after finish using it
591+
FLDoc_Release(doc);
592+
// end::tojson-dictionary[]
593+
}
594+
595+
static void array_json() {
596+
// tag::tojson-array[]
597+
// NOTE: No error handling, for brevity (see getting started)
598+
599+
FLString json = FLSTR("[\"Hotel Ned\", \"Hotel Ted\"]");
600+
601+
// Create an array from the JSON string
602+
FLError err;
603+
FLDoc doc = FLDoc_FromJSON(json, &err);
604+
FLArray hotels = FLValue_AsArray(FLDoc_GetRoot(doc));
605+
606+
// Iterate through the array
607+
FLArrayIterator iter;
608+
FLArrayIterator_Begin(hotels, &iter);
609+
FLValue value;
610+
while (NULL != (value = FLArrayIterator_GetValue(&iter))) {
611+
FLString hotel = FLValue_AsString(value);
612+
printf("Hotel :: %.*s\n", (int)hotel.size, (const char *)hotel.buf);
613+
FLArrayIterator_Next(&iter);
614+
}
615+
616+
// Convert the array to JSON
617+
FLSliceResult hotelsJson = FLValue_ToJSON((FLValue)hotels);
618+
printf("Hotels in JSON :: %.*s\n", (int)hotelsJson.size, (const char *)hotelsJson.buf);
619+
620+
// Release JSON data after finish using it
621+
FLSliceResult_Release(hotelsJson);
622+
623+
// Release doc after finish using it
624+
FLDoc_Release(doc);
625+
// end::tojson-array[]
626+
}
627+
628+
527629
static void create_index() {
528630
CBLDatabase* db = kDatabase;
529631

@@ -639,8 +741,8 @@ static void use_collection_contains() {
639741
CBLError err;
640742
CBLQuery* query = CBLDatabase_CreateQuery(db, kCBLN1QLLanguage,
641743
FLSTR("SELECT meta().id, name, public_likes FROM _ WHERE type = \"hotel\" "
642-
"AND ARRAY_CONTAINS(public_likes, \"Armani Langworth\""), NULL, &err);
643-
744+
"AND ARRAY_CONTAINS(public_likes, \"Armani Langworth\")"), NULL, &err);
745+
644746
CBLResultSet* results = CBLQuery_Execute(query, &err);
645747
while(CBLResultSet_Next(results)) {
646748
FLArray publicLikes = FLValue_AsArray(CBLResultSet_ValueForKey(results, FLSTR("public_likes")));
@@ -1088,11 +1190,14 @@ int main(int argc, char** argv) {
10881190
create_document();
10891191
update_document();
10901192
do_batch_operation();
1091-
use_blob();
1092-
select_meta();
1093-
1193+
// Disable use_blob() as no avatar.jpg to load and crash
1194+
// use_blob();
1195+
doc_json();
1196+
dict_json();
1197+
array_json();
10941198
load_prebuilt();
10951199
create_index();
1200+
select_meta();
10961201
select_where();
10971202
use_collection_contains();
10981203
select_like();
@@ -1119,15 +1224,13 @@ int main(int argc, char** argv) {
11191224
return 0;
11201225
}
11211226

1122-
11231227
// tag::console-logging-db[]
1124-
// Placeholder for code to increase level of console logging for kCBLLogDomainDatabase domain
1228+
// Placeholder for code to increase level of console logging for kCBLLogDomainDatabase domain (Not Applicable for C)
11251229
// end::console-logging-db[]
11261230

11271231
// tag::console-logging[]
1128-
// Placeholder for code to increase level of console logging for all domains
1232+
// Placeholder for code to increase level of console logging for all domains (Not Applicable for C)
11291233
// end::console-logging[]
11301234

11311235
// tag::date-getter[]
1132-
// Placeholder for Date accessors.
1133-
// end::date-getter[]
1236+
// Placeholder for Date accessors (Not Applicable for C)

0 commit comments

Comments
 (0)