json-format.c - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       json-format.c (1115B)
       ---
            1 /* Simple JSON formatter, does not validate, does not print control chars. */
            2 
            3 #include <ctype.h>
            4 #include <stdio.h>
            5 
            6 int depth = 0;
            7 
            8 void
            9 indent(void)
           10 {
           11         int i;
           12 
           13         for (i = 0; i < depth; i++)
           14                 putchar('\t'); /* replace with spaces if you're a heathen. */
           15 }
           16 
           17 void
           18 newline(void)
           19 {
           20         putchar('\n');
           21 }
           22 
           23 int
           24 main(void)
           25 {
           26         int c, escape, p = 1;
           27 
           28         indent();
           29         while ((c = getchar()) != EOF) {
           30                 if (isspace(c) || iscntrl(c))
           31                         continue;
           32 
           33                 switch (c) {
           34                 case ':':
           35                         putchar(' ');
           36                         break;
           37                 case ']':
           38                 case '}':
           39                         p = 0; /* force indent */
           40                         depth--; /* FALLTHROUGH */
           41                 case '{':
           42                 case '[':
           43                         if (!p) {
           44                                 newline();
           45                                 indent();
           46                                 p = 1;
           47                         }
           48                         break;
           49                 }
           50 
           51                 putchar(c);
           52 
           53                 switch (c) {
           54                 case ':':
           55                         putchar(' ');
           56                         break;
           57                 case '"':
           58                         for (escape = 0; (c = getchar()) != EOF;) {
           59                                 if (iscntrl(c))
           60                                         continue;
           61                                 putchar(c);
           62 
           63                                 if (escape)
           64                                         escape = 0;
           65                                 else if (c == '\\')
           66                                         escape = 1;
           67                                 else if (c == '"')
           68                                         break;
           69                         }
           70                         break;
           71                 case '[':
           72                 case '{':
           73                         depth++;
           74                 case ',': /* FALLTHROUGH */
           75                         newline();
           76                         indent();
           77                         p = 1;
           78                         break;
           79                 }
           80         }
           81         newline();
           82 
           83         return 0;
           84 }