2009-07-28 16:01:52 +08:00
|
|
|
/*
|
2016-09-18 19:17:03 +08:00
|
|
|
* Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
|
2009-07-28 16:01:52 +08:00
|
|
|
*
|
|
|
|
* Jansson is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the MIT license. See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
2013-03-15 04:34:11 +08:00
|
|
|
#ifndef _GNU_SOURCE
|
2009-05-13 02:34:14 +08:00
|
|
|
#define _GNU_SOURCE
|
2013-03-15 04:34:11 +08:00
|
|
|
#endif
|
|
|
|
|
2017-01-27 00:23:31 +08:00
|
|
|
#include "jansson_private.h"
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
#include <assert.h>
|
2009-05-13 02:34:14 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-01-27 00:23:31 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2012-04-30 03:09:29 +08:00
|
|
|
#include "jansson.h"
|
2009-06-11 13:56:11 +08:00
|
|
|
#include "strbuffer.h"
|
2009-12-06 04:55:30 +08:00
|
|
|
#include "utf.h"
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
#define MAX_INTEGER_STR_LENGTH 100
|
|
|
|
#define MAX_REAL_STR_LENGTH 100
|
2009-10-13 20:40:26 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
#define FLAGS_TO_INDENT(f) ((f)&0x1F)
|
|
|
|
#define FLAGS_TO_PRECISION(f) (((f) >> 11) & 0x1F)
|
2014-04-30 17:46:33 +08:00
|
|
|
|
2017-01-26 16:46:48 +08:00
|
|
|
struct buffer {
|
|
|
|
const size_t size;
|
|
|
|
size_t used;
|
|
|
|
char *data;
|
|
|
|
};
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static int dump_to_strbuffer(const char *buffer, size_t size, void *data) {
|
2009-06-11 13:56:11 +08:00
|
|
|
return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
|
2009-06-10 03:37:31 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static int dump_to_buffer(const char *buffer, size_t size, void *data) {
|
2017-01-26 16:46:48 +08:00
|
|
|
struct buffer *buf = (struct buffer *)data;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (buf->used + size <= buf->size)
|
2017-01-26 16:46:48 +08:00
|
|
|
memcpy(&buf->data[buf->used], buffer, size);
|
|
|
|
|
|
|
|
buf->used += size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static int dump_to_file(const char *buffer, size_t size, void *data) {
|
2009-05-13 02:34:14 +08:00
|
|
|
FILE *dest = (FILE *)data;
|
2019-10-17 14:08:51 +08:00
|
|
|
if (fwrite(buffer, size, 1, dest) != 1)
|
2009-05-13 02:34:14 +08:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static int dump_to_fd(const char *buffer, size_t size, void *data) {
|
2017-01-27 00:23:31 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2018-08-30 20:45:28 +08:00
|
|
|
int *dest = (int *)data;
|
2019-10-17 14:08:51 +08:00
|
|
|
if (write(*dest, buffer, size) == (ssize_t)size)
|
2017-01-27 00:23:31 +08:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-14 03:11:04 +08:00
|
|
|
/* 32 spaces (the maximum indentation size) */
|
2012-12-06 08:46:14 +08:00
|
|
|
static const char whitespace[] = " ";
|
2009-09-04 03:00:33 +08:00
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t dump,
|
|
|
|
void *data) {
|
2019-10-17 14:08:51 +08:00
|
|
|
if (FLAGS_TO_INDENT(flags) > 0) {
|
2019-10-20 01:35:28 +08:00
|
|
|
unsigned int ws_count = FLAGS_TO_INDENT(flags), n_spaces = depth * ws_count;
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (dump("\n", 1, data))
|
2009-05-13 02:34:14 +08:00
|
|
|
return -1;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
while (n_spaces > 0) {
|
2019-10-20 01:35:28 +08:00
|
|
|
int cur_n =
|
|
|
|
n_spaces < sizeof whitespace - 1 ? n_spaces : sizeof whitespace - 1;
|
2015-05-01 21:18:53 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (dump(whitespace, cur_n, data))
|
2009-09-04 03:00:33 +08:00
|
|
|
return -1;
|
2015-05-01 21:18:53 +08:00
|
|
|
|
|
|
|
n_spaces -= cur_n;
|
2009-09-04 03:00:33 +08:00
|
|
|
}
|
2019-10-17 14:08:51 +08:00
|
|
|
} else if (space && !(flags & JSON_COMPACT)) {
|
2009-11-05 03:24:22 +08:00
|
|
|
return dump(" ", 1, data);
|
|
|
|
}
|
2009-07-10 01:54:29 +08:00
|
|
|
return 0;
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
static int dump_string(const char *str, size_t len, json_dump_callback_t dump, void *data,
|
|
|
|
size_t flags) {
|
2012-04-04 09:00:29 +08:00
|
|
|
const char *pos, *end, *lim;
|
2018-08-31 15:01:36 +08:00
|
|
|
int32_t codepoint = 0;
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (dump("\"", 1, data))
|
2009-05-13 02:34:14 +08:00
|
|
|
return -1;
|
|
|
|
|
2009-12-06 04:55:30 +08:00
|
|
|
end = pos = str;
|
2012-04-04 09:00:29 +08:00
|
|
|
lim = str + len;
|
2019-10-17 14:08:51 +08:00
|
|
|
while (1) {
|
2009-07-05 03:02:16 +08:00
|
|
|
const char *text;
|
2009-12-06 04:55:30 +08:00
|
|
|
char seq[13];
|
2009-07-05 03:02:16 +08:00
|
|
|
int length;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
while (end < lim) {
|
2012-04-04 09:00:29 +08:00
|
|
|
end = utf8_iterate(pos, lim - pos, &codepoint);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!end)
|
2009-12-06 04:55:30 +08:00
|
|
|
return -1;
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2009-12-06 04:55:30 +08:00
|
|
|
/* mandatory escape or control char */
|
2019-10-17 14:08:51 +08:00
|
|
|
if (codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
|
2012-06-29 18:11:41 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* slash */
|
2019-10-17 14:08:51 +08:00
|
|
|
if ((flags & JSON_ESCAPE_SLASH) && codepoint == '/')
|
2009-12-06 04:55:30 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* non-ASCII */
|
2019-10-17 14:08:51 +08:00
|
|
|
if ((flags & JSON_ENSURE_ASCII) && codepoint > 0x7F)
|
2009-12-06 04:55:30 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
pos = end;
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (pos != str) {
|
|
|
|
if (dump(str, pos - str, data))
|
2009-05-13 02:34:14 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2009-07-05 03:02:16 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (end == pos)
|
2009-07-05 03:02:16 +08:00
|
|
|
break;
|
|
|
|
|
2012-06-28 11:50:01 +08:00
|
|
|
/* handle \, /, ", and control codes */
|
2009-07-05 03:02:16 +08:00
|
|
|
length = 2;
|
2019-10-17 14:08:51 +08:00
|
|
|
switch (codepoint) {
|
2019-10-20 01:35:28 +08:00
|
|
|
case '\\':
|
|
|
|
text = "\\\\";
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
text = "\\\"";
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
text = "\\b";
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
text = "\\f";
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
text = "\\n";
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
text = "\\r";
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
text = "\\t";
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
text = "\\/";
|
|
|
|
break;
|
2019-10-17 14:08:51 +08:00
|
|
|
default: {
|
2009-12-06 04:55:30 +08:00
|
|
|
/* codepoint is in BMP */
|
2019-10-17 14:08:51 +08:00
|
|
|
if (codepoint < 0x10000) {
|
2019-10-20 01:35:28 +08:00
|
|
|
snprintf(seq, sizeof(seq), "\\u%04X", (unsigned int)codepoint);
|
2009-12-06 04:55:30 +08:00
|
|
|
length = 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not in BMP -> construct a UTF-16 surrogate pair */
|
2019-10-17 14:08:51 +08:00
|
|
|
else {
|
2009-12-06 04:55:30 +08:00
|
|
|
int32_t first, last;
|
|
|
|
|
|
|
|
codepoint -= 0x10000;
|
|
|
|
first = 0xD800 | ((codepoint & 0xffc00) >> 10);
|
|
|
|
last = 0xDC00 | (codepoint & 0x003ff);
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
snprintf(seq, sizeof(seq), "\\u%04X\\u%04X", (unsigned int)first,
|
|
|
|
(unsigned int)last);
|
2009-12-06 04:55:30 +08:00
|
|
|
length = 12;
|
|
|
|
}
|
|
|
|
|
2009-07-05 03:02:16 +08:00
|
|
|
text = seq;
|
|
|
|
break;
|
|
|
|
}
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
2009-07-05 03:02:16 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (dump(text, length, data))
|
2009-07-05 03:02:16 +08:00
|
|
|
return -1;
|
|
|
|
|
2009-12-06 04:55:30 +08:00
|
|
|
str = pos = end;
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return dump("\"", 1, data);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static int compare_keys(const void *key1, const void *key2) {
|
2016-06-21 02:03:02 +08:00
|
|
|
return strcmp(*(const char **)key1, *(const char **)key2);
|
2009-12-07 19:16:45 +08:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
static int do_dump(const json_t *json, size_t flags, int depth, hashtable_t *parents,
|
|
|
|
json_dump_callback_t dump, void *data) {
|
2017-02-28 03:56:55 +08:00
|
|
|
int embed = flags & JSON_EMBED;
|
|
|
|
|
|
|
|
flags &= ~JSON_EMBED;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json)
|
2013-04-15 13:42:23 +08:00
|
|
|
return -1;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
switch (json_typeof(json)) {
|
2019-10-20 01:35:28 +08:00
|
|
|
case JSON_NULL:
|
|
|
|
return dump("null", 4, data);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
case JSON_TRUE:
|
|
|
|
return dump("true", 4, data);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
case JSON_FALSE:
|
|
|
|
return dump("false", 5, data);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
case JSON_INTEGER: {
|
2009-10-13 20:40:26 +08:00
|
|
|
char buffer[MAX_INTEGER_STR_LENGTH];
|
|
|
|
int size;
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%" JSON_INTEGER_FORMAT,
|
|
|
|
json_integer_value(json));
|
2019-10-17 14:08:51 +08:00
|
|
|
if (size < 0 || size >= MAX_INTEGER_STR_LENGTH)
|
2009-06-23 05:14:28 +08:00
|
|
|
return -1;
|
|
|
|
|
2009-10-13 20:40:26 +08:00
|
|
|
return dump(buffer, size, data);
|
2009-06-23 05:14:28 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
case JSON_REAL: {
|
2009-10-13 20:40:26 +08:00
|
|
|
char buffer[MAX_REAL_STR_LENGTH];
|
|
|
|
int size;
|
2011-10-03 02:27:53 +08:00
|
|
|
double value = json_real_value(json);
|
2009-06-23 05:14:28 +08:00
|
|
|
|
2014-04-30 17:46:33 +08:00
|
|
|
size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value,
|
|
|
|
FLAGS_TO_PRECISION(flags));
|
2019-10-17 14:08:51 +08:00
|
|
|
if (size < 0)
|
2009-05-13 02:34:14 +08:00
|
|
|
return -1;
|
|
|
|
|
2009-10-13 20:40:26 +08:00
|
|
|
return dump(buffer, size, data);
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
|
|
|
|
case JSON_STRING:
|
2019-10-20 01:35:28 +08:00
|
|
|
return dump_string(json_string_value(json), json_string_length(json), dump,
|
|
|
|
data, flags);
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
case JSON_ARRAY: {
|
2014-11-25 05:04:13 +08:00
|
|
|
size_t n;
|
|
|
|
size_t i;
|
2019-10-17 14:08:51 +08:00
|
|
|
/* Space for "0x", double the sizeof a pointer for the hex and a
|
|
|
|
* terminator. */
|
2018-01-23 04:39:58 +08:00
|
|
|
char key[2 + (sizeof(json) * 2) + 1];
|
2009-10-16 02:02:27 +08:00
|
|
|
|
|
|
|
/* detect circular references */
|
2019-08-12 11:17:25 +08:00
|
|
|
if (jsonp_loop_check(parents, json, key, sizeof(key)))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-10-16 02:02:27 +08:00
|
|
|
|
|
|
|
n = json_array_size(json);
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!embed && dump("[", 1, data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2019-10-17 14:08:51 +08:00
|
|
|
if (n == 0) {
|
2020-01-10 01:40:47 +08:00
|
|
|
hashtable_del(parents, key, strlen(key));
|
2017-02-28 03:56:55 +08:00
|
|
|
return embed ? 0 : dump("]", 1, data);
|
2010-05-12 20:41:09 +08:00
|
|
|
}
|
2019-10-17 14:08:51 +08:00
|
|
|
if (dump_indent(flags, depth + 1, 0, dump, data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
for (i = 0; i < n; ++i) {
|
2019-10-20 01:35:28 +08:00
|
|
|
if (do_dump(json_array_get(json, i), flags, depth + 1, parents, dump,
|
|
|
|
data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (i < n - 1) {
|
|
|
|
if (dump(",", 1, data) ||
|
|
|
|
dump_indent(flags, depth + 1, 1, dump, data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2019-10-17 14:08:51 +08:00
|
|
|
} else {
|
|
|
|
if (dump_indent(flags, depth, 0, dump, data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
2009-10-16 02:02:27 +08:00
|
|
|
|
2020-01-10 01:40:47 +08:00
|
|
|
hashtable_del(parents, key, strlen(key));
|
2017-02-28 03:56:55 +08:00
|
|
|
return embed ? 0 : dump("]", 1, data);
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
case JSON_OBJECT: {
|
2009-10-16 02:02:27 +08:00
|
|
|
void *iter;
|
2009-11-05 03:24:22 +08:00
|
|
|
const char *separator;
|
|
|
|
int separator_length;
|
2019-09-16 15:50:59 +08:00
|
|
|
char loop_key[LOOP_KEY_LEN];
|
2009-11-05 03:24:22 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (flags & JSON_COMPACT) {
|
2009-11-05 03:24:22 +08:00
|
|
|
separator = ":";
|
|
|
|
separator_length = 1;
|
2019-10-17 14:08:51 +08:00
|
|
|
} else {
|
2009-11-05 03:24:22 +08:00
|
|
|
separator = ": ";
|
|
|
|
separator_length = 2;
|
|
|
|
}
|
2009-10-16 02:02:27 +08:00
|
|
|
|
|
|
|
/* detect circular references */
|
2019-08-12 11:17:25 +08:00
|
|
|
if (jsonp_loop_check(parents, json, loop_key, sizeof(loop_key)))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-10-16 02:02:27 +08:00
|
|
|
|
|
|
|
iter = json_object_iter((json_t *)json);
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!embed && dump("{", 1, data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!iter) {
|
2020-01-10 01:40:47 +08:00
|
|
|
hashtable_del(parents, loop_key, strlen(loop_key));
|
2017-02-28 03:56:55 +08:00
|
|
|
return embed ? 0 : dump("}", 1, data);
|
2010-05-12 20:41:09 +08:00
|
|
|
}
|
2019-10-17 14:08:51 +08:00
|
|
|
if (dump_indent(flags, depth + 1, 0, dump, data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (flags & JSON_SORT_KEYS) {
|
2016-06-21 02:03:02 +08:00
|
|
|
const char **keys;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t size, i;
|
2009-12-07 19:16:45 +08:00
|
|
|
|
|
|
|
size = json_object_size(json);
|
2016-06-21 02:03:02 +08:00
|
|
|
keys = jsonp_malloc(size * sizeof(const char *));
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!keys)
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2009-12-07 19:16:45 +08:00
|
|
|
i = 0;
|
2019-10-17 14:08:51 +08:00
|
|
|
while (iter) {
|
2016-06-21 02:03:02 +08:00
|
|
|
keys[i] = json_object_iter_key(iter);
|
2009-12-07 19:16:45 +08:00
|
|
|
iter = json_object_iter_next((json_t *)json, iter);
|
|
|
|
i++;
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
2009-12-07 19:16:45 +08:00
|
|
|
assert(i == size);
|
|
|
|
|
2016-06-21 02:03:02 +08:00
|
|
|
qsort(keys, size, sizeof(const char *), compare_keys);
|
2009-12-07 19:16:45 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
for (i = 0; i < size; i++) {
|
2009-12-07 19:16:45 +08:00
|
|
|
const char *key;
|
|
|
|
json_t *value;
|
|
|
|
|
2016-06-21 02:03:02 +08:00
|
|
|
key = keys[i];
|
2009-12-07 19:16:45 +08:00
|
|
|
value = json_object_get(json, key);
|
|
|
|
assert(value);
|
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
dump_string(key, strlen(key), dump, data, flags);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (dump(separator, separator_length, data) ||
|
|
|
|
do_dump(value, flags, depth + 1, parents, dump, data)) {
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(keys);
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-12-07 19:16:45 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (i < size - 1) {
|
|
|
|
if (dump(",", 1, data) ||
|
|
|
|
dump_indent(flags, depth + 1, 1, dump, data)) {
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(keys);
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-12-07 19:16:45 +08:00
|
|
|
}
|
2019-10-17 14:08:51 +08:00
|
|
|
} else {
|
|
|
|
if (dump_indent(flags, depth, 0, dump, data)) {
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(keys);
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-12-07 19:16:45 +08:00
|
|
|
}
|
|
|
|
}
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
|
|
|
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(keys);
|
2019-10-17 14:08:51 +08:00
|
|
|
} else {
|
2009-12-07 19:16:45 +08:00
|
|
|
/* Don't sort keys */
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
while (iter) {
|
2009-12-07 19:16:45 +08:00
|
|
|
void *next = json_object_iter_next((json_t *)json, iter);
|
2012-04-04 09:00:29 +08:00
|
|
|
const char *key = json_object_iter_key(iter);
|
2009-12-07 19:16:45 +08:00
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
dump_string(key, strlen(key), dump, data, flags);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (dump(separator, separator_length, data) ||
|
2019-10-20 01:35:28 +08:00
|
|
|
do_dump(json_object_iter_value(iter), flags, depth + 1, parents,
|
|
|
|
dump, data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-12-07 19:16:45 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (next) {
|
|
|
|
if (dump(",", 1, data) ||
|
|
|
|
dump_indent(flags, depth + 1, 1, dump, data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2019-10-17 14:08:51 +08:00
|
|
|
} else {
|
|
|
|
if (dump_indent(flags, depth, 0, dump, data))
|
2018-01-23 04:39:58 +08:00
|
|
|
return -1;
|
2009-12-07 19:16:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
iter = next;
|
|
|
|
}
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
2009-10-16 02:02:27 +08:00
|
|
|
|
2020-01-10 01:40:47 +08:00
|
|
|
hashtable_del(parents, loop_key, strlen(loop_key));
|
2017-02-28 03:56:55 +08:00
|
|
|
return embed ? 0 : dump("}", 1, data);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2009-05-13 02:34:14 +08:00
|
|
|
/* not reached */
|
|
|
|
return -1;
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
char *json_dumps(const json_t *json, size_t flags) {
|
2009-06-11 13:56:11 +08:00
|
|
|
strbuffer_t strbuff;
|
2009-06-10 03:37:31 +08:00
|
|
|
char *result;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (strbuffer_init(&strbuff))
|
2009-10-17 02:20:38 +08:00
|
|
|
return NULL;
|
2009-06-11 13:56:11 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
|
2011-09-25 02:10:23 +08:00
|
|
|
result = NULL;
|
|
|
|
else
|
|
|
|
result = jsonp_strdup(strbuffer_value(&strbuff));
|
2009-06-10 03:37:31 +08:00
|
|
|
|
2009-06-11 13:56:11 +08:00
|
|
|
strbuffer_close(&strbuff);
|
2009-06-10 03:37:31 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags) {
|
|
|
|
struct buffer buf = {size, 0, buffer};
|
2017-01-26 16:46:48 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_dump_callback(json, dump_to_buffer, (void *)&buf, flags))
|
2017-01-26 16:46:48 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return buf.used;
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
int json_dumpf(const json_t *json, FILE *output, size_t flags) {
|
2011-09-25 02:10:23 +08:00
|
|
|
return json_dump_callback(json, dump_to_file, (void *)output, flags);
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
2009-07-28 15:57:17 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
int json_dumpfd(const json_t *json, int output, size_t flags) {
|
2017-01-27 00:23:31 +08:00
|
|
|
return json_dump_callback(json, dump_to_fd, (void *)&output, flags);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
int json_dump_file(const json_t *json, const char *path, size_t flags) {
|
2009-07-28 15:57:17 +08:00
|
|
|
int result;
|
|
|
|
|
2011-11-15 03:15:17 +08:00
|
|
|
FILE *output = fopen(path, "w");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!output)
|
2009-07-28 15:57:17 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
result = json_dumpf(json, output, flags);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (fclose(output) != 0)
|
2017-08-06 02:22:49 +08:00
|
|
|
return -1;
|
|
|
|
|
2009-07-28 15:57:17 +08:00
|
|
|
return result;
|
|
|
|
}
|
2011-06-25 01:00:53 +08:00
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data,
|
|
|
|
size_t flags) {
|
2018-01-23 04:39:58 +08:00
|
|
|
int res;
|
|
|
|
hashtable_t parents_set;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!(flags & JSON_ENCODE_ANY)) {
|
|
|
|
if (!json_is_array(json) && !json_is_object(json))
|
|
|
|
return -1;
|
2011-06-25 01:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-01-23 04:39:58 +08:00
|
|
|
if (hashtable_init(&parents_set))
|
|
|
|
return -1;
|
|
|
|
res = do_dump(json, flags, 0, &parents_set, callback, data);
|
|
|
|
hashtable_close(&parents_set);
|
|
|
|
|
|
|
|
return res;
|
2011-06-25 01:00:53 +08:00
|
|
|
}
|