19750e409Sopenharmony_ci/* 29750e409Sopenharmony_ci Copyright (c) 2009-2017 Dave Gamble and cJSON contributors 39750e409Sopenharmony_ci 49750e409Sopenharmony_ci Permission is hereby granted, free of charge, to any person obtaining a copy 59750e409Sopenharmony_ci of this software and associated documentation files (the "Software"), to deal 69750e409Sopenharmony_ci in the Software without restriction, including without limitation the rights 79750e409Sopenharmony_ci to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 89750e409Sopenharmony_ci copies of the Software, and to permit persons to whom the Software is 99750e409Sopenharmony_ci furnished to do so, subject to the following conditions: 109750e409Sopenharmony_ci 119750e409Sopenharmony_ci The above copyright notice and this permission notice shall be included in 129750e409Sopenharmony_ci all copies or substantial portions of the Software. 139750e409Sopenharmony_ci 149750e409Sopenharmony_ci THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 159750e409Sopenharmony_ci IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 169750e409Sopenharmony_ci FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 179750e409Sopenharmony_ci AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 189750e409Sopenharmony_ci LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 199750e409Sopenharmony_ci OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 209750e409Sopenharmony_ci THE SOFTWARE. 219750e409Sopenharmony_ci*/ 229750e409Sopenharmony_ci 239750e409Sopenharmony_ci#include <stdio.h> 249750e409Sopenharmony_ci#include <stdlib.h> 259750e409Sopenharmony_ci#include <string.h> 269750e409Sopenharmony_ci#include "cJSON.h" 279750e409Sopenharmony_ci 289750e409Sopenharmony_ci/* Used by some code below as an example datatype. */ 299750e409Sopenharmony_cistruct record 309750e409Sopenharmony_ci{ 319750e409Sopenharmony_ci const char *precision; 329750e409Sopenharmony_ci double lat; 339750e409Sopenharmony_ci double lon; 349750e409Sopenharmony_ci const char *address; 359750e409Sopenharmony_ci const char *city; 369750e409Sopenharmony_ci const char *state; 379750e409Sopenharmony_ci const char *zip; 389750e409Sopenharmony_ci const char *country; 399750e409Sopenharmony_ci}; 409750e409Sopenharmony_ci 419750e409Sopenharmony_ci 429750e409Sopenharmony_ci/* Create a bunch of objects as demonstration. */ 439750e409Sopenharmony_cistatic int print_preallocated(cJSON *root) 449750e409Sopenharmony_ci{ 459750e409Sopenharmony_ci /* declarations */ 469750e409Sopenharmony_ci char *out = NULL; 479750e409Sopenharmony_ci char *buf = NULL; 489750e409Sopenharmony_ci char *buf_fail = NULL; 499750e409Sopenharmony_ci size_t len = 0; 509750e409Sopenharmony_ci size_t len_fail = 0; 519750e409Sopenharmony_ci 529750e409Sopenharmony_ci /* formatted print */ 539750e409Sopenharmony_ci out = cJSON_Print(root); 549750e409Sopenharmony_ci 559750e409Sopenharmony_ci /* create buffer to succeed */ 569750e409Sopenharmony_ci /* the extra 5 bytes are because of inaccuracies when reserving memory */ 579750e409Sopenharmony_ci len = strlen(out) + 5; 589750e409Sopenharmony_ci buf = (char*)malloc(len); 599750e409Sopenharmony_ci if (buf == NULL) 609750e409Sopenharmony_ci { 619750e409Sopenharmony_ci printf("Failed to allocate memory.\n"); 629750e409Sopenharmony_ci exit(1); 639750e409Sopenharmony_ci } 649750e409Sopenharmony_ci 659750e409Sopenharmony_ci /* create buffer to fail */ 669750e409Sopenharmony_ci len_fail = strlen(out); 679750e409Sopenharmony_ci buf_fail = (char*)malloc(len_fail); 689750e409Sopenharmony_ci if (buf_fail == NULL) 699750e409Sopenharmony_ci { 709750e409Sopenharmony_ci printf("Failed to allocate memory.\n"); 719750e409Sopenharmony_ci exit(1); 729750e409Sopenharmony_ci } 739750e409Sopenharmony_ci 749750e409Sopenharmony_ci /* Print to buffer */ 759750e409Sopenharmony_ci if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) { 769750e409Sopenharmony_ci printf("cJSON_PrintPreallocated failed!\n"); 779750e409Sopenharmony_ci if (strcmp(out, buf) != 0) { 789750e409Sopenharmony_ci printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n"); 799750e409Sopenharmony_ci printf("cJSON_Print result:\n%s\n", out); 809750e409Sopenharmony_ci printf("cJSON_PrintPreallocated result:\n%s\n", buf); 819750e409Sopenharmony_ci } 829750e409Sopenharmony_ci free(out); 839750e409Sopenharmony_ci free(buf_fail); 849750e409Sopenharmony_ci free(buf); 859750e409Sopenharmony_ci return -1; 869750e409Sopenharmony_ci } 879750e409Sopenharmony_ci 889750e409Sopenharmony_ci /* success */ 899750e409Sopenharmony_ci printf("%s\n", buf); 909750e409Sopenharmony_ci 919750e409Sopenharmony_ci /* force it to fail */ 929750e409Sopenharmony_ci if (cJSON_PrintPreallocated(root, buf_fail, (int)len_fail, 1)) { 939750e409Sopenharmony_ci printf("cJSON_PrintPreallocated failed to show error with insufficient memory!\n"); 949750e409Sopenharmony_ci printf("cJSON_Print result:\n%s\n", out); 959750e409Sopenharmony_ci printf("cJSON_PrintPreallocated result:\n%s\n", buf_fail); 969750e409Sopenharmony_ci free(out); 979750e409Sopenharmony_ci free(buf_fail); 989750e409Sopenharmony_ci free(buf); 999750e409Sopenharmony_ci return -1; 1009750e409Sopenharmony_ci } 1019750e409Sopenharmony_ci 1029750e409Sopenharmony_ci free(out); 1039750e409Sopenharmony_ci free(buf_fail); 1049750e409Sopenharmony_ci free(buf); 1059750e409Sopenharmony_ci return 0; 1069750e409Sopenharmony_ci} 1079750e409Sopenharmony_ci 1089750e409Sopenharmony_ci/* Create a bunch of objects as demonstration. */ 1099750e409Sopenharmony_cistatic void create_objects(void) 1109750e409Sopenharmony_ci{ 1119750e409Sopenharmony_ci /* declare a few. */ 1129750e409Sopenharmony_ci cJSON *root = NULL; 1139750e409Sopenharmony_ci cJSON *fmt = NULL; 1149750e409Sopenharmony_ci cJSON *img = NULL; 1159750e409Sopenharmony_ci cJSON *thm = NULL; 1169750e409Sopenharmony_ci cJSON *fld = NULL; 1179750e409Sopenharmony_ci int i = 0; 1189750e409Sopenharmony_ci 1199750e409Sopenharmony_ci /* Our "days of the week" array: */ 1209750e409Sopenharmony_ci const char *strings[7] = 1219750e409Sopenharmony_ci { 1229750e409Sopenharmony_ci "Sunday", 1239750e409Sopenharmony_ci "Monday", 1249750e409Sopenharmony_ci "Tuesday", 1259750e409Sopenharmony_ci "Wednesday", 1269750e409Sopenharmony_ci "Thursday", 1279750e409Sopenharmony_ci "Friday", 1289750e409Sopenharmony_ci "Saturday" 1299750e409Sopenharmony_ci }; 1309750e409Sopenharmony_ci /* Our matrix: */ 1319750e409Sopenharmony_ci int numbers[3][3] = 1329750e409Sopenharmony_ci { 1339750e409Sopenharmony_ci {0, -1, 0}, 1349750e409Sopenharmony_ci {1, 0, 0}, 1359750e409Sopenharmony_ci {0 ,0, 1} 1369750e409Sopenharmony_ci }; 1379750e409Sopenharmony_ci /* Our "gallery" item: */ 1389750e409Sopenharmony_ci int ids[4] = { 116, 943, 234, 38793 }; 1399750e409Sopenharmony_ci /* Our array of "records": */ 1409750e409Sopenharmony_ci struct record fields[2] = 1419750e409Sopenharmony_ci { 1429750e409Sopenharmony_ci { 1439750e409Sopenharmony_ci "zip", 1449750e409Sopenharmony_ci 37.7668, 1459750e409Sopenharmony_ci -1.223959e+2, 1469750e409Sopenharmony_ci "", 1479750e409Sopenharmony_ci "SAN FRANCISCO", 1489750e409Sopenharmony_ci "CA", 1499750e409Sopenharmony_ci "94107", 1509750e409Sopenharmony_ci "US" 1519750e409Sopenharmony_ci }, 1529750e409Sopenharmony_ci { 1539750e409Sopenharmony_ci "zip", 1549750e409Sopenharmony_ci 37.371991, 1559750e409Sopenharmony_ci -1.22026e+2, 1569750e409Sopenharmony_ci "", 1579750e409Sopenharmony_ci "SUNNYVALE", 1589750e409Sopenharmony_ci "CA", 1599750e409Sopenharmony_ci "94085", 1609750e409Sopenharmony_ci "US" 1619750e409Sopenharmony_ci } 1629750e409Sopenharmony_ci }; 1639750e409Sopenharmony_ci volatile double zero = 0.0; 1649750e409Sopenharmony_ci 1659750e409Sopenharmony_ci /* Here we construct some JSON standards, from the JSON site. */ 1669750e409Sopenharmony_ci 1679750e409Sopenharmony_ci /* Our "Video" datatype: */ 1689750e409Sopenharmony_ci root = cJSON_CreateObject(); 1699750e409Sopenharmony_ci cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); 1709750e409Sopenharmony_ci cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject()); 1719750e409Sopenharmony_ci cJSON_AddStringToObject(fmt, "type", "rect"); 1729750e409Sopenharmony_ci cJSON_AddNumberToObject(fmt, "width", 1920); 1739750e409Sopenharmony_ci cJSON_AddNumberToObject(fmt, "height", 1080); 1749750e409Sopenharmony_ci cJSON_AddFalseToObject (fmt, "interlace"); 1759750e409Sopenharmony_ci cJSON_AddNumberToObject(fmt, "frame rate", 24); 1769750e409Sopenharmony_ci 1779750e409Sopenharmony_ci /* Print to text */ 1789750e409Sopenharmony_ci if (print_preallocated(root) != 0) { 1799750e409Sopenharmony_ci cJSON_Delete(root); 1809750e409Sopenharmony_ci exit(EXIT_FAILURE); 1819750e409Sopenharmony_ci } 1829750e409Sopenharmony_ci cJSON_Delete(root); 1839750e409Sopenharmony_ci 1849750e409Sopenharmony_ci /* Our "days of the week" array: */ 1859750e409Sopenharmony_ci root = cJSON_CreateStringArray(strings, 7); 1869750e409Sopenharmony_ci 1879750e409Sopenharmony_ci if (print_preallocated(root) != 0) { 1889750e409Sopenharmony_ci cJSON_Delete(root); 1899750e409Sopenharmony_ci exit(EXIT_FAILURE); 1909750e409Sopenharmony_ci } 1919750e409Sopenharmony_ci cJSON_Delete(root); 1929750e409Sopenharmony_ci 1939750e409Sopenharmony_ci /* Our matrix: */ 1949750e409Sopenharmony_ci root = cJSON_CreateArray(); 1959750e409Sopenharmony_ci for (i = 0; i < 3; i++) 1969750e409Sopenharmony_ci { 1979750e409Sopenharmony_ci cJSON_AddItemToArray(root, cJSON_CreateIntArray(numbers[i], 3)); 1989750e409Sopenharmony_ci } 1999750e409Sopenharmony_ci 2009750e409Sopenharmony_ci /* cJSON_ReplaceItemInArray(root, 1, cJSON_CreateString("Replacement")); */ 2019750e409Sopenharmony_ci 2029750e409Sopenharmony_ci if (print_preallocated(root) != 0) { 2039750e409Sopenharmony_ci cJSON_Delete(root); 2049750e409Sopenharmony_ci exit(EXIT_FAILURE); 2059750e409Sopenharmony_ci } 2069750e409Sopenharmony_ci cJSON_Delete(root); 2079750e409Sopenharmony_ci 2089750e409Sopenharmony_ci /* Our "gallery" item: */ 2099750e409Sopenharmony_ci root = cJSON_CreateObject(); 2109750e409Sopenharmony_ci cJSON_AddItemToObject(root, "Image", img = cJSON_CreateObject()); 2119750e409Sopenharmony_ci cJSON_AddNumberToObject(img, "Width", 800); 2129750e409Sopenharmony_ci cJSON_AddNumberToObject(img, "Height", 600); 2139750e409Sopenharmony_ci cJSON_AddStringToObject(img, "Title", "View from 15th Floor"); 2149750e409Sopenharmony_ci cJSON_AddItemToObject(img, "Thumbnail", thm = cJSON_CreateObject()); 2159750e409Sopenharmony_ci cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943"); 2169750e409Sopenharmony_ci cJSON_AddNumberToObject(thm, "Height", 125); 2179750e409Sopenharmony_ci cJSON_AddStringToObject(thm, "Width", "100"); 2189750e409Sopenharmony_ci cJSON_AddItemToObject(img, "IDs", cJSON_CreateIntArray(ids, 4)); 2199750e409Sopenharmony_ci 2209750e409Sopenharmony_ci if (print_preallocated(root) != 0) { 2219750e409Sopenharmony_ci cJSON_Delete(root); 2229750e409Sopenharmony_ci exit(EXIT_FAILURE); 2239750e409Sopenharmony_ci } 2249750e409Sopenharmony_ci cJSON_Delete(root); 2259750e409Sopenharmony_ci 2269750e409Sopenharmony_ci /* Our array of "records": */ 2279750e409Sopenharmony_ci root = cJSON_CreateArray(); 2289750e409Sopenharmony_ci for (i = 0; i < 2; i++) 2299750e409Sopenharmony_ci { 2309750e409Sopenharmony_ci cJSON_AddItemToArray(root, fld = cJSON_CreateObject()); 2319750e409Sopenharmony_ci cJSON_AddStringToObject(fld, "precision", fields[i].precision); 2329750e409Sopenharmony_ci cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat); 2339750e409Sopenharmony_ci cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon); 2349750e409Sopenharmony_ci cJSON_AddStringToObject(fld, "Address", fields[i].address); 2359750e409Sopenharmony_ci cJSON_AddStringToObject(fld, "City", fields[i].city); 2369750e409Sopenharmony_ci cJSON_AddStringToObject(fld, "State", fields[i].state); 2379750e409Sopenharmony_ci cJSON_AddStringToObject(fld, "Zip", fields[i].zip); 2389750e409Sopenharmony_ci cJSON_AddStringToObject(fld, "Country", fields[i].country); 2399750e409Sopenharmony_ci } 2409750e409Sopenharmony_ci 2419750e409Sopenharmony_ci /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root, 1), "City", cJSON_CreateIntArray(ids, 4)); */ 2429750e409Sopenharmony_ci 2439750e409Sopenharmony_ci if (print_preallocated(root) != 0) { 2449750e409Sopenharmony_ci cJSON_Delete(root); 2459750e409Sopenharmony_ci exit(EXIT_FAILURE); 2469750e409Sopenharmony_ci } 2479750e409Sopenharmony_ci cJSON_Delete(root); 2489750e409Sopenharmony_ci 2499750e409Sopenharmony_ci root = cJSON_CreateObject(); 2509750e409Sopenharmony_ci cJSON_AddNumberToObject(root, "number", 1.0 / zero); 2519750e409Sopenharmony_ci 2529750e409Sopenharmony_ci if (print_preallocated(root) != 0) { 2539750e409Sopenharmony_ci cJSON_Delete(root); 2549750e409Sopenharmony_ci exit(EXIT_FAILURE); 2559750e409Sopenharmony_ci } 2569750e409Sopenharmony_ci cJSON_Delete(root); 2579750e409Sopenharmony_ci} 2589750e409Sopenharmony_ci 2599750e409Sopenharmony_ciint CJSON_CDECL main(void) 2609750e409Sopenharmony_ci{ 2619750e409Sopenharmony_ci /* print the version */ 2629750e409Sopenharmony_ci printf("Version: %s\n", cJSON_Version()); 2639750e409Sopenharmony_ci 2649750e409Sopenharmony_ci /* Now some samplecode for building objects concisely: */ 2659750e409Sopenharmony_ci create_objects(); 2669750e409Sopenharmony_ci 2679750e409Sopenharmony_ci return 0; 2689750e409Sopenharmony_ci} 269