Example #1
0
/**
 * walk over a JSON object and call the walker function on each object
 * It passes a pointer to the value, so the function can change the 
 * value in place if desired
 *
 * The payload is passed to the walker function at each call, this can be used to
 * pass additional information to the function, or for the function to store information
 * in a reentrant manner
 */
void json_walk_object(JSON_object obj, json_objWalker_t walker, void *payload)
{

    if( ! obj ){
        return;  //return if it is a null object
    }

    json_walk_object(obj->left, walker,payload);

    walker(obj->key, &(obj->value), payload);

    json_walk_object(obj->right, walker,payload);
}
Example #2
0
int json_walk_node(char * js, jsmntok_t *t, int lvl)
{
	if (t->type == JSMN_OBJECT) {
		return json_walk_object(js, t, lvl);
	}

	if (t->type == JSMN_ARRAY) {
		return json_walk_array(js, t, lvl);
	}

	if (t->type == JSMN_STRING) {
//		printf("STRING: \"%s\"\n", json_token_tostr(js, t));
		printf("\"%s\"", json_token_tostr(js, t));
		return 1;
	}
		
	if (t->type == JSMN_PRIMITIVE) {
//		printf("PRIMITIVE: \"%s\"\n", json_token_tostr(js, t));
		printf("%s", json_token_tostr(js, t));
		return 1;
	}

	printf("Invalid response:");
	return -1;

}
Example #3
0
int json_parse_dump(char * js, jsmntok_t * t, void * ptr) 
{
	char s[JSON_STR_LEN_MAX + 1];

	if (t->start == JSMN_NULL || t->end == JSMN_NULL)
		return -JSON_ERR_INVALID_TOKEN;

	if (ptr == NULL)
		return -JSON_ERR_NULL_POINTER;

	switch (t->type) {
	case JSMN_OBJECT:
		return json_walk_object(stdout, js, t, 1);

	case JSMN_ARRAY:
		return json_walk_array(stdout, js, t, 1);

	case JSMN_STRING:
		json_token_tostr(s, JSON_STR_LEN_MAX, js, t);
		fprintf(stdout, "\"%s\"", s);
		return 1;
		
	case JSMN_PRIMITIVE:
		json_token_tostr(s, JSON_STR_LEN_MAX, js, t);
		fprintf(stdout, "%s", s);
		return 1;
	}

	return -JSON_ERR_INVALID_TYPE;
}
Example #4
0
int json_walk_node(FILE * f, char * js, jsmntok_t *t, int lvl)
{
	char s[JSON_STR_LEN_MAX + 1];

	if(t->start == JSMN_NULL || t->end == JSMN_NULL) {
		DCC_LOG(LOG_ERROR, "parameter invalid!");
		return -1;
	}

	switch (t->type) {
	case JSMN_OBJECT:
		return json_walk_object(f, js, t, lvl);

	case JSMN_ARRAY:
		return json_walk_array(f, js, t, lvl);

	case JSMN_STRING:
		json_token_tostr(s, JSON_STR_LEN_MAX, js, t);
		fprintf(f, "\"%s\"", s);
		return 1;
		
	case JSMN_PRIMITIVE:
		json_token_tostr(s, JSON_STR_LEN_MAX, js, t);
		fprintf(f, "%s", s);
		return 1;
	}

	DCC_LOG(LOG_ERROR, "Invalid type");

	return -1;

}
Example #5
0
int json_traverse(char * js, jsmntok_t *t)
{
	int ret;

	if (t->type != JSMN_OBJECT) {
		printf("Invalid response: root element must be an object.");
		return -1;
	}

	ret = json_walk_object(js, t, 0);

	printf("\n");

	return ret;
}
Example #6
0
int json_dump(FILE * f, char * js, jsmntok_t *t)
{
	int ret;

	/* Should never reach uninitialized tokens */
	if(t->start == JSMN_NULL || t->end == JSMN_NULL) {
		DCC_LOG(LOG_ERROR, "parameter invalid!");
		return -1;
	}

	if (t->type != JSMN_OBJECT) {
		DCC_LOG(LOG_ERROR, "root element must be an object.");
		return -1;
	}

	ret = json_walk_object(f, js, t, 0);

	printf("\n");

	return ret;
}