コード例 #1
0
ファイル: jsmn_test.c プロジェクト: ntpsec/ntpsec
int test_issue_22() {
	int r;
	jsmn_parser p;
	jsmntok_t tokens[128];
	const char *js;

	js = "{ \"height\":10, \"layers\":[ { \"data\":[6,6], \"height\":10, "
		"\"name\":\"Calque de Tile 1\", \"opacity\":1, \"type\":\"tilelayer\", "
		"\"visible\":true, \"width\":10, \"x\":0, \"y\":0 }], "
		"\"orientation\":\"orthogonal\", \"properties\": { }, \"tileheight\":32, "
		"\"tilesets\":[ { \"firstgid\":1, \"image\":\"..\\/images\\/tiles.png\", "
		"\"imageheight\":64, \"imagewidth\":160, \"margin\":0, \"name\":\"Tiles\", "
		"\"properties\":{}, \"spacing\":0, \"tileheight\":32, \"tilewidth\":32 }], "
		"\"tilewidth\":32, \"version\":1, \"width\":10 }";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 128);
	check(r >= 0);
#if 0
	for (i = 1; tokens[i].end < tokens[0].end; i++) {
		if (tokens[i].type == JSMN_STRING || tokens[i].type == JSMN_PRIMITIVE) {
			printf("%.*s\n", tokens[i].end - tokens[i].start, js + tokens[i].start);
		} else if (tokens[i].type == JSMN_ARRAY) {
			printf("[%d elems]\n", tokens[i].size);
		} else if (tokens[i].type == JSMN_OBJECT) {
			printf("{%d elems}\n", tokens[i].size);
		} else {
			TOKEN_PRINT(tokens[i]);
		}
	}
#endif
	return 0;
}
コード例 #2
0
ファイル: jsmndemo.cpp プロジェクト: OptiRTC/JSMNSpark
void loop() {
	int i, r;
	jsmn_parser p;
	jsmntok_t tok[10];
	char obj[20];
	
	// This is the full JSON test string - Array of one object containg 6 string objects
	js = "  [{\"id\":\"1\",\"timestamp\":\"1337\",\"username\":\"kaul\"}]";

	if (!Serial.available())
	    return;

    obj[0] = Serial.read();
    
	jsmn_init(&p);
	r = jsmn_parse(&p, js, tok, 10);
	if (r == JSMN_SUCCESS)
	    Serial.println("parsed successfuly");
	else {
	    Serial.println("parse failed");
	    return;
	}

	for (i = 0; i < 8; i++) {
		Serial.print("Token ");
		Serial.print(i);
		TOKEN_PRINT(tok[i]);
		delay(10);
	}
	if (tok[0].type == JSMN_ARRAY && tok[0].size == 1)
	    Serial.println("Valid array found");
	if (tok[1].type == JSMN_OBJECT && tok[1].size == 6);
	    Serial.println("Valid object in array found");
	if (tok[1].type == JSMN_OBJECT && tok[1].size == 6);
	    Serial.println("6 valid string types in object found");

	for (i = 2; i < (tok[1].size + 2); i++) {
		if (tok[i].type != JSMN_STRING)
		    Serial.println("error: expected string");
		else {
			strlcpy(obj, &js[tok[i].start], (tok[i].end - tok[i].start + 1));
			Serial.print(obj);
			Serial.print(", ");
		}
	}
	Serial.println(" ");
}