Browse Source

jim: implement basic pretty-printing

rexim 4 weeks ago
parent
commit
1e9f5f9e0f
3 changed files with 106 additions and 34 deletions
  1. 55 25
      README.md
  2. 19 1
      examples/01_from_readme.c
  3. 32 8
      jim2.h

+ 55 - 25
README.md

@@ -14,7 +14,7 @@ Immediate Mode JSON Serialization Library in C. Similar to [imgui](https://githu
 
 int main()
 {
-    Jim jim = {0};
+    Jim jim = {.pp = 4};
 
     jim_object_begin(&jim);
         jim_member_key(&jim, "null");
@@ -50,6 +50,24 @@ int main()
             jim_string(&jim, "Hello\tWorld\n");
             jim_string_sized(&jim, "\0\0\0\0", 4);
         jim_array_end(&jim);
+
+        jim_member_key(&jim, "nested_object");
+        jim_object_begin(&jim);
+            jim_member_key(&jim, "foo");
+            jim_integer(&jim, 69);
+            jim_member_key(&jim, "bar");
+            jim_integer(&jim, 420);
+            jim_member_key(&jim, "baz");
+            jim_integer(&jim, 1337);
+        jim_object_end(&jim);
+
+        jim_member_key(&jim, "empty_array"),
+        jim_array_begin(&jim);
+        jim_array_end(&jim);
+
+        jim_member_key(&jim, "empty_object"),
+        jim_object_begin(&jim);
+        jim_object_end(&jim);
     jim_object_end(&jim);
 
     fwrite(jim.sink, jim.sink_count, 1, stdout);
@@ -62,31 +80,43 @@ int main()
 
 ```console
 $ cc -o example example.c
-$ ./example | jq .
+$ ./example
 {
-  "null": null,
-  "bool": [
-    false,
-    true
-  ],
-  "integers": [
-    -3,
-    -2,
-    -1,
-    0,
-    1,
-    2,
-    3
-  ],
-  "floats": [
-    3.1415,
-    2.71828,
-    1.618
-  ],
-  "string": [
-    "Hello\tWorld\n",
-    "\u0000\u0000\u0000\u0000"
-  ]
+    "null": null,
+    "bool": [
+        false,
+        true
+    ],
+    "integers": [
+        -3,
+        -2,
+        -1,
+        0,
+        1,
+        2,
+        3
+    ],
+    "floats": [
+        0.0,
+        0.0,
+        3.1415,
+        2.71828,
+        1.6180,
+        null,
+        null,
+        null
+    ],
+    "string": [
+        "Hello\tWorld\n",
+        "\u0000\u0000\u0000\u0000"
+    ],
+    "nested_object": {
+        "foo": 69,
+        "bar": 420,
+        "baz": 1337
+    },
+    "empty_array": [],
+    "empty_object": {}
 }
 ```
 

+ 19 - 1
examples/01_from_readme.c

@@ -5,7 +5,7 @@
 
 int main()
 {
-    Jim jim = {0};
+    Jim jim = {.pp = 4};
 
     jim_object_begin(&jim);
         jim_member_key(&jim, "null");
@@ -41,6 +41,24 @@ int main()
             jim_string(&jim, "Hello\tWorld\n");
             jim_string_sized(&jim, "\0\0\0\0", 4);
         jim_array_end(&jim);
+
+        jim_member_key(&jim, "nested_object");
+        jim_object_begin(&jim);
+            jim_member_key(&jim, "foo");
+            jim_integer(&jim, 69);
+            jim_member_key(&jim, "bar");
+            jim_integer(&jim, 420);
+            jim_member_key(&jim, "baz");
+            jim_integer(&jim, 1337);
+        jim_object_end(&jim);
+
+        jim_member_key(&jim, "empty_array"),
+        jim_array_begin(&jim);
+        jim_array_end(&jim);
+
+        jim_member_key(&jim, "empty_object"),
+        jim_object_begin(&jim);
+        jim_object_end(&jim);
     jim_object_end(&jim);
 
     fwrite(jim.sink, jim.sink_count, 1, stdout);

+ 32 - 8
jim2.h

@@ -14,6 +14,7 @@
 
 #include <assert.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 
 typedef enum {
@@ -23,8 +24,8 @@ typedef enum {
 
 typedef struct {
     Jim_Scope_Kind kind;
-    int tail;
-    int key;
+    int tail;                   // Not the first element in an array or an object
+    int key;                    // An object key was just placed
 } Jim_Scope;
 
 typedef struct {
@@ -34,12 +35,9 @@ typedef struct {
     Jim_Scope *scopes;
     size_t scopes_count;
     size_t scopes_capacity;
+    size_t pp;
 } Jim;
 
-// TODO: implement pretty-printing based on how nested the scopes are.
-//   Introduce a separate boolean flag in the Jim struct to enable/disable
-//   the pretty-printing.
-
 void jim_begin(Jim *jim);
 void jim_null(Jim *jim);
 void jim_bool(Jim *jim, int boolean);
@@ -131,8 +129,20 @@ void jim_begin(Jim *jim)
 void jim_element_begin(Jim *jim)
 {
     Jim_Scope *scope = jim_current_scope(jim);
-    if (scope && scope->tail && !scope->key) {
-        jim_write_cstr(jim, ",");
+    if (scope) {
+        if (scope->tail && !scope->key) {
+            jim_write_cstr(jim, ",");
+        }
+        if (jim->pp) {
+            if (scope->key) {
+                jim_write_cstr(jim, " ");
+            } else {
+                jim_write_cstr(jim, "\n");
+                for (size_t i = 0; i < jim->scopes_count*jim->pp; ++i) {
+                    jim_write_cstr(jim, " ");
+                }
+            }
+        }
     }
 }
 
@@ -282,6 +292,13 @@ void jim_array_begin(Jim *jim)
 
 void jim_array_end(Jim *jim)
 {
+    Jim_Scope *scope = jim_current_scope(jim);
+    if (jim->pp && scope && scope->tail) {
+        jim_write_cstr(jim, "\n");
+        for (size_t i = 0; i < (jim->scopes_count - 1)*jim->pp; ++i) {
+            jim_write_cstr(jim, " ");
+        }
+    }
     jim_write_cstr(jim, "]");
     jim_scope_pop(jim);
     jim_element_end(jim);
@@ -313,6 +330,13 @@ void jim_member_key_sized(Jim *jim, const char *str, size_t size)
 
 void jim_object_end(Jim *jim)
 {
+    Jim_Scope *scope = jim_current_scope(jim);
+    if (jim->pp && scope && scope->tail) {
+        jim_write_cstr(jim, "\n");
+        for (size_t i = 0; i < (jim->scopes_count - 1)*jim->pp; ++i) {
+            jim_write_cstr(jim, " ");
+        }
+    }
     jim_write_cstr(jim, "}");
     jim_scope_pop(jim);
     jim_element_end(jim);