83 #include "../../json/json.h"
87 static const char* cpMakeFileName(
char* cpBuffer,
const char* cpBase,
const char* cpDivider,
const char* cpName){
88 strcpy(cpBuffer, cpBase);
89 strcat(cpBuffer, cpDivider);
90 strcat(cpBuffer, cpName);
94 static char s_caBuf[PATH_MAX];
95 static char s_caBufOut[5*PATH_MAX];
97 static char* s_cpDescription =
98 "Illustrate the JSON object for parsing and building JSON files.";
100 static char* s_cppCases[] = {
101 "Display application information.",
102 "Illustrate a simple case of reading and parsing a JSON file.",
103 "Illustrate finding keys in the tree of JSON values.",
104 "Illustrate walking a sub-tree and the siblings of a sub-root explicitly with the iterator.",
105 "Illustrate writing a JSON file from a value tree of parsed JSON values.",
106 "Illustrate building a JSON file.",
108 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
110 static int iHelp(
void){
113 printf(
"description: %s\n", s_cpDescription);
114 printf(
" usage: ex-api arg\n");
115 printf(
" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
116 printf(
" execute case number n\n");
117 printf(
" arg = anthing else\n");
118 printf(
" print this help screen\n");
120 for(; i < s_iCaseCount; i++){
121 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
136 static int iSimpleParse() {
137 int iReturn = EXIT_SUCCESS;
138 static void* vpMem = NULL;
139 static void* vpJson = NULL;
148 const char* cpInput = cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"json-parse.json");
152 "This example case illustrates a simple parse and display of a JSON file.\n"
153 "The file has many non-printing ASCII characters.\n";
154 printf(
"\n%s", cpHeader);
160 printf(
"\nThe Input File (with line numbers)\n");
164 printf(
"\nThe JSON Values\n");
171 iReturn = EXIT_FAILURE;
180 static int iFindKeys() {
181 int iReturn = EXIT_SUCCESS;
182 static void* vpMem = NULL;
183 static void* vpJson = NULL;
184 json_value* spRoot, *spValue;
193 const char* cpInput = cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"json-parse.json");
197 "This example case illustrates finding keys in a parsed JSON file.\n";
198 printf(
"\n%s", cpHeader);
205 printf(
"\nThe Input File (with line numbers)\n");
209 printf(
"\nFind the \"numbers\" Key\n");
218 XTHROW(&e,
"numbers key not found");
222 printf(
"\nFind the \"ctrl\" Keys\n");
231 XTHROW(&e,
"ctrl key not found");
235 printf(
"\nFind the Non-ASCII Key\n");
245 XTHROW(&e,
"non-ascii key not found");
251 iReturn = EXIT_FAILURE;
260 static int iWalker() {
261 int iReturn = EXIT_SUCCESS;
262 static void* vpMem = NULL;
263 static void* vpJson = NULL;
264 json_value* spRoot, *spValue;
265 void* vpIn, *vpIt, *vpIt2;
272 const char* cpInput = cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"json-parse.json");
276 "This example case illustrates walking a tree, depth-first from any value as root\n"
277 "or horizontally across children of a specific node.\n";
278 printf(
"\n%s", cpHeader);
285 printf(
"\nThe Input File (with line numbers)\n");
289 printf(
"\nWalk the Children of the Root Node\n");
298 XTHROW(&e,
"no children of the root node found");
302 printf(
"\nWalk the Sub-Tree of the \"unsigned\" Node as the Root Node\n");
312 XTHROW(&e,
"signed key not found");
318 iReturn = EXIT_FAILURE;
327 static int iWriter() {
328 int iReturn = EXIT_SUCCESS;
329 static void* vpMem = NULL;
330 static void* vpJson = NULL;
331 json_value* spRoot, *spValue;
339 const char* cpaOutFile[] = {
340 cpMakeFileName(&s_caBufOut[0], SOURCE_DIR,
"/../output/",
"text.json"),
341 cpMakeFileName(&s_caBufOut[PATH_MAX], SOURCE_DIR,
"/../output/",
"unicode.json"),
342 cpMakeFileName(&s_caBufOut[2*PATH_MAX], SOURCE_DIR,
"/../output/",
"numbers.json"),
343 cpMakeFileName(&s_caBufOut[3*PATH_MAX], SOURCE_DIR,
"/../output/",
"odd.json"),
344 cpMakeFileName(&s_caBufOut[4*PATH_MAX], SOURCE_DIR,
"/../output/",
"root.json"),
355 const char* cpInput = cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"json-parse.json");
359 "This example case illustrates writing JSON files from trees of values.\n"
360 "JSON files are generated for a series of tree values a root node.\n"
361 "The generated files are written in the current working directory.\n";
362 printf(
"\n%s", cpHeader);
369 printf(
"\nThe Input File (with line numbers)\n");
372 printf(
"\nWrite JSON Files\n");
373 printf(
"For the JSON file with the named key as root node view these files.\n");
374 for(ui = 0; ui < 4; ui++){
377 XTHROW(&e,
"expected to find key");
381 XTHROW(&e,
"expected to get value");
383 ucpOutput =
ucpJsonWrite(vpJson, spValue, &uiOutputLen);
385 printf(
"%s\n", cpaOutFile[ui]);
389 printf(
"\nThe root node\n%s\n", cpaOutFile[4]);
394 iReturn = EXIT_FAILURE;
403 static int iBuilder() {
404 int iReturn = EXIT_SUCCESS;
405 static void* vpMem = NULL;
406 static void* vpBld = NULL;
407 static void* vpJson = NULL;
413 const char* cpFileName;
415 file_def saFiles[] = {
416 {0,
"single", cpMakeFileName(&s_caBufOut[0], SOURCE_DIR,
"/../output/",
"builder-single-value.json")},
417 {1,
"text", cpMakeFileName(&s_caBufOut[PATH_MAX], SOURCE_DIR,
"/../output/",
"builder-text.json")},
418 {2,
"unicode", cpMakeFileName(&s_caBufOut[2*PATH_MAX], SOURCE_DIR,
"/../output/",
"builder-unicode.json")},
419 {3,
"numbers", cpMakeFileName(&s_caBufOut[3*PATH_MAX], SOURCE_DIR,
"/../output/",
"builder-numbers.json")},
420 {4,
"root", cpMakeFileName(&s_caBufOut[4*PATH_MAX], SOURCE_DIR,
"/../output/",
"builder-root.json")},
424 aint uiSingle, uiText, uiUnicode, uiNumbers, uiRoot, uiTemp;
435 "This example case illustrates building JSON files from scratch.\n"
436 "For simple ASCII files, this is most easily done with a text editor.\n"
437 "However, when working with Unicode data a more general method is needed.\n"
438 "This JSON builder works by creating root nodes (objects or arrays) and adding children to them.\n";
439 printf(
"\n%s", cpHeader);
441 printf(
"\nBuild a single-value JSON file.\n");
445 ucpOutput =
ucpJsonWrite(vpJson, spValue, &uiOutputLen);
446 vUtilFileWrite(vpMem, saFiles[0].cpFileName, ucpOutput, uiOutputLen);
447 printf(
"%s node written to file %s\n", saFiles[0].cpKey, saFiles[0].cpFileName);
450 printf(
"\nBuild the text node.\n");
461 ucpOutput =
ucpJsonWrite(vpJson, spValue, &uiOutputLen);
462 vUtilFileWrite(vpMem, saFiles[1].cpFileName, ucpOutput, uiOutputLen);
463 printf(
"%s node written to file %s\n", saFiles[1].cpKey, saFiles[1].cpFileName);
467 printf(
"\nBuild the unicode node.\n");
478 uint32_t uiaBuf[] = {255, 939, 10348};
484 ucpOutput =
ucpJsonWrite(vpJson, spValue, &uiOutputLen);
485 vUtilFileWrite(vpMem, saFiles[2].cpFileName, ucpOutput, uiOutputLen);
486 printf(
"%s node written to file %s\n", saFiles[2].cpKey, saFiles[2].cpFileName);
490 printf(
"\nBuild the numbers node.\n");
513 ucpOutput =
ucpJsonWrite(vpJson, spValue, &uiOutputLen);
514 vUtilFileWrite(vpMem, saFiles[3].cpFileName, ucpOutput, uiOutputLen);
515 printf(
"%s node written to file %s\n", saFiles[3].cpKey, saFiles[3].cpFileName);
519 printf(
"\nAdd all keyed nodes to a single, parent root.\n");
527 ucpOutput =
ucpJsonWrite(vpJson, spValue, &uiOutputLen);
528 vUtilFileWrite(vpMem, saFiles[4].cpFileName, ucpOutput, uiOutputLen);
529 printf(
"%s node written to file %s\n", saFiles[4].cpKey, saFiles[4].cpFileName);
536 iReturn = EXIT_FAILURE;
552 int main(
int argc,
char **argv) {
555 iCase = atol(argv[1]);
557 if((iCase > 0) && (iCase <= s_iCaseCount)){
558 printf(
"%s\n", s_cppCases[iCase -1]);
564 return iSimpleParse();