fdopen() makes supporting separate API for file descriptors useless.
Supporting fd's also makes Jansson less portable.
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)
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);
-}
json_t *json_load(const char *path, json_error_t *error);
json_t *json_loads(const char *input, json_error_t *error);
json_t *json_loadf(FILE *input, json_error_t *error);
-json_t *json_loadfd(int fd, json_error_t *error);
#define JSON_INDENT(n) (n & 0xFF)
#define JSON_SORT_KEYS 0x100
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
strbuffer_close(&strbuff);
return result;
}
-
-json_t *json_loadfd(int fd, json_error_t *error)
-{
- strbuffer_t strbuff;
- char buffer[BUFFER_SIZE];
- ssize_t length;
- json_t *result = NULL;
-
- if(strbuffer_init(&strbuff))
- return NULL;
-
- while(1)
- {
- length = read(fd, buffer, BUFFER_SIZE);
- if(length == -1)
- {
- error_set(error, NULL, "read error: %s", strerror(errno));
- goto out;
- }
- else if(length == 0)
- break;
-
- if(strbuffer_append_bytes(&strbuff, buffer, length))
- {
- error_set(error, NULL, "error allocating memory");
- goto out;
- }
- }
-
- result = json_loads(strbuffer_value(&strbuff), error);
-
-out:
- strbuffer_close(&strbuff);
- return result;
-}
load_dump
loadf_dumpf
-loadfd_dumpfd
loads_dumps
-check_PROGRAMS = load_dump loadf_dumpf loadfd_dumpfd loads_dumps
+check_PROGRAMS = load_dump loadf_dumpf loads_dumps
AM_CPPFLAGS = -I$(top_srcdir)/src
AM_CFLAGS = -Wall -Werror
+++ /dev/null
-#include <stdio.h>
-#include <unistd.h>
-#include <jansson.h>
-
-int main(int argc, char *argv[])
-{
- json_t *json;
- json_error_t error;
-
- if(argc != 1) {
- fprintf(stderr, "usage: %s\n", argv[0]);
- return 2;
- }
-
- json = json_loadfd(STDIN_FILENO, &error);
- if(!json) {
- fprintf(stderr, "%d\n%s\n", error.line, error.text);
- return 1;
- }
-
- json_dumpfd(json, STDOUT_FILENO, 0);
- json_decref(json);
-
- return 0;
-}
while read input output; do
run_test load_dump $input $output
run_test loadf_dumpf $input $output
- run_test loadfd_dumpfd $input $output
run_test loads_dumps $input $output
done