Пример #1
0
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;
}
Пример #2
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;
}
Пример #3
0
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;
}