Browse Source

Merge branch 'issue-165' of https://github.com/jobol/json-c into jobol-issue-165

Conflicts:
	tests/test_util_file.c
tags/json-c-0.13-20171207
Eric Haszlakiewicz 9 years ago
parent
commit
f3db59d990
3 changed files with 55 additions and 10 deletions
  1. +24
    -9
      json_object.c
  2. +10
    -0
      json_object.h
  3. +21
    -1
      tests/test1.c

+ 24
- 9
json_object.c View File

@@ -295,20 +295,35 @@ void json_object_set_serializer(json_object *jso,

/* extended conversion to string */

const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
{
if (!jso)
return "null";
const char *r = NULL;
size_t s = 0;

if ((!jso->_pb) && !(jso->_pb = printbuf_new()))
return NULL;
if (!jso)
{
s = 4;
r = "null";
}
else if ((jso->_pb) || (jso->_pb = printbuf_new()))
{
printbuf_reset(jso->_pb);

printbuf_reset(jso->_pb);
if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
{
s = (size_t)jso->_pb->bpos;
r = jso->_pb->buf;
}
}

if(jso->_to_json_string(jso, jso->_pb, 0, flags) < 0)
return NULL;
if (length)
*length = s;
return r;
}

return jso->_pb->buf;
const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
{
return json_object_to_json_string_length(jso, flags, NULL);
}

/* backwards-compatible conversion to string */


+ 10
- 0
json_object.h View File

@@ -223,6 +223,16 @@ extern const char* json_object_to_json_string(struct json_object *obj);
extern const char* json_object_to_json_string_ext(struct json_object *obj, int
flags);

/** Stringify object to json format
* @see json_object_to_json_string() for details on how to free string.
* @param obj the json_object instance
* @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants
* @param length a pointer where, if not NULL, the length (without null) is stored
* @returns a string in JSON format and the length if not NULL
*/
extern const char* json_object_to_json_string_length(struct json_object *obj, int
flags, size_t *length);

/**
* Returns the userdata set by json_object_set_userdata() or
* json_object_set_serializer()


+ 21
- 1
tests/test1.c View File

@@ -28,7 +28,27 @@ static int sort_fn (const void *j1, const void *j2)
}

#ifdef TEST_FORMATTED
#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj,sflags)
static const char *to_json_string(json_object *obj, int flags)
{
size_t length;
char *copy;
const char *result;

result = json_object_to_json_string_length(obj, flags, &length);
copy = strndup(result, length);
if (copy == NULL)
printf("to_json_string: Allocation failed!\n");
else {
result = json_object_to_json_string_ext(obj, flags);
if (length != strlen(result))
printf("to_json_string: Length mismatch!\n");
if (strcmp(copy, result) != 0)
printf("to_json_string: Comparison Failed!\n");
free(copy);
}
return result;
}
#define json_object_to_json_string(obj) to_json_string(obj,sflags)
#else
/* no special define */
#endif


Loading…
Cancel
Save