Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #165 #243

Merged
merged 3 commits into from Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 24 additions & 9 deletions json_object.c
Expand Up @@ -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 */
Expand Down
10 changes: 10 additions & 0 deletions json_object.h
Expand Up @@ -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()
Expand Down
22 changes: 21 additions & 1 deletion tests/test1.c
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/test_util_file.c
Expand Up @@ -5,6 +5,7 @@
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

#include "json.h"
#include "json_util.h"
Expand Down