예제 #1
0
파일: json.c 프로젝트: yairgd/nodes
/**
 * Created  07/25/2015
 * @brief   creates new json_value_t structure
 * @param   
 * @return 0 - success,1 fail  
 */
json_value_t * json_object11()
{
	json_value_t *json_value= malloc(sizeof(json_value_t));
	json_value->type = JS_OBJECT;
	json_object_init ( &json_value->u.object);
	return json_value;
}
예제 #2
0
파일: parson.c 프로젝트: sduclos/S52
/* JSON Value */
static JSON_Value * json_value_init_object(void) {
    JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
    if (!new_value) { return NULL; }
    new_value->type = JSONObject;
    new_value->value.object = json_object_init();
    if (!new_value->value.object) { parson_free(new_value); return NULL; }
    return new_value;
}
예제 #3
0
int main()
{
	JSON_Object root;
	JSON_Array arr;
	JSON_String *str;

	json_object_init(&root);

	json_object_set(&root, "a", json_string_new("str123"));
	//json_object_debug_hash(&root);
	json_object_set(&root, "b", json_number_new(123));
	//json_object_debug_hash(&root);

	json_array_init(&arr);
	json_array_append(&arr, json_boolean_true());
	json_array_append(&arr, json_boolean_true());
	json_array_append(&arr, json_boolean_false());
	json_array_append(&arr, json_null());

	json_object_set(&root, "c", &arr);
	//json_object_debug_hash(&root);
	str = json_value_to_string(&root, 0);
	json_value_unref(&root);

	json_print(json_string_cstr(str));
	json_value_unref(str);

#if 0
#   define print_size(T) printf("Size of '" #T "': %lu\n", sizeof(T))
	printf("==================================\n");
	print_size(JSON_Value);
	print_size(JSON_Null);
	print_size(JSON_Boolean);
	print_size(JSON_Number);
	print_size(JSON_String);
	print_size(JSON_Array);
	print_size(JSON_Object);
#endif

	return 0;
}
예제 #4
0
void json_test(int argc, char **argv)
{
	int num;
	int fmt;
	char text[] =
	    "{\n     \"precision\": \"zip\",\n       \"Latitude\":  37668,\n       \"Longitude\": -12259,\n     \"Address\":   \"\",\n  \"City\":      \"SAN FRANCISCO\",\n     \"State\":     \"CA\",\n        \"Zip\":       \"94107\",\n     \"Country\":   \"US\"\n                                                                                                    }";
	char complex_obj[] =
	    "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", 		\"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\",\"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", 	\"Abbrev\": \"ISO 8879:1986\", 	\"GlossDef\": { \"para\": \"language\",	\"GlossSeeAlso\": \"XML\" },	\"GlossSee\": \"markup\"  } } } } }";

	debug("#### json_parser ####\n");

	json_object_init(&tmp, text);

	if (!json_get_val_str(&tmp, "Country", str, MAX_JSON_STR_LEN)) {
		if (!strcmp(str, "US")) {
			debug("***Json_parser ---str_get match\n");
		} else {
			debug("***Err ---str_get mismatch\n");
			goto ERROR;
		}
	} else {
		debug("***Err ---str_get failed\n");
		goto ERROR;
	}

	if (!json_get_val_int(&tmp, "Longitude", &num)) {
		if (num == -12259) {
			debug("***Json_parser ---int_get match\n");
		} else {
			debug("***Err ---int_get mismatch\n");
			goto ERROR;
		}
	} else {
		debug("***Err ---int_get failed\n");
		goto ERROR;
	}

	fmt = 1;
	debug("***Json_parser ---Simple Set Example\n");
	json_str_init(&jstr, tmpstr, sizeof(tmpstr), fmt);
	json_start_object(&jstr);
	json_set_val_str(&jstr, "name", "John Galt");
	json_set_val_int(&jstr, "age", 29);
	json_set_val_str(&jstr, "numberstr", "123");
	json_close_object(&jstr);
	debug("The JSON String is :%s:\n", jstr.buff);

	debug("***Json_parser --- Complex object Set Example\n");
	json_str_init(&jstr_c, tmpstr, sizeof(tmpstr), fmt);
	json_start_object(&jstr_c);
	json_push_object(&jstr_c, "menu");
	json_set_val_str(&jstr_c, "pictures", "tmp");

	json_push_object(&jstr_c, "Image");
	json_set_val_str(&jstr_c, "name", "sun");
	json_set_val_int(&jstr_c, "length", 1000);
	json_set_val_str(&jstr_c, "size", "1600pi");

	json_pop_object(&jstr_c);	/* Close Image */

	json_push_object(&jstr_c, "text");
	json_set_val_str(&jstr_c, "style", "bold");
	json_set_val_str(&jstr_c, "alignment", "center");
	json_set_val_str(&jstr_c, "name", "text1");

	json_pop_object(&jstr_c);	/* Close text */
	json_set_val_str(&jstr_c, "object", "close");
	json_pop_object(&jstr_c);	/* Close menu */
	json_close_object(&jstr_c);	/* Close main */

	debug("The JSON String is *%s*\n", jstr_c.buff);

	struct json_object obj;
	json_object_init(&obj, jstr_c.buff);
	if (!json_get_composite_object(&obj, "text")) {
		if (!json_get_val_int(&obj, "length", &num))
			debug("val is %d \n", num);
		else
			debug("***Json_parser ---passed, no such member\n");

		if (!json_get_val_str(&obj, "style", str, MAX_JSON_STR_LEN)) {
			if (!strcmp(str, "bold")) {
				debug
				    ("***Json_parser ---composite_str_get match\n");
				debug("member->style:val->%s\n", str);
			} else {
				debug("***Err ---composite_str_get mismatch\n");
				goto ERROR;
			}
		} else {
			debug("***Err ---composite_str_get failed\n");
			goto ERROR;
		}

	} else {
		debug("***Err ---get_object_offsets failed\n");
		goto ERROR;
	}

	json_release_composite_object(&obj);

	json_object_init(&obj, complex_obj);
	if (!json_get_composite_object(&obj, "GlossEntry")) {
		if (!json_get_val_str(&obj, "GlossSee", str, 64)) {
			if (!strcmp(str, "markup")) {
				debug
				    ("***Json_parser ---composite_str_get match\n");
				debug("member->GlossEntry:val->%s\n", str);
			} else {
				debug("***Err ---composite_str_get mismatch\n");
				goto ERROR;
			}
		} else {
			debug("***Err ---composite_str_get failed\n");
			goto ERROR;
		}
	} else {
		debug("***Err ---get_object_offsets failed\n");
		goto ERROR;
	}

	json_release_composite_object(&obj);
	goto SUCCESS;
ERROR:
	wmprintf("Error");
SUCCESS:
	wmprintf("Success");
}