@@ -524,6 +524,108 @@ static void use_blob() {
524
524
CBLBlob_Release (blob );
525
525
}
526
526
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
+
527
629
static void create_index () {
528
630
CBLDatabase * db = kDatabase ;
529
631
@@ -639,8 +741,8 @@ static void use_collection_contains() {
639
741
CBLError err ;
640
742
CBLQuery * query = CBLDatabase_CreateQuery (db , kCBLN1QLLanguage ,
641
743
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
+
644
746
CBLResultSet * results = CBLQuery_Execute (query , & err );
645
747
while (CBLResultSet_Next (results )) {
646
748
FLArray publicLikes = FLValue_AsArray (CBLResultSet_ValueForKey (results , FLSTR ("public_likes" )));
@@ -1088,11 +1190,14 @@ int main(int argc, char** argv) {
1088
1190
create_document ();
1089
1191
update_document ();
1090
1192
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 ();
1094
1198
load_prebuilt ();
1095
1199
create_index ();
1200
+ select_meta ();
1096
1201
select_where ();
1097
1202
use_collection_contains ();
1098
1203
select_like ();
@@ -1119,15 +1224,13 @@ int main(int argc, char** argv) {
1119
1224
return 0 ;
1120
1225
}
1121
1226
1122
-
1123
1227
// 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)
1125
1229
// end::console-logging-db[]
1126
1230
1127
1231
// 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)
1129
1233
// end::console-logging[]
1130
1234
1131
1235
// tag::date-getter[]
1132
- // Placeholder for Date accessors.
1133
- // end::date-getter[]
1236
+ // Placeholder for Date accessors (Not Applicable for C)
0 commit comments