jansson/src/dump.c

438 lines
12 KiB
C
Raw Normal View History

/*
2010-02-03 03:26:11 +08:00
* Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
2009-05-13 02:34:14 +08:00
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
2009-05-13 02:34:14 +08:00
2009-02-07 02:26:27 +08:00
#include <jansson.h>
#include "jansson_private.h"
#include "strbuffer.h"
#include "utf.h"
2009-02-07 02:26:27 +08:00
#define MAX_INTEGER_STR_LENGTH 100
#define MAX_REAL_STR_LENGTH 100
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;
};
static int dump_to_strbuffer(const char *buffer, int size, void *data)
2009-06-10 03:37:31 +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;
}
/* 256 spaces (the maximum indentation size) */
static char whitespace[] = " ";
static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data)
2009-05-13 02:34:14 +08:00
{
if(JSON_INDENT(flags) > 0)
{
int i, ws_count = JSON_INDENT(flags);
2009-05-13 02:34:14 +08:00
if(dump("\n", 1, data))
return -1;
for(i = 0; i < depth; i++)
{
if(dump(whitespace, ws_count, data))
return -1;
}
2009-05-13 02:34:14 +08:00
}
else if(space && !(flags & JSON_COMPACT))
{
return dump(" ", 1, 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, int ascii, dump_func dump, void *data)
2009-02-07 02:26:27 +08:00
{
const char *pos, *end;
int32_t codepoint;
2009-05-13 02:34:14 +08:00
if(dump("\"", 1, data))
return -1;
end = pos = str;
while(1)
2009-05-13 02:34:14 +08:00
{
const char *text;
char seq[13];
int length;
while(*end)
{
end = utf8_iterate(pos, &codepoint);
if(!end)
return -1;
2009-05-13 02:34:14 +08:00
/* mandatory escape or control char */
if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
break;
/* non-ASCII */
if(ascii && codepoint > 0x7F)
break;
pos = end;
}
if(pos != str) {
if(dump(str, pos - str, data))
2009-05-13 02:34:14 +08:00
return -1;
}
if(end == pos)
break;
/* handle \, ", and control codes */
length = 2;
switch(codepoint)
2009-05-13 02:34:14 +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:
{
/* codepoint is in BMP */
if(codepoint < 0x10000)
{
sprintf(seq, "\\u%04x", codepoint);
length = 6;
}
/* not in BMP -> construct a UTF-16 surrogate pair */
else
{
int32_t first, last;
codepoint -= 0x10000;
first = 0xD800 | ((codepoint & 0xffc00) >> 10);
last = 0xDC00 | (codepoint & 0x003ff);
sprintf(seq, "\\u%04x\\u%04x", first, last);
length = 12;
}
text = seq;
break;
}
2009-05-13 02:34:14 +08:00
}
if(dump(text, length, data))
return -1;
str = pos = end;
2009-05-13 02:34:14 +08:00
}
return dump("\"", 1, data);
}
static int object_key_cmp(const void *key1, const void *key2)
{
return strcmp(*(const char **)key1, *(const char **)key2);
}
static int do_dump(const json_t *json, unsigned long flags, int depth,
2009-05-13 02:34:14 +08:00
dump_func dump, void *data)
{
int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
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[MAX_INTEGER_STR_LENGTH];
int size;
2009-05-13 02:34:14 +08:00
size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json));
if(size >= MAX_INTEGER_STR_LENGTH)
2009-06-23 05:14:28 +08:00
return -1;
return dump(buffer, size, data);
2009-06-23 05:14:28 +08:00
}
case JSON_REAL:
{
char buffer[MAX_REAL_STR_LENGTH];
int size;
2009-06-23 05:14:28 +08:00
size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g",
json_real_value(json));
if(size >= MAX_REAL_STR_LENGTH)
2009-05-13 02:34:14 +08:00
return -1;
/* Make sure there's a dot or 'e' in the output. Otherwise
a real is converted to an integer when decoding */
if(strchr(buffer, '.') == NULL &&
strchr(buffer, 'e') == NULL)
{
if(size + 2 >= MAX_REAL_STR_LENGTH) {
/* No space to append ".0" */
return -1;
}
buffer[size] = '.';
buffer[size + 1] = '0';
size += 2;
}
return dump(buffer, size, data);
2009-05-13 02:34:14 +08:00
}
2009-02-07 02:26:27 +08:00
case JSON_STRING:
return dump_string(json_string_value(json), ascii, dump, data);
2009-05-13 02:34:14 +08:00
case JSON_ARRAY:
{
int i;
int n;
json_array_t *array;
/* detect circular references */
array = json_to_array(json);
if(array->visited)
return -1;
array->visited = 1;
n = json_array_size(json);
2009-05-13 02:34:14 +08:00
if(dump("[", 1, data))
return -1;
if(n == 0)
2009-07-04 04:22:47 +08:00
return dump("]", 1, data);
if(dump_indent(flags, depth + 1, 0, dump, data))
2009-05-13 02:34:14 +08:00
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, 1, dump, data))
2009-05-13 02:34:14 +08:00
return -1;
}
else
{
if(dump_indent(flags, depth, 0, dump, data))
2009-05-13 02:34:14 +08:00
return -1;
}
2009-02-07 02:26:27 +08:00
}
array->visited = 0;
2009-05-13 02:34:14 +08:00
return dump("]", 1, data);
}
case JSON_OBJECT:
{
json_object_t *object;
void *iter;
const char *separator;
int separator_length;
if(flags & JSON_COMPACT) {
separator = ":";
separator_length = 1;
}
else {
separator = ": ";
separator_length = 2;
}
/* detect circular references */
object = json_to_object(json);
if(object->visited)
return -1;
object->visited = 1;
iter = json_object_iter((json_t *)json);
2009-05-13 02:34:14 +08:00
if(dump("{", 1, data))
return -1;
if(!iter)
return dump("}", 1, data);
if(dump_indent(flags, depth + 1, 0, dump, data))
2009-05-13 02:34:14 +08:00
return -1;
if(flags & JSON_SORT_KEYS)
2009-05-13 02:34:14 +08:00
{
/* Sort keys */
2009-05-13 02:34:14 +08:00
const char **keys;
unsigned int size;
unsigned int i;
size = json_object_size(json);
keys = malloc(size * sizeof(const char *));
if(!keys)
2009-05-13 02:34:14 +08:00
return -1;
i = 0;
while(iter)
2009-05-13 02:34:14 +08:00
{
keys[i] = json_object_iter_key(iter);
iter = json_object_iter_next((json_t *)json, iter);
i++;
2009-05-13 02:34:14 +08:00
}
assert(i == size);
qsort(keys, size, sizeof(const char *), object_key_cmp);
for(i = 0; i < size; i++)
2009-05-13 02:34:14 +08:00
{
const char *key;
json_t *value;
key = keys[i];
value = json_object_get(json, key);
assert(value);
dump_string(key, ascii, dump, data);
if(dump(separator, separator_length, data) ||
do_dump(value, flags, depth + 1, dump, data))
{
free(keys);
2009-05-13 02:34:14 +08:00
return -1;
}
if(i < size - 1)
{
if(dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data))
{
free(keys);
return -1;
}
}
else
{
if(dump_indent(flags, depth, 0, dump, data))
{
free(keys);
return -1;
}
}
2009-05-13 02:34:14 +08:00
}
free(keys);
}
else
{
/* Don't sort keys */
while(iter)
{
void *next = json_object_iter_next((json_t *)json, iter);
dump_string(json_object_iter_key(iter), ascii, dump, data);
if(dump(separator, separator_length, 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, 1, dump, data))
return -1;
}
else
{
if(dump_indent(flags, depth, 0, dump, data))
return -1;
}
iter = next;
}
2009-05-13 02:34:14 +08:00
}
object->visited = 0;
2009-05-13 02:34:14 +08:00
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
char *json_dumps(const json_t *json, unsigned long flags)
2009-06-10 03:37:31 +08:00
{
strbuffer_t strbuff;
2009-06-10 03:37:31 +08:00
char *result;
if(!json_is_array(json) && !json_is_object(json))
return NULL;
if(strbuffer_init(&strbuff))
2009-10-17 02:20:38 +08:00
return NULL;
if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) {
strbuffer_close(&strbuff);
2009-06-10 03:37:31 +08:00
return NULL;
}
2009-06-10 03:37:31 +08:00
result = strdup(strbuffer_value(&strbuff));
strbuffer_close(&strbuff);
2009-06-10 03:37:31 +08:00
return result;
}
int json_dumpf(const json_t *json, FILE *output, unsigned long flags)
2009-05-13 02:34:14 +08:00
{
if(!json_is_array(json) && !json_is_object(json))
return -1;
return do_dump(json, flags, 0, dump_to_file, (void *)output);
2009-05-13 02:34:14 +08:00
}
int json_dump_file(const json_t *json, const char *path, unsigned long flags)
{
int result;
FILE *output = fopen(path, "w");
if(!output)
return -1;
result = json_dumpf(json, output, flags);
fclose(output);
return result;
}