Skip to content

Commit 1e9f5f9

Browse files
committed
jim: implement basic pretty-printing
1 parent a790483 commit 1e9f5f9

3 files changed

Lines changed: 106 additions & 34 deletions

File tree

README.md

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Immediate Mode JSON Serialization Library in C. Similar to [imgui](https://githu
1414

1515
int main()
1616
{
17-
Jim jim = {0};
17+
Jim jim = {.pp = 4};
1818

1919
jim_object_begin(&jim);
2020
jim_member_key(&jim, "null");
@@ -50,6 +50,24 @@ int main()
5050
jim_string(&jim, "Hello\tWorld\n");
5151
jim_string_sized(&jim, "\0\0\0\0", 4);
5252
jim_array_end(&jim);
53+
54+
jim_member_key(&jim, "nested_object");
55+
jim_object_begin(&jim);
56+
jim_member_key(&jim, "foo");
57+
jim_integer(&jim, 69);
58+
jim_member_key(&jim, "bar");
59+
jim_integer(&jim, 420);
60+
jim_member_key(&jim, "baz");
61+
jim_integer(&jim, 1337);
62+
jim_object_end(&jim);
63+
64+
jim_member_key(&jim, "empty_array"),
65+
jim_array_begin(&jim);
66+
jim_array_end(&jim);
67+
68+
jim_member_key(&jim, "empty_object"),
69+
jim_object_begin(&jim);
70+
jim_object_end(&jim);
5371
jim_object_end(&jim);
5472

5573
fwrite(jim.sink, jim.sink_count, 1, stdout);
@@ -62,31 +80,43 @@ int main()
6280

6381
```console
6482
$ cc -o example example.c
65-
$ ./example | jq .
83+
$ ./example
6684
{
67-
"null": null,
68-
"bool": [
69-
false,
70-
true
71-
],
72-
"integers": [
73-
-3,
74-
-2,
75-
-1,
76-
0,
77-
1,
78-
2,
79-
3
80-
],
81-
"floats": [
82-
3.1415,
83-
2.71828,
84-
1.618
85-
],
86-
"string": [
87-
"Hello\tWorld\n",
88-
"\u0000\u0000\u0000\u0000"
89-
]
85+
"null": null,
86+
"bool": [
87+
false,
88+
true
89+
],
90+
"integers": [
91+
-3,
92+
-2,
93+
-1,
94+
0,
95+
1,
96+
2,
97+
3
98+
],
99+
"floats": [
100+
0.0,
101+
0.0,
102+
3.1415,
103+
2.71828,
104+
1.6180,
105+
null,
106+
null,
107+
null
108+
],
109+
"string": [
110+
"Hello\tWorld\n",
111+
"\u0000\u0000\u0000\u0000"
112+
],
113+
"nested_object": {
114+
"foo": 69,
115+
"bar": 420,
116+
"baz": 1337
117+
},
118+
"empty_array": [],
119+
"empty_object": {}
90120
}
91121
```
92122

examples/01_from_readme.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
int main()
77
{
8-
Jim jim = {0};
8+
Jim jim = {.pp = 4};
99

1010
jim_object_begin(&jim);
1111
jim_member_key(&jim, "null");
@@ -41,6 +41,24 @@ int main()
4141
jim_string(&jim, "Hello\tWorld\n");
4242
jim_string_sized(&jim, "\0\0\0\0", 4);
4343
jim_array_end(&jim);
44+
45+
jim_member_key(&jim, "nested_object");
46+
jim_object_begin(&jim);
47+
jim_member_key(&jim, "foo");
48+
jim_integer(&jim, 69);
49+
jim_member_key(&jim, "bar");
50+
jim_integer(&jim, 420);
51+
jim_member_key(&jim, "baz");
52+
jim_integer(&jim, 1337);
53+
jim_object_end(&jim);
54+
55+
jim_member_key(&jim, "empty_array"),
56+
jim_array_begin(&jim);
57+
jim_array_end(&jim);
58+
59+
jim_member_key(&jim, "empty_object"),
60+
jim_object_begin(&jim);
61+
jim_object_end(&jim);
4462
jim_object_end(&jim);
4563

4664
fwrite(jim.sink, jim.sink_count, 1, stdout);

jim2.h

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <assert.h>
1616
#include <stdlib.h>
17+
#include <stdbool.h>
1718
#include <string.h>
1819

1920
typedef enum {
@@ -23,8 +24,8 @@ typedef enum {
2324

2425
typedef struct {
2526
Jim_Scope_Kind kind;
26-
int tail;
27-
int key;
27+
int tail; // Not the first element in an array or an object
28+
int key; // An object key was just placed
2829
} Jim_Scope;
2930

3031
typedef struct {
@@ -34,12 +35,9 @@ typedef struct {
3435
Jim_Scope *scopes;
3536
size_t scopes_count;
3637
size_t scopes_capacity;
38+
size_t pp;
3739
} Jim;
3840

39-
// TODO: implement pretty-printing based on how nested the scopes are.
40-
// Introduce a separate boolean flag in the Jim struct to enable/disable
41-
// the pretty-printing.
42-
4341
void jim_begin(Jim *jim);
4442
void jim_null(Jim *jim);
4543
void jim_bool(Jim *jim, int boolean);
@@ -131,8 +129,20 @@ void jim_begin(Jim *jim)
131129
void jim_element_begin(Jim *jim)
132130
{
133131
Jim_Scope *scope = jim_current_scope(jim);
134-
if (scope && scope->tail && !scope->key) {
135-
jim_write_cstr(jim, ",");
132+
if (scope) {
133+
if (scope->tail && !scope->key) {
134+
jim_write_cstr(jim, ",");
135+
}
136+
if (jim->pp) {
137+
if (scope->key) {
138+
jim_write_cstr(jim, " ");
139+
} else {
140+
jim_write_cstr(jim, "\n");
141+
for (size_t i = 0; i < jim->scopes_count*jim->pp; ++i) {
142+
jim_write_cstr(jim, " ");
143+
}
144+
}
145+
}
136146
}
137147
}
138148

@@ -282,6 +292,13 @@ void jim_array_begin(Jim *jim)
282292

283293
void jim_array_end(Jim *jim)
284294
{
295+
Jim_Scope *scope = jim_current_scope(jim);
296+
if (jim->pp && scope && scope->tail) {
297+
jim_write_cstr(jim, "\n");
298+
for (size_t i = 0; i < (jim->scopes_count - 1)*jim->pp; ++i) {
299+
jim_write_cstr(jim, " ");
300+
}
301+
}
285302
jim_write_cstr(jim, "]");
286303
jim_scope_pop(jim);
287304
jim_element_end(jim);
@@ -313,6 +330,13 @@ void jim_member_key_sized(Jim *jim, const char *str, size_t size)
313330

314331
void jim_object_end(Jim *jim)
315332
{
333+
Jim_Scope *scope = jim_current_scope(jim);
334+
if (jim->pp && scope && scope->tail) {
335+
jim_write_cstr(jim, "\n");
336+
for (size_t i = 0; i < (jim->scopes_count - 1)*jim->pp; ++i) {
337+
jim_write_cstr(jim, " ");
338+
}
339+
}
316340
jim_write_cstr(jim, "}");
317341
jim_scope_pop(jim);
318342
jim_element_end(jim);

0 commit comments

Comments
 (0)