int main(int argc, char **argv) { FILE *jsonfile; FILE *schemafile; WJReader readjson; WJReader readschema; WJElement json; WJElement schema; XplBool succ; char *format; if(argc != 3 && argc != 4) { printf("usage:\n"); printf("\t%s <json-file> <schema-file>\n", argv[0]); printf("\t%s <json-file> <schema-file> <schema-pattern>\n", argv[0]); printf("<schema-pattern>: \"path/to/%%s.json\" additional schemas\n"); return 255; } if(!(jsonfile = fopen(argv[1], "r"))) { fprintf(stderr, "json file not found: '%s'\n", argv[1]); return 1; } if(!(schemafile = fopen(argv[2], "r"))) { fprintf(stderr, "schema file not found: '%s'\n", argv[2]); return 2; } if(argc == 4) { format = argv[3]; } else { format = NULL; } if(!(readjson = WJROpenFILEDocument(jsonfile, NULL, 0)) || !(json = WJEOpenDocument(readjson, NULL, NULL, NULL))) { fprintf(stderr, "json could not be read.\n"); return 3; } if(!(readschema = WJROpenFILEDocument(schemafile, NULL, 0)) || !(schema = WJEOpenDocument(readschema, NULL, NULL, NULL))) { fprintf(stderr, "schema could not be read.\n"); WJECloseDocument(json); return 4; } WJEDump(json); printf("json: %s\n", readjson->depth ? "bad" : "good"); WJEDump(schema); printf("schema: %s\n", readschema->depth ? "bad" : "good"); if(WJESchemaValidate(schema, json, schema_error, schema_load, NULL, format)) { printf("validation: PASS\n"); } else { printf("validation: FAIL\n"); } WJECloseDocument(json); WJECloseDocument(schema); return 0; }
int main(int argc, char **argv) { WJElement doc = NULL; WJElement person = NULL; WJElement cameo = NULL; doc = WJEObject(NULL, NULL, WJE_NEW); WJEString(doc, "name", WJE_SET, "Serenity"); WJEString(doc, "class", WJE_SET, "firefly"); WJEArray(doc, "crew", WJE_SET); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Malcolm Reynolds"); WJEString(doc, "crew[-1].job", WJE_SET, "captain"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2468); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Kaywinnet Lee Fry"); WJEString(doc, "crew[-1].job", WJE_SET, "mechanic"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2494); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Jayne Cobb"); WJEString(doc, "crew[-1].job", WJE_SET, "public relations"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2485); WJEArray(doc, "cameo", WJE_SET); WJEString(doc, "cameo[$]", WJE_NEW, "Battlestar Galactica"); WJEString(doc, "cameo[$]", WJE_NEW, "Star Wars Evasive Action"); WJEString(doc, "cameo[$]", WJE_NEW, "Dr. Horrible's Sing-Along Blog"); WJEString(doc, "cameo[$]", WJE_NEW, "Ready Player One"); WJEBool(doc, "shiny", WJE_SET, TRUE); WJEInt64(doc, "crew[].born == 2468", WJE_SET, 2486); /* note: awesome! */ WJECloseDocument(WJEGet(doc, "shiny", NULL)); while((person = _WJEObject(doc, "crew[]", WJE_GET, &person))) { printf("%s (%s) is %"PRId64"\n", WJEString(person, "name", WJE_GET, ""), WJEString(person, "job", WJE_GET, ""), (2517 - WJEInt64(person, "born", WJE_GET, 0))); } while((cameo = WJEGet(doc, "cameo[]", cameo))) { printf("Cameo: %s\n", WJEString(cameo, NULL, WJE_GET, "")); } WJEDump(doc); WJECloseDocument(doc); return 0; }
EXPORT XplBool WJEMergeObjects(WJElement to, WJElement from, XplBool overwrite) { WJElement a, b; if (!to || !from || WJR_TYPE_OBJECT != to->type || WJR_TYPE_OBJECT != from->type ) { return(FALSE); } for (a = from->child; a; a = a->next) { if ((b = WJEChild(to, a->name, WJE_GET))) { if (WJR_TYPE_OBJECT == b->type && WJR_TYPE_OBJECT == a->type) { /* Merge all the children */ WJEMergeObjects(b, a, overwrite); continue; } else if (overwrite) { /* Remove the existing value and overwrite it */ WJECloseDocument(b); } else { /* Do nothing with this element */ continue; } } /* Copy the object over */ WJEAttach(to, WJECopyDocument(NULL, a, NULL, NULL)); } return(TRUE); }
EXPORT XplBool WJEAttach(WJElement container, WJElement document) { WJElement prev; if (!document || !container) { return(FALSE); } if (document->name) { while ((prev = WJEChild(container, document->name, WJE_GET))) { WJECloseDocument(prev); } } WJEDetach(document); /* Insert it into the new container */ document->parent = container; if (!container->child) { container->child = document; } else { /* Find the last child so it can be added at the end */ for (prev = container->child; prev && prev->next; prev = prev->next); prev->next = document; document->prev = prev; } container->count++; WJEChanged(container); return(TRUE); }
int main(int argc, char **argv) { int r = 0; WJElement doc = NULL; WJReader reader; int i, a, line; char *j, *x; MemoryManagerOpen("wjeunit"); /* Begin tests */ for (a = 1; a < argc; a++) { for (i = 0; tests[i].name && tests[i].cb; i++) { if (!stricmp(argv[a], tests[i].name)) { break; } } if (!tests[i].cb) { fprintf(stderr, "Ignoring unknown test \"%s\"\n", argv[a]); } else { /* Reopen the JSON for each test in case a test modified it */ if ((j = MemStrdup(json))) { /* Correct the quotes */ for (x = j; *x; x++) { if (*x == '\'') *x = '"'; } // printf("JSON:\n%s\n", j); if ((reader = WJROpenMemDocument(j, NULL, 0))) { doc = WJEOpenDocument(reader, NULL, NULL, NULL); WJRCloseDocument(reader); } MemRelease(&j); } if (!doc) { fprintf(stderr, "error: Could not parse JSON document\n"); MemoryManagerClose("wjeunit"); return(1); } if ((line = tests[i].cb(doc))) { fprintf(stderr, "error: %s:%d: Failed test \"%s\"\n", __FILE__, line, tests[i].name); r = 1; } WJECloseDocument(doc); } } /* All done */ MemoryManagerClose("wjeunit"); return(r); }
int main(int argc, char **argv) { WJElement doc = NULL; WJElement person = NULL; doc = WJEObject(NULL, NULL, WJE_NEW); WJEString(doc, "name", WJE_SET, "Serenity"); WJEString(doc, "class", WJE_SET, "firefly"); WJEArray(doc, "crew", WJE_SET); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Malcolm Reynolds"); WJEString(doc, "crew[-1].job", WJE_SET, "captain"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2468); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Kaywinnet Lee Fry"); WJEString(doc, "crew[-1].job", WJE_SET, "mechanic"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2494); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Jayne Cobb"); WJEString(doc, "crew[-1].job", WJE_SET, "public relations"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2485); WJEBool(doc, "shiny", WJE_SET, TRUE); WJEInt64(doc, "crew[].born == 2468", WJE_SET, 2486); /* note: awesome! */ WJECloseDocument(WJEGet(doc, "shiny", NULL)); while((person = _WJEObject(doc, "crew[]", WJE_GET, &person))) { printf("%s (%s) is %d\n", WJEString(person, "name", WJE_GET, ""), WJEString(person, "job", WJE_GET, ""), 2517 - WJEInt64(person, "born", WJE_GET, 0)); } WJEDump(doc); WJECloseDocument(doc); return 0; }
EXPORT XplBool WJECloseDocument(WJElement document) { _WJElement *current = (_WJElement *) document; if (!document) { return(FALSE); } if (document->freecb && !document->freecb(document)) { /* The callback has prevented free'ing the document */ return(TRUE); } WJEChanged(document); /* Remove references to this object */ if (document->parent) { if (document->parent->child == document) { document->parent->child = document->next; } document->parent->count--; } if (document->prev) { document->prev->next = document->next; } if (document->next) { document->next->prev = document->prev; } /* Destroy all children */ while (document->child) { WJECloseDocument(document->child); } if (current->pub.type == WJR_TYPE_STRING) { MemFree(current->value.string); current->pub.length = 0; } if (document->name && current->_name != document->name) { MemRelease(&document->name); } MemFree(current); return(TRUE); }
_WJElement * _WJEReset(_WJElement *e, WJRType type) { if (!e) return(NULL); while (e->pub.child) { WJECloseDocument(e->pub.child); } if (WJR_TYPE_STRING == e->pub.type && e->value.string) { MemRelease(&(e->value.string)); } e->value.string = NULL; e->pub.length = 0; e->pub.type = type; return(e); }
static int PathsTest(WJElement doc) { WJElement e; /* Compare multiple methods of referencing deep elements */ if ((e = WJEGet(doc, "a.b.c.d.e.f", NULL)) == NULL) return(__LINE__); if (e != WJEGet(doc, "a[\"b\"].c.d.e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a['b'].c.d.e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a.b[\"c\"].d.e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a.b['c'].d.e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a['b'].c['d'].e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a['b']['c']['d'].e.f",NULL)) return(__LINE__); /* Do the same thing with paths that don't exist yet */ if (!(e = WJEObject(doc, "z[\"y\"].x.w.v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z['y'].x.w.v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z.y[\"x\"].w.v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z.y['x'].w.v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z['y'].x['w'].v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z['y']['x']['w'].v.u",WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); /* Test paths that contain spaces, and dashes */ if (!WJEGet(doc, "space balls", NULL)) return(__LINE__); if (!WJEGet(doc, "space balls.the toliet paper", NULL)) return(__LINE__); if ( WJEGet(doc, "space balls.the what ", NULL)) return(__LINE__); if (!WJEGet(doc, "space balls.the what []", NULL)) return(__LINE__); return(0); }
int main(int argc, char **argv) { FILE *in = NULL; WJElement doc = NULL; WJElement current = NULL; int r = 0; WJReader reader; char *cmd; char line[1024]; /* Print pretty documents by default */ wje.pretty = TRUE; /* Print base 10 by default */ wje.base = 10; if (argc > 2) { /* Umm, we only allow one argument... a filename */ usage(argv[0]); return(1); } MemoryManagerOpen("wje-cli"); if (argc == 2) { if (!stricmp(argv[1], "--help") || !stricmp(argv[1], "-h")) { usage(argv[0]); MemoryManagerClose("wje-cli"); return(0); } if (!(in = fopen(argv[1], "rb"))) { perror(NULL); MemoryManagerClose("wje-cli"); return(2); } /* A filename was specified on the command line. Does this look like a script, or a JSON document? */ if (fgets(line, sizeof(line), in) && !strncmp(line, "#!", 2) ) { /* This looks like a script, read commands from this file */ ; } else { /* Assume it is a JSON document, rewind back to the start. */ rewind(in); if ((reader = WJROpenFILEDocument(in, NULL, 0))) { doc = WJEOpenDocument(reader, NULL, NULL, NULL); WJRCloseDocument(reader); wje.filename = MemStrdup(argv[1]); } fclose(in); in = NULL; if (!doc) { fprintf(stderr, "Could not parse JSON document: %s\n", argv[1]); MemoryManagerClose("wje-cli"); return(3); } } } if (!in) { /* Read commands from standard in */ in = stdin; } if (!doc) { /* Start with an empty document if one wasn't specified */ doc = WJEObject(NULL, NULL, WJE_SET); } current = doc; for (;;) { /* Read the next command */ if (in == stdin && isatty(fileno(stdin))) { fprintf(stdout, "wje"); if (r) { fprintf(stdout, " (%d)", r); } fprintf(stdout, "> "); fflush(stdout); } if (fgets(line, sizeof(line), in)) { cmd = skipspace(line); } else { cmd = NULL; } if (!cmd || !*cmd) { /* Ignore blank lines */ } else { r = runcmd(&doc, ¤t, cmd); } cmd = NULL; if (feof(in) || wje.exiting) { break; } } if (doc) { WJECloseDocument(doc); doc = NULL; } if (in && in != stdin) { fclose(in); in = NULL; } if (wje.filename) { MemRelease(&wje.filename); } MemoryManagerClose("wje-cli"); return(r); }