You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-8Lines changed: 28 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ Simple JSON parser written in C99
12
12
-[FAQs](#faqs)
13
13
14
14
15
-
## Features
15
+
## Features (Design Goals)
16
16
- Simple, small and easy to use, integration.
17
17
- Writing in C99: simple, small, portability.
18
18
- Robust error handling, no pointer-as-object.
@@ -25,12 +25,28 @@ Simple JSON parser written in C99
25
25
26
26
## Limits
27
27
- No scientific number
28
-
-Not use state machine for parsing
28
+
-Do not use state machine for parsing
29
29
- Not the best, the fastest json parser
30
30
- Parsing require a preallocated large buffer (about kilobytes, based on file size, cannot detect buffer size requirement), buffer not an allocator (I dont often store the json value as persitent, just temp for parsing levels, game data)
31
-
- Use longjmp to handling error, jump from error point to parse function call, which is have 2 disadvantages:
32
-
- longjmp may works different over platforms
33
-
- longjmp depends on stdlib, not work on freestanding platforms
31
+
- Use `longjmp` to handling error, jump from error point to parse function call, which is have disadvantages:
32
+
-`longjmp` may works different over platforms
33
+
-`longjmp` depends on stdlib, not work on freestanding platforms
34
+
-`longjmp` make call stack unpredictable, and must be unwind the call stack (which are overhead, and crash prone)
35
+
36
+
37
+
## API design flaws
38
+
After sometimes use this, I found that API have flaws of its own:
39
+
- Too verbose, not just for good reasons
40
+
- Too many params for function call, which can be better to combine as a struct
41
+
- Allocator buffer, but cannot custom allocations
42
+
- It's claimed cache-friendly, but the layout of data in memory after parsing usually un-ordered, un-lineared in hierarchy point-of-view (address of items of array come before the array itself)
43
+
```plain
44
+
[Items of a array] -> [Maybe items of other array] -> [Maybe other array JSON] -> [Array JSON] -> [... and some unpredictable value, and string/object come to the party]
45
+
```
46
+
- Continue to the above, reorder should help? Not exactly, how to reorder, how much reorder overhead, that too much works for simple parsing simple data format like JSON.
47
+
- No big projects usage.
48
+
- Parsing still too complex
49
+
-> Decisioning to exploring some best practices from other parser, [MetaDesk](https://github.com/ryanfleury/metadesk) is good example. Linked list in one single arena of memory is good enough.
34
50
35
51
36
52
## Examples
@@ -169,10 +185,14 @@ make lib
169
185
170
186
## FAQs
171
187
### Why another json parser?
172
-
When I first read an article about common mistake of c libraries are do much dynamic allocations and have no custom allocators. So I create this projects to learn to make good library in C.
188
+
When I first read an article about common mistake of c libraries are do much dynamic allocations and have no custom allocators. So I create this project to learn to make good library in C.
189
+
190
+
### Have no update after long time?
191
+
Like I said above, I started this project mainly for learning purpose, how to design a good API for C. Have no purpose for compete with battle-tested library. When I have any ideas for better API design, I comeback and do some hacks. If you have ideas, just make a issue in this repo.
173
192
174
193
### You said custom allocators, but I donot find one?
175
-
In the first version there is a custom allocator interface. But after the long run, I found the memory of Json was throw away at one, so dynamic allocators are expensive for that. Now we just given a temporary buffer to parser, and there is a linear allocator in internal. So no dynamic allocations.
194
+
In the first version there is a custom allocator interface. But after the long run, I found the memory of Json was throw away at one, so dynamic allocators are expensive for that. Now we just given a temporary buffer to parser, and there is a linear allocator in internal. So no dynamic allocations.
195
+
_Note_: Next version will combine both, support passing allocator around, but have API to create one-time allocator from buffer. (Better API)
176
196
177
197
### Where stringify/serialize functions?
178
198
It easy to write an JsonStringify version, but the real problem in C is not that simple. You need to create Json value, create Json may need memory, so we need to care about memory allocation. That headache! Fortunately, C is static type language, so we can easily convert out data structure/object to json easily base on its types. See example below:
0 commit comments