|
Packit Service |
def718 |
#include <stdio.h>
|
|
Packit Service |
def718 |
#include <stdlib.h>
|
|
Packit Service |
def718 |
#include <stddef.h>
|
|
Packit Service |
def718 |
#include <string.h>
|
|
Packit Service |
def718 |
#include <assert.h>
|
|
Packit Service |
def718 |
#include <errno.h>
|
|
Packit Service |
def718 |
#include <time.h>
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
#include "json.h"
|
|
Packit Service |
def718 |
#include "printbuf.h"
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static void do_benchmark(json_object *src1);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static const char *json_str1 =
|
|
Packit Service |
def718 |
"{"
|
|
Packit Service |
def718 |
" \"glossary\": {"
|
|
Packit Service |
def718 |
" \"title\": \"example glossary\","
|
|
Packit Service |
def718 |
" \"GlossDiv\": {"
|
|
Packit Service |
def718 |
" \"title\": \"S\","
|
|
Packit Service |
def718 |
" \"null_obj\": null, "
|
|
Packit Service |
def718 |
" \"GlossList\": {"
|
|
Packit Service |
def718 |
" \"GlossEntry\": {"
|
|
Packit Service |
def718 |
" \"ID\": \"SGML\","
|
|
Packit Service |
def718 |
" \"SortAs\": \"SGML\","
|
|
Packit Service |
def718 |
" \"GlossTerm\": \"Standard Generalized Markup Language\","
|
|
Packit Service |
def718 |
" \"Acronym\": \"SGML\","
|
|
Packit Service |
def718 |
" \"Abbrev\": \"ISO 8879:1986\","
|
|
Packit Service |
def718 |
" \"GlossDef\": {"
|
|
Packit Service |
def718 |
" \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\","
|
|
Packit Service |
def718 |
" \"GlossSeeAlso\": [\"GML\", \"XML\"]"
|
|
Packit Service |
def718 |
" },"
|
|
Packit Service |
def718 |
" \"GlossSee\": \"markup\""
|
|
Packit Service |
def718 |
" }"
|
|
Packit Service |
def718 |
" }"
|
|
Packit Service |
def718 |
" }"
|
|
Packit Service |
def718 |
" }"
|
|
Packit Service |
def718 |
"}";
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static const char *json_str2 =
|
|
Packit Service |
def718 |
"{\"menu\": {"
|
|
Packit Service |
def718 |
" \"header\": \"SVG Viewer\","
|
|
Packit Service |
def718 |
" \"items\": ["
|
|
Packit Service |
def718 |
" {\"id\": \"Open\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"OpenNew\", \"label\": \"Open New\"},"
|
|
Packit Service |
def718 |
" null,"
|
|
Packit Service |
def718 |
" {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"OriginalView\", \"label\": \"Original View\"},"
|
|
Packit Service |
def718 |
" null,"
|
|
Packit Service |
def718 |
" {\"id\": \"Quality\", \"another_null\": null},"
|
|
Packit Service |
def718 |
" {\"id\": \"Pause\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"Mute\"},"
|
|
Packit Service |
def718 |
" null,"
|
|
Packit Service |
def718 |
" {\"id\": \"Find\", \"label\": \"Find...\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"FindAgain\", \"label\": \"Find Again\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"Copy\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"ViewSource\", \"label\": \"View Source\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"SaveAs\", \"label\": \"Save As\"},"
|
|
Packit Service |
def718 |
" null,"
|
|
Packit Service |
def718 |
" {\"id\": \"Help\"},"
|
|
Packit Service |
def718 |
" {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}"
|
|
Packit Service |
def718 |
" ]"
|
|
Packit Service |
def718 |
"}}";
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static const char *json_str3 =
|
|
Packit Service |
def718 |
"{\"menu\": {"
|
|
Packit Service |
def718 |
" \"id\": \"file\","
|
|
Packit Service |
def718 |
" \"value\": \"File\","
|
|
Packit Service |
def718 |
" \"popup\": {"
|
|
Packit Service |
def718 |
" \"menuitem\": ["
|
|
Packit Service |
def718 |
" {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},"
|
|
Packit Service |
def718 |
" {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},"
|
|
Packit Service |
def718 |
" {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}"
|
|
Packit Service |
def718 |
" ]"
|
|
Packit Service |
def718 |
" }"
|
|
Packit Service |
def718 |
"}}";
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object_to_json_string_fn my_custom_serializer;
|
|
Packit Service |
def718 |
int my_custom_serializer(struct json_object *jso, struct printbuf *pb, int level, int flags)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
sprintbuf(pb, "OTHER");
|
|
Packit Service |
def718 |
return 0;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_c_shallow_copy_fn my_shallow_copy;
|
|
Packit Service |
def718 |
int my_shallow_copy(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
int rc;
|
|
Packit Service |
def718 |
rc = json_c_shallow_copy_default(src, parent, key, index, dst);
|
|
Packit Service |
def718 |
if (rc < 0)
|
|
Packit Service |
def718 |
return rc;
|
|
Packit Service |
def718 |
if (key != NULL && strcmp(key, "with_serializer") == 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("CALLED: my_shallow_copy on with_serializer object\n");
|
|
Packit Service |
def718 |
void *userdata = json_object_get_userdata(src);
|
|
Packit Service |
def718 |
json_object_set_serializer(*dst, my_custom_serializer, userdata, NULL);
|
|
Packit Service |
def718 |
return 2;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
return rc;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int main(int argc, char **argv)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
struct json_object *src1, *src2, *src3;
|
|
Packit Service |
def718 |
struct json_object *dst1 = NULL, *dst2 = NULL, *dst3 = NULL;
|
|
Packit Service |
def718 |
int benchmark = 0;
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
if (argc > 1 && strcmp(argv[1], "--benchmark") == 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
benchmark = 1;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
src1 = json_tokener_parse(json_str1);
|
|
Packit Service |
def718 |
src2 = json_tokener_parse(json_str2);
|
|
Packit Service |
def718 |
src3 = json_tokener_parse(json_str3);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
assert(src1 != NULL);
|
|
Packit Service |
def718 |
assert(src1 != NULL);
|
|
Packit Service |
def718 |
assert(src3 != NULL);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("PASSED - loaded input data\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
/* do this 3 times to make sure overwriting it works */
|
|
Packit Service |
def718 |
assert(0 == json_object_deep_copy(src1, &dst1, NULL));
|
|
Packit Service |
def718 |
assert(0 == json_object_deep_copy(src2, &dst2, NULL));
|
|
Packit Service |
def718 |
assert(0 == json_object_deep_copy(src3, &dst3, NULL));
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("PASSED - all json_object_deep_copy() returned succesful\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
|
|
Packit Service |
def718 |
assert(errno == EINVAL);
|
|
Packit Service |
def718 |
assert(-1 == json_object_deep_copy(src2, &dst2, NULL));
|
|
Packit Service |
def718 |
assert(errno == EINVAL);
|
|
Packit Service |
def718 |
assert(-1 == json_object_deep_copy(src3, &dst3, NULL));
|
|
Packit Service |
def718 |
assert(errno == EINVAL);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
assert(1 == json_object_equal(src1, dst1));
|
|
Packit Service |
def718 |
assert(1 == json_object_equal(src2, dst2));
|
|
Packit Service |
def718 |
assert(1 == json_object_equal(src3, dst3));
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("PASSED - all json_object_equal() tests returned succesful\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
assert(0 == strcmp(json_object_to_json_string_ext(src1, JSON_C_TO_STRING_PRETTY),
|
|
Packit Service |
def718 |
json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)));
|
|
Packit Service |
def718 |
assert(0 == strcmp(json_object_to_json_string_ext(src2, JSON_C_TO_STRING_PRETTY),
|
|
Packit Service |
def718 |
json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY)));
|
|
Packit Service |
def718 |
assert(0 == strcmp(json_object_to_json_string_ext(src3, JSON_C_TO_STRING_PRETTY),
|
|
Packit Service |
def718 |
json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY)));
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("PASSED - comparison of string output\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object_get(dst1);
|
|
Packit Service |
def718 |
assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
|
|
Packit Service |
def718 |
assert(errno == EINVAL);
|
|
Packit Service |
def718 |
json_object_put(dst1);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("PASSED - trying to overrwrite an object that has refcount > 1");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("\nPrinting JSON objects for visual inspection\n");
|
|
Packit Service |
def718 |
printf("------------------------------------------------\n");
|
|
Packit Service |
def718 |
printf(" JSON1\n");
|
|
Packit Service |
def718 |
printf("%s\n", json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY));
|
|
Packit Service |
def718 |
printf("------------------------------------------------\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("------------------------------------------------\n");
|
|
Packit Service |
def718 |
printf(" JSON2\n");
|
|
Packit Service |
def718 |
printf("%s\n", json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY));
|
|
Packit Service |
def718 |
printf("------------------------------------------------\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("------------------------------------------------\n");
|
|
Packit Service |
def718 |
printf(" JSON3\n");
|
|
Packit Service |
def718 |
printf("------------------------------------------------\n");
|
|
Packit Service |
def718 |
printf("%s\n", json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY));
|
|
Packit Service |
def718 |
printf("------------------------------------------------\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object_put(dst1);
|
|
Packit Service |
def718 |
json_object_put(dst2);
|
|
Packit Service |
def718 |
json_object_put(dst3);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
printf("\nTesting deep_copy with a custom serializer set\n");
|
|
Packit Service |
def718 |
json_object *with_serializer = json_object_new_string("notemitted");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object_set_serializer(with_serializer, my_custom_serializer, "dummy userdata", NULL);
|
|
Packit Service |
def718 |
json_object_object_add(src1, "with_serializer", with_serializer);
|
|
Packit Service |
def718 |
dst1 = NULL;
|
|
Packit Service |
def718 |
/* With a custom serializer in use, a custom shallow_copy function must also be used */
|
|
Packit Service |
def718 |
assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
|
|
Packit Service |
def718 |
assert(0 == json_object_deep_copy(src1, &dst1, my_shallow_copy));
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object *dest_with_serializer = json_object_object_get(dst1, "with_serializer");
|
|
Packit Service |
def718 |
assert(dest_with_serializer != NULL);
|
|
Packit Service |
def718 |
char *dst_userdata = json_object_get_userdata(dest_with_serializer);
|
|
Packit Service |
def718 |
assert(strcmp(dst_userdata, "dummy userdata") == 0);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
const char *special_output = json_object_to_json_string(dest_with_serializer);
|
|
Packit Service |
def718 |
assert(strcmp(special_output, "OTHER") == 0);
|
|
Packit Service |
def718 |
printf("\ndeep_copy with custom serializer worked OK.\n");
|
|
Packit Service |
def718 |
json_object_put(dst1);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
if (benchmark)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
do_benchmark(src2);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object_put(src1);
|
|
Packit Service |
def718 |
json_object_put(src2);
|
|
Packit Service |
def718 |
json_object_put(src3);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
return 0;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static void do_benchmark(json_object *src2)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
json_object *dst2 = NULL;
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int ii;
|
|
Packit Service |
def718 |
/**
|
|
Packit Service |
def718 |
* The numbers that I got are:
|
|
Packit Service |
def718 |
* BENCHMARK - 1000000 iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took 71 seconds
|
|
Packit Service |
def718 |
* BENCHMARK - 1000000 iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took 29 seconds
|
|
Packit Service |
def718 |
*/
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int iterations = 1000000;
|
|
Packit Service |
def718 |
time_t start = time(NULL);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
start = time(NULL);
|
|
Packit Service |
def718 |
for (ii = 0; ii < iterations; ii++) {
|
|
Packit Service |
def718 |
dst2 = json_tokener_parse(json_object_get_string(src2));
|
|
Packit Service |
def718 |
json_object_put(dst2);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
printf("BENCHMARK - %d iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took %d seconds\n", iterations, (int)(time(NULL) - start));
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
start = time(NULL);
|
|
Packit Service |
def718 |
dst2 = NULL;
|
|
Packit Service |
def718 |
for (ii = 0; ii < iterations; ii++) {
|
|
Packit Service |
def718 |
json_object_deep_copy(src2, &dst2, NULL);
|
|
Packit Service |
def718 |
json_object_put(dst2);
|
|
Packit Service |
def718 |
dst2 = NULL;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took %d seconds\n", iterations, (int)(time(NULL) - start));
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|