Implement dumping to an fd
authorPetri Lehtinen <petri@digip.org>
Sat, 16 May 2009 12:15:01 +0000 (15:15 +0300)
committerPetri Lehtinen <petri@digip.org>
Sat, 16 May 2009 12:15:01 +0000 (15:15 +0300)
src/dump.c
src/jansson.h

index 3a88d63..3bf01fd 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <jansson.h>
 
@@ -15,6 +16,14 @@ static int dump_to_file(const char *buffer, int size, void *data)
     return 0;
 }
 
+static int dump_to_fd(const char *buffer, int size, void *data)
+{
+    int *fd = (int *)data;
+    if(write(*fd, buffer, size) != size)
+        return -1;
+    return 0;
+}
+
 static int dump_indent(uint32_t flags, int depth, dump_func dump, void *data)
 {
     if(JSON_INDENT(flags) > 0)
@@ -184,3 +193,10 @@ int json_dumpf(const json_t *json, FILE *output, uint32_t flags)
         return -1;
     return dump_to_file("\n", 1, (void *)output);
 }
+
+int json_dumpfd(const json_t *json, int fd, uint32_t flags)
+{
+    if(do_dump(json, flags, 0, dump_to_fd, (void *)&fd))
+        return -1;
+    return dump_to_fd("\n", 1, (void *)&fd);
+}
index 0fd77f1..bb15b6c 100644 (file)
@@ -97,5 +97,6 @@ json_t *json_loadf(FILE *input, json_error_t *error);
 int json_dump(const json_t *json, const char *path, uint32_t flags);
 char *json_dumps(const json_t *json, uint32_t flags);
 int json_dumpf(const json_t *json, FILE *output, uint32_t flags);
+int json_dumpfd(const json_t *json, int fd, uint32_t flags);
 
 #endif