Files
tippecanoe/jsoncat.c
T
Eric Fischer 979b9243d8 A container object makes the state transitions more straightforward
And should make it possible to free an object that has just been read
and still know what the next state will be, importantly.
2014-02-06 18:56:22 -08:00

111 lines
2.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include "jsonpull.h"
static void indent(int depth) {
int i;
for (i = 0; i < depth; i++) {
printf(" ");
}
}
void json_print(json_object *j, int depth) {
if (j == NULL) {
printf("NULL");
} else if (j->type == JSON_STRING) {
printf("\"");
char *cp;
for (cp = j->string; *cp != '\0'; cp++) {
if (*cp == '\\' || *cp == '"') {
printf("\\%c", *cp);
} else if (*cp >= 0 && *cp < ' ') {
printf("\\u%04x", *cp);
} else {
putchar(*cp);
}
}
printf("\"");
} else if (j->type == JSON_NUMBER) {
printf("%f", j->number);
} else if (j->type == JSON_NULL) {
printf("null");
} else if (j->type == JSON_TRUE) {
printf("true");
} else if (j->type == JSON_FALSE) {
printf("false");
} else if (j->type == JSON_HASH) {
printf("{\n");
indent(depth + 1);
int i;
for (i = 0; i < j->length; i++) {
json_print(j->keys[i], depth + 1);
printf(" : ");
json_print(j->values[i], depth + 1);
if (i + 1 < j->length) {
printf(",\n");
indent(depth + 1);
}
}
printf("\n");
indent(depth);
printf("}");
} else if (j->type == JSON_ARRAY) {
printf("[\n");
indent(depth + 1);
int i;
for (i = 0; i < j->length; i++) {
json_print(j->array[i], depth + 1);
if (i + 1 < j->length) {
printf(",\n");
indent(depth + 1);
}
}
printf("\n");
indent(depth);
printf("]");
} else {
printf("what type? %d", j->type);
}
}
void process(FILE *f) {
json_pull *jp = json_begin(f);
json_object *j = NULL;
while ((j = json_parse(jp)) != NULL) {
if (j->parent == NULL) {
json_print(j, 0);
printf("\n");
j = NULL;
}
}
if (jp->error != NULL) {
fprintf(stderr, "%s\n", jp->error);
}
}
int main(int argc, char **argv) {
if (argc == 1) {
process(stdin);
} else {
int i;
for (i = 1; i < argc; i++) {
FILE *f = fopen(argv[i], "r");
if (f == NULL) {
perror(argv[i]);
exit(EXIT_FAILURE);
}
process(f);
fclose(f);
}
}
}