Beispiel #1
0
void serialization_example(void) {
    JSON_Value *root_value = json_value_init_object();
    JSON_Object *root_object = json_value_get_object(root_value);
    char *serialized_string = NULL;
    json_object_set_string(root_object, "name", "John Smith");
    json_object_set_number(root_object, "age", 25);
    json_object_dotset_string(root_object, "address.city", "Cupertino");
    json_object_dotset_value(root_object, "contact.emails",
                             json_parse_string("[\"[email protected]\", \"[email protected]\"]"));
    serialized_string = json_serialize_to_string(root_value);
    puts(serialized_string);
    json_free_serialized_string(serialized_string);
    json_value_free(root_value);
}
Beispiel #2
0
void test_suite_5(void) {
	JSON_Value *val_from_file = json_parse_file("tests/test_5.txt");

	JSON_Value *val = NULL;
	JSON_Object *obj = NULL;
	JSON_Array *interests_arr = NULL;

	val = json_value_init_object();
	TEST(val != NULL);

	obj = json_value_get_object(val);
	TEST(obj != NULL);

	TEST(json_object_set_string(obj, "first", "John") == JSONSuccess);
	TEST(json_object_set_string(obj, "last", "Doe") == JSONSuccess);
	TEST(json_object_set_number(obj, "age", 25) == JSONSuccess);
	TEST(json_object_set_boolean(obj, "registered", 1) == JSONSuccess);

	TEST(json_object_set_value(obj, "interests", json_value_init_array()) == JSONSuccess);
	interests_arr = json_object_get_array(obj, "interests");
	TEST(interests_arr != NULL);
	TEST(json_array_append_string(interests_arr, "Writing") == JSONSuccess);
	TEST(json_array_append_string(interests_arr, "Mountain Biking") == JSONSuccess);
	TEST(json_array_replace_string(interests_arr, 0, "Reading") == JSONSuccess);

	TEST(json_object_dotset_string(obj, "favorites.color", "blue") == JSONSuccess);
	TEST(json_object_dotset_string(obj, "favorites.sport", "running") == JSONSuccess);
	TEST(json_object_dotset_string(obj, "favorites.fruit", "apple") == JSONSuccess);
	TEST(json_object_dotremove(obj, "favorites.fruit") == JSONSuccess);
	TEST(json_object_set_string(obj, "utf string", "lorem ipsum") == JSONSuccess);
	TEST(json_object_set_string(obj, "utf-8 string", "あいうえお") == JSONSuccess);
	TEST(json_object_set_string(obj, "surrogate string", "lorem𝄞ipsum𝍧lorem") == JSONSuccess);
	TEST(json_object_set_string(obj, "windows path", "C:\\Windows\\Path") == JSONSuccess);
	TEST(json_value_equals(val_from_file, val));

	TEST(json_object_set_string(obj, NULL, "") == JSONFailure);
	TEST(json_object_set_string(obj, "last", NULL) == JSONFailure);
	TEST(json_object_set_string(obj, NULL, NULL) == JSONFailure);
	TEST(json_object_set_value(obj, NULL, NULL) == JSONFailure);

	TEST(json_object_dotset_string(obj, NULL, "") == JSONFailure);
	TEST(json_object_dotset_string(obj, "favorites.color", NULL) == JSONFailure);
	TEST(json_object_dotset_string(obj, NULL, NULL) == JSONFailure);
	TEST(json_object_dotset_value(obj, NULL, NULL) == JSONFailure);

	TEST(json_array_append_string(NULL, "lorem") == JSONFailure);
	TEST(json_array_append_value(interests_arr, NULL) == JSONFailure);
	TEST(json_array_append_value(NULL, NULL) == JSONFailure);

	TEST(json_array_remove(NULL, 0) == JSONFailure);
	TEST(json_array_replace_value(interests_arr, 0, NULL) == JSONFailure);
	TEST(json_array_replace_string(NULL, 0, "lorem") == JSONFailure);
	TEST(json_array_replace_string(interests_arr, 100, "not existing") == JSONFailure);

	TEST(json_array_append_string(json_object_get_array(obj, "interests"), NULL) == JSONFailure);


	/* UTF-8 tests */
	TEST(json_object_set_string(obj, "correct string", "κόσμε") == JSONSuccess);

	TEST(json_object_set_string(obj, "boundary 1", "\xed\x9f\xbf") == JSONSuccess);
	TEST(json_object_set_string(obj, "boundary 2", "\xee\x80\x80") == JSONSuccess);
	TEST(json_object_set_string(obj, "boundary 3", "\xef\xbf\xbd") == JSONSuccess);
	TEST(json_object_set_string(obj, "boundary 4", "\xf4\x8f\xbf\xbf") == JSONSuccess);

	TEST(json_object_set_string(obj, "first continuation byte", "\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "last continuation byte", "\xbf") == JSONFailure);

	TEST(json_object_set_string(obj, "impossible sequence 1", "\xfe") == JSONFailure);
	TEST(json_object_set_string(obj, "impossible sequence 2", "\xff") == JSONFailure);
	TEST(json_object_set_string(obj, "impossible sequence 3", "\xfe\xfe\xff\xff") == JSONFailure);

	TEST(json_object_set_string(obj, "overlong 1", "\xc0\xaf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 2", "\xc1\xbf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 3", "\xe0\x80\xaf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 4", "\xe0\x9f\xbf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 5", "\xf0\x80\x80\xaf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 6", "\xf0\x8f\xbf\xbf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 7", "\xf0\x8f\xbf\xbf") == JSONFailure);

	TEST(json_object_set_string(obj, "overlong null 1", "\xc0\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong null 2", "\xe0\x80\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong null 3", "\xf0\x80\x80\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong null 4", "\xf8\x80\x80\x80\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong null 5", "\xfc\x80\x80\x80\x80\x80") == JSONFailure);

	TEST(json_object_set_string(obj, "single surrogate 1", "\xed\xa0\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "single surrogate 2", "\xed\xaf\xbf") == JSONFailure);
	TEST(json_object_set_string(obj, "single surrogate 3", "\xed\xbf\xbf") == JSONFailure);
}