2009-05-13 02:34:14 +08:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-05-16 20:15:01 +08:00
|
|
|
#include <unistd.h>
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
#include <jansson.h>
|
2009-06-11 13:56:11 +08:00
|
|
|
#include "strbuffer.h"
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-05-13 02:34:14 +08:00
|
|
|
typedef int (*dump_func)(const char *buffer, int size, void *data);
|
|
|
|
|
2009-06-10 03:37:31 +08:00
|
|
|
struct string
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
int length;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
2009-06-11 13:56:11 +08:00
|
|
|
static int dump_to_strbuffer(const char *buffer, int size, void *data)
|
2009-06-10 03:37:31 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2009-05-13 02:34:14 +08:00
|
|
|
static int dump_to_file(const char *buffer, int size, void *data)
|
|
|
|
{
|
|
|
|
FILE *dest = (FILE *)data;
|
|
|
|
if(fwrite(buffer, size, 1, dest) != 1)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_indent(uint32_t flags, int depth, dump_func dump, void *data)
|
|
|
|
{
|
|
|
|
if(JSON_INDENT(flags) > 0)
|
|
|
|
{
|
|
|
|
char *ws_buffer;
|
|
|
|
int ws_count = JSON_INDENT(flags) * depth;
|
|
|
|
|
|
|
|
if(dump("\n", 1, data))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(ws_count == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ws_buffer = alloca(ws_count);
|
|
|
|
memset(ws_buffer, ' ', ws_count);
|
|
|
|
return dump(ws_buffer, ws_count, data);
|
|
|
|
}
|
2009-07-10 01:54:29 +08:00
|
|
|
return 0;
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_string(const char *str, dump_func dump, void *data)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2009-05-13 02:34:14 +08:00
|
|
|
const char *end;
|
|
|
|
|
|
|
|
if(dump("\"", 1, data))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
end = str;
|
2009-07-05 03:02:16 +08:00
|
|
|
while(1)
|
2009-05-13 02:34:14 +08:00
|
|
|
{
|
2009-07-05 03:02:16 +08:00
|
|
|
const char *text;
|
|
|
|
char seq[7];
|
|
|
|
int length;
|
|
|
|
|
|
|
|
while(*end && *end != '\\' && *end != '"' && (*end < 0 || *end > 0x1F))
|
2009-05-13 02:34:14 +08:00
|
|
|
end++;
|
|
|
|
|
2009-07-05 03:02:16 +08:00
|
|
|
if(end != str) {
|
2009-05-13 02:34:14 +08:00
|
|
|
if(dump(str, end - str, data))
|
|
|
|
return -1;
|
|
|
|
}
|
2009-07-05 03:02:16 +08:00
|
|
|
|
|
|
|
if(!*end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* handle \, ", and control codes */
|
|
|
|
length = 2;
|
|
|
|
switch(*end)
|
2009-05-13 02:34:14 +08:00
|
|
|
{
|
2009-07-05 03:02:16 +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;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
sprintf(seq, "\\u00%02x", *end);
|
|
|
|
text = seq;
|
|
|
|
length = 6;
|
|
|
|
break;
|
|
|
|
}
|
2009-05-13 02:34:14 +08:00
|
|
|
}
|
2009-07-05 03:02:16 +08:00
|
|
|
|
|
|
|
if(dump(text, length, data))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
end++;
|
2009-05-13 02:34:14 +08:00
|
|
|
str = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dump("\"", 1, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_dump(const json_t *json, uint32_t flags, int depth,
|
|
|
|
dump_func dump, void *data)
|
|
|
|
{
|
2009-02-07 02:26:27 +08:00
|
|
|
switch(json_typeof(json)) {
|
|
|
|
case JSON_NULL:
|
2009-05-13 02:34:14 +08:00
|
|
|
return dump("null", 4, data);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
|
|
|
case JSON_TRUE:
|
2009-05-13 02:34:14 +08:00
|
|
|
return dump("true", 4, data);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
|
|
|
case JSON_FALSE:
|
2009-05-13 02:34:14 +08:00
|
|
|
return dump("false", 5, data);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-06-23 05:14:28 +08:00
|
|
|
case JSON_INTEGER:
|
2009-05-13 02:34:14 +08:00
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
int size, ret;
|
|
|
|
|
2009-06-23 05:14:28 +08:00
|
|
|
size = asprintf(&buffer, "%d", json_integer_value(json));
|
|
|
|
if(size == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = dump(buffer, size, data);
|
|
|
|
free(buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSON_REAL:
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
int size, ret;
|
|
|
|
|
|
|
|
size = asprintf(&buffer, "%.17f", json_real_value(json));
|
2009-05-13 02:34:14 +08:00
|
|
|
if(size == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = dump(buffer, size, data);
|
|
|
|
free(buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
|
|
|
|
case JSON_STRING:
|
2009-05-13 02:34:14 +08:00
|
|
|
return dump_string(json_string_value(json), dump, data);
|
|
|
|
|
|
|
|
case JSON_ARRAY:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n = json_array_size(json);
|
|
|
|
|
|
|
|
if(dump("[", 1, data))
|
|
|
|
return -1;
|
|
|
|
if(n == 0)
|
2009-07-04 04:22:47 +08:00
|
|
|
return dump("]", 1, data);
|
2009-05-13 02:34:14 +08:00
|
|
|
if(dump_indent(flags, depth + 1, dump, data))
|
|
|
|
return -1;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
|
|
|
for(i = 0; i < n; ++i) {
|
2009-05-13 02:34:14 +08:00
|
|
|
if(do_dump(json_array_get(json, i), flags, depth + 1,
|
|
|
|
dump, data))
|
|
|
|
return -1;
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
if(i < n - 1)
|
2009-05-13 02:34:14 +08:00
|
|
|
{
|
|
|
|
if(dump(",", 1, data) ||
|
|
|
|
dump_indent(flags, depth + 1, dump, data))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(dump_indent(flags, depth, dump, data))
|
|
|
|
return -1;
|
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
2009-05-13 02:34:14 +08:00
|
|
|
return dump("]", 1, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSON_OBJECT:
|
|
|
|
{
|
|
|
|
void *iter = json_object_iter((json_t *)json);
|
|
|
|
|
|
|
|
if(dump("{", 1, data))
|
|
|
|
return -1;
|
|
|
|
if(!iter)
|
|
|
|
return dump("}", 1, data);
|
|
|
|
if(dump_indent(flags, depth + 1, dump, data))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while(iter)
|
|
|
|
{
|
|
|
|
void *next = json_object_iter_next((json_t *)json, iter);
|
|
|
|
|
|
|
|
dump_string(json_object_iter_key(iter), dump, data);
|
|
|
|
if(dump(": ", 2, data) ||
|
|
|
|
do_dump(json_object_iter_value(iter), flags, depth + 1,
|
|
|
|
dump, data))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(next)
|
|
|
|
{
|
|
|
|
if(dump(",", 1, data) ||
|
|
|
|
dump_indent(flags, depth + 1, dump, data))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(dump_indent(flags, depth, dump, data))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter = next;
|
|
|
|
}
|
|
|
|
return 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:34:14 +08:00
|
|
|
|
2009-06-10 03:37:31 +08:00
|
|
|
int json_dump(const json_t *json, const char *path, uint32_t flags)
|
|
|
|
{
|
|
|
|
FILE *output = fopen(path, "w");
|
|
|
|
if(!output)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return json_dumpf(json, output, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *json_dumps(const json_t *json, uint32_t flags)
|
|
|
|
{
|
2009-06-11 13:56:11 +08:00
|
|
|
strbuffer_t strbuff;
|
2009-06-10 03:37:31 +08:00
|
|
|
char *result;
|
|
|
|
|
2009-06-23 02:09:25 +08:00
|
|
|
if(strbuffer_init(&strbuff))
|
|
|
|
return NULL;
|
2009-06-11 13:56:11 +08:00
|
|
|
|
|
|
|
if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff))
|
2009-06-10 03:37:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
2009-06-11 13:56:11 +08:00
|
|
|
if(dump_to_strbuffer("\n", 1, (void *)&strbuff))
|
2009-06-10 03:37:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
2009-06-14 03:26:45 +08:00
|
|
|
result = strdup(strbuffer_value(&strbuff));
|
2009-06-11 13:56:11 +08:00
|
|
|
strbuffer_close(&strbuff);
|
2009-06-10 03:37:31 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:34:14 +08:00
|
|
|
int json_dumpf(const json_t *json, FILE *output, uint32_t flags)
|
|
|
|
{
|
|
|
|
if(do_dump(json, flags, 0, dump_to_file, (void *)output))
|
|
|
|
return -1;
|
|
|
|
return dump_to_file("\n", 1, (void *)output);
|
|
|
|
}
|