#define MAX_INTEGER_STR_LENGTH 100
#define MAX_REAL_STR_LENGTH 100
-typedef int (*dump_func)(const char *buffer, int size, void *data);
-
struct string
{
char *buffer;
int size;
};
-static int dump_to_strbuffer(const char *buffer, int size, void *data)
+static int dump_to_strbuffer(const char *buffer, size_t size, void *data)
{
return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
}
-static int dump_to_file(const char *buffer, int size, void *data)
+static int dump_to_file(const char *buffer, size_t size, void *data)
{
FILE *dest = (FILE *)data;
if(fwrite(buffer, size, 1, dest) != 1)
/* 32 spaces (the maximum indentation size) */
static char whitespace[] = " ";
-static int dump_indent(size_t flags, int depth, int space, dump_func dump, void *data)
+static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t dump, void *data)
{
if(JSON_INDENT(flags) > 0)
{
return 0;
}
-static int dump_string(const char *str, int ascii, dump_func dump, void *data)
+static int dump_string(const char *str, int ascii, json_dump_callback_t dump, void *data)
{
const char *pos, *end;
int32_t codepoint;
}
static int do_dump(const json_t *json, size_t flags, int depth,
- dump_func dump, void *data)
+ json_dump_callback_t dump, void *data)
{
int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
int (*cmp_func)(const void *, const void *);
size = json_object_size(json);
- keys = malloc(size * sizeof(object_key_t *));
+ keys = jsonp_malloc(size * sizeof(object_key_t *));
if(!keys)
goto object_error;
if(dump(separator, separator_length, data) ||
do_dump(value, flags, depth + 1, dump, data))
{
- free(keys);
+ jsonp_free(keys);
goto object_error;
}
if(dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data))
{
- free(keys);
+ jsonp_free(keys);
goto object_error;
}
}
{
if(dump_indent(flags, depth, 0, dump, data))
{
- free(keys);
+ jsonp_free(keys);
goto object_error;
}
}
}
- free(keys);
+ jsonp_free(keys);
}
else
{
strbuffer_t strbuff;
char *result;
- if(!json_is_array(json) && !json_is_object(json))
- return NULL;
+ if(!(flags & JSON_ENCODE_ANY)) {
+ if(!json_is_array(json) && !json_is_object(json))
+ return NULL;
+ }
if(strbuffer_init(&strbuff))
return NULL;
return NULL;
}
- result = strdup(strbuffer_value(&strbuff));
+ result = jsonp_strdup(strbuffer_value(&strbuff));
strbuffer_close(&strbuff);
return result;
int json_dumpf(const json_t *json, FILE *output, size_t flags)
{
- if(!json_is_array(json) && !json_is_object(json))
- return -1;
+ if(!(flags & JSON_ENCODE_ANY)) {
+ if(!json_is_array(json) && !json_is_object(json))
+ return -1;
+ }
return do_dump(json, flags, 0, dump_to_file, (void *)output);
}
fclose(output);
return result;
}
+
+int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags)
+{
+ if(!(flags & JSON_ENCODE_ANY)) {
+ if(!json_is_array(json) && !json_is_object(json))
+ return -1;
+ }
+
+ return do_dump(json, flags, 0, callback, data);
+}