Skip to content

Commit bc1a983

Browse files
committed
added example that uses both jim and jimp
1 parent a790483 commit bc1a983

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

examples/04_from_readme_round.c

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include <stdio.h>
2+
3+
#define NOB_IMPLEMENTATION
4+
#define NOB_STRIP_PREFIX
5+
#include "../thirdparty/nob.h"
6+
#define JIM_IMPLEMENTATION
7+
#include "../jim.h"
8+
#define JIMP_IMPLEMENTATION
9+
#include "../jimp.h"
10+
11+
const char* JSON_STR =
12+
"{"
13+
// TODO: Uncomment when null is implemented
14+
//" \"null\": null,"
15+
"\"bool\":["
16+
"false,"
17+
"true"
18+
"],"
19+
"\"integers\":["
20+
"-3,"
21+
"-2,"
22+
"-1,"
23+
"0,"
24+
"1,"
25+
"2,"
26+
"3"
27+
"],"
28+
"\"floats\":["
29+
"3.1415,"
30+
"2.71828,"
31+
"1.618"
32+
"],"
33+
"\"string\":["
34+
"\"Hello\\tWorld\\n\","
35+
"\"\\u0000\\u0000\\u0000\\u0000\""
36+
"]"
37+
"}";
38+
39+
typedef struct {
40+
bool *items;
41+
size_t count;
42+
size_t capacity;
43+
} Bools;
44+
45+
typedef struct {
46+
long long *items;
47+
size_t count;
48+
size_t capacity;
49+
} Integers;
50+
51+
typedef struct {
52+
double *items;
53+
size_t count;
54+
size_t capacity;
55+
} Floats;
56+
57+
typedef struct {
58+
const char* *items;
59+
size_t count;
60+
size_t capacity;
61+
} Strings;
62+
63+
typedef struct {
64+
// TODO: Re-add null when null is implemented
65+
Bools bools;
66+
Integers integers;
67+
Floats floats;
68+
Strings strings;
69+
} JSON;
70+
71+
size_t sbwrite(const char *ptr, size_t size, size_t nmemb, String_Builder *sb) {
72+
assert(size == 1);
73+
74+
sb_append_buf(sb, ptr, nmemb);
75+
return nmemb;
76+
}
77+
78+
int main()
79+
{
80+
JSON json = {0};
81+
82+
Jimp jimp = {0};
83+
jimp_begin(&jimp, "JSON_STR", sb.items, sb.count);
84+
85+
if (!jimp_object_begin(&jimp)) return 1;
86+
while (jimp_object_member(&jimp)) {
87+
if (strcmp(jimp.member, "null") == 0) {
88+
return 1; // TODO: Handle when null is implemented
89+
} else if (strcmp(jimp.member, "bool") == 0) {
90+
if (!jimp_array_begin(&jimp)) return 1;
91+
while (jimp_array_item(&jimp)) {
92+
bool x = false;
93+
if (!jimp_bool(&jimp, &x)) return 1;
94+
da_append(&json.bools, x);
95+
}
96+
if (!jimp_array_end(&jimp)) return 1;
97+
} else if (strcmp(jimp.member, "integers") == 0) {
98+
if (!jimp_array_begin(&jimp)) return 1;
99+
while (jimp_array_item(&jimp)) {
100+
long long x = 0;
101+
if (!jimp_int(&jimp, &x)) return 1;
102+
da_append(&json.integers, x);
103+
}
104+
if (!jimp_array_end(&jimp)) return 1;
105+
} else if (strcmp(jimp.member, "floats") == 0) {
106+
if (!jimp_array_begin(&jimp)) return 1;
107+
while (jimp_array_item(&jimp)) {
108+
double x = 0;
109+
if (!jimp_float(&jimp, &x)) return 1;
110+
da_append(&json.floats, x);
111+
}
112+
if (!jimp_array_end(&jimp)) return 1;
113+
} else if (strcmp(jimp.member, "string") == 0) {
114+
if (!jimp_array_begin(&jimp)) return 1;
115+
while (jimp_array_item(&jimp)) {
116+
const char* x = NULL;
117+
if (!jimp_string(&jimp, &x)) return 1;
118+
da_append(&json.strings, x);
119+
}
120+
if (!jimp_array_end(&jimp)) return 1;
121+
} else {
122+
jimp_unknown_member(&jimp);
123+
return 1;
124+
}
125+
}
126+
if (!jimp_object_end(&jimp)) return 1;
127+
128+
129+
Jim jim = {0};
130+
jim_begin(&jim);
131+
132+
jim_object_begin(&jim);
133+
// TODO: Redo when null is implemented
134+
//jim_member_key(&jim, "null");
135+
//jim_null(&jim);
136+
137+
jim_member_key(&jim, "bool");
138+
jim_array_begin(&jim);
139+
da_foreach(bool, b, &json.bools) {
140+
jim_bool(&jim, *b);
141+
}
142+
jim_array_end(&jim);
143+
144+
jim_member_key(&jim, "integers");
145+
jim_array_begin(&jim);
146+
da_foreach(long long, i, &json.integers) {
147+
jim_integer(&jim, *i);
148+
}
149+
jim_array_end(&jim);
150+
151+
jim_member_key(&jim, "floats");
152+
jim_array_begin(&jim);
153+
da_foreach(double, f, &json.floats) {
154+
jim_float(&jim, *f, 5);
155+
}
156+
jim_array_end(&jim);
157+
158+
jim_member_key(&jim, "string");
159+
jim_array_begin(&jim);
160+
da_foreach(const char*, s, &json.strings) {
161+
jim_string(&jim, *s);
162+
}
163+
jim_array_end(&jim);
164+
jim_object_end(&jim);
165+
166+
if (jim.error != JIM_OK) {
167+
fprintf(stderr, "ERROR: could not serialize json properly: %s\n",
168+
jim_error_string(jim.error));
169+
return -1;
170+
}
171+
172+
printf("Initial: %s\n", JSON_STR);
173+
printf("Round Trip: %.*s\n", (int)jim.sink_count, jim.sink);
174+
175+
// TODO: Compare the two and print a roundtrip error if they differ
176+
177+
return 0;
178+
}

examples/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ all: 01_from_readme 02_binary_tree 03_parsing_database
1111

1212
03_parsing_database: 03_parsing_database.c ../jimp.h ../thirdparty/nob.h
1313
$(CC) $(CFLAGS) -o 03_parsing_database 03_parsing_database.c
14+
15+
04_from_readme_round: 04_from_readme_round.c ../jimp.h ../thirdparty/nob.h
16+
$(CC) $(CFLAGS) -o 04_from_readme_round 04_from_readme_round.c

0 commit comments

Comments
 (0)