#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
#include <jansson.h>
#include "strbuffer.h"
return 0;
}
-static int dump_indent(uint32_t flags, int depth, dump_func dump, void *data)
+/* 256 spaces (the maximum indentation size) */
+static char whitespace[] = " ";
+
+static int dump_indent(unsigned long flags, int depth, dump_func dump, void *data)
{
if(JSON_INDENT(flags) > 0)
{
- char *ws_buffer;
- int ws_count = JSON_INDENT(flags) * depth;
+ int i, ws_count = JSON_INDENT(flags);
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);
+ for(i = 0; i < depth; i++)
+ {
+ if(dump(whitespace, ws_count, data))
+ return -1;
+ }
}
return 0;
}
return dump("\"", 1, data);
}
-static int do_dump(const json_t *json, uint32_t flags, int depth,
+static int do_dump(const json_t *json, unsigned long flags, int depth,
dump_func dump, void *data)
{
switch(json_typeof(json)) {
}
-char *json_dumps(const json_t *json, uint32_t flags)
+char *json_dumps(const json_t *json, unsigned long flags)
{
strbuffer_t strbuff;
char *result;
+ if(!json_is_array(json) && !json_is_object(json))
+ return NULL;
+
if(strbuffer_init(&strbuff))
return NULL;
return result;
}
-int json_dumpf(const json_t *json, FILE *output, uint32_t flags)
+int json_dumpf(const json_t *json, FILE *output, unsigned long flags)
{
+ if(!json_is_array(json) && !json_is_object(json))
+ return -1;
+
if(do_dump(json, flags, 0, dump_to_file, (void *)output))
return -1;
return dump_to_file("\n", 1, (void *)output);
}
-int json_dump_file(const json_t *json, const char *path, uint32_t flags)
+int json_dump_file(const json_t *json, const char *path, unsigned long flags)
{
int result;