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; }
/* callback: load more schema from files based on "name" and a pattern argument */ static WJElement schema_load(const char *name, void *client, const char *file, const int line) { char *format; char *path; FILE *schemafile; WJReader readschema; WJElement schema; schema = NULL; if(client && name) { format = (char *)client; path = malloc(strlen(format) + strlen(name)); sprintf(path, format, name); if(schemafile = fopen(path, "r")) { if((readschema = WJROpenFILEDocument(schemafile, NULL, 0))) { schema = WJEOpenDocument(readschema, NULL, NULL, NULL); } else { fprintf(stderr, "json document failed to open: '%s'\n", path); } fclose(schemafile); } else { fprintf(stderr, "json file not found: '%s'\n", path); } free(path); } return schema; }
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); }
/* callback: load more schema from files based on "name" and a pattern argument */ static WJElement schema_load(const char *name, void *client, const char *file, const int line) { char *format; char *path; WJReader readschema; WJElement schema; schema = NULL; if(client && name) { format = (char *)client; asprintf(&path, format, name); if((readschema = WJROpenFILEDocument(path, NULL, 0))) { schema = WJEOpenDocument(readschema, NULL, NULL, NULL); } } return schema; }
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); }