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
+53-5Lines changed: 53 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,42 @@
1
-
# JSONy - A loose, direct to object json parser with hooks.
1
+
# JSONy - A loose, direct to object json parser and serializer with hooks.
2
2
3
3
`nimble install jsony`
4
4
5
+
```nim
6
+
@[1, 2, 3].toJson() -> "[1,2,3]"
7
+
"[1, 2, 3]".fromJson(seq[int]) -> @[1, 2, 3]
8
+
```
9
+
5
10
Real world json is *never what you want*. It might have extra fields that you don't care about. It might have missing fields requiring default values. It might change or grow new fields at any moment. Json might use `camelCase` or `snake_case`. It might use inconsistent naming.
6
11
7
-
With this library you can parse json your way, from the mess you get to the objects you want.
12
+
With this library you can use json your way, from the mess you get to the objects you want.
13
+
14
+
## Fast.
15
+
16
+
Currently the Nim standard module first parses or serializes json into JsonNodes and then turns the JsonNodes into your objects with the `to()` macro. This is slower and creates unnecessary work for the garbage collector. This library skips the JsonNodes and creates the objects you want directly.
8
17
9
-
## Fast/No garbage.
18
+
Another speed up comes from not using `StringStream`. `Stream` has a function dispatch overhead because it has not know if you are using it as a `StringStream` or `FileStream`. Jsony skips the overhead and just directly writes to memory buffers.
19
+
20
+
### Parse speed.
21
+
```
22
+
name ............................... min time avg time std dv times
23
+
treeform/jsony .................... 10.121 ms 10.809 ms ±1.208 x100
24
+
planetis-m/eminim ................. 12.006 ms 12.574 ms ±1.156 x100
25
+
nim std/json ...................... 23.282 ms 34.679 ms ±7.512 x100
26
+
```
27
+
28
+
### Serialize speed.
29
+
```
30
+
name ............................... min time avg time std dv times
31
+
treeform/jsony ..................... 4.047 ms 4.172 ms ±0.225 x100
32
+
planetis-m/eminim .................. 7.173 ms 7.324 ms ±0.253 x100
33
+
disruptek/jason ................... 10.220 ms 11.155 ms ±0.689 x100
34
+
nim std/json ...................... 11.526 ms 15.181 ms ±0.857 x100
35
+
```
10
36
11
-
Currently the Nim standard module first parses json into JsonNodes and then turns the JsonNodes into your objects with the `to()` macro. This is slower and creates unnecessary work for the garbage collector. This library skips the JsonNodes and creates the objects you want directly.
37
+
Note: If you find a faster nim json parser or serializer let me know!
12
38
13
-
## Can parse most object types:
39
+
## Can parse or serializer most types:
14
40
15
41
* numbers and strings
16
42
* objects and ref objects
@@ -169,3 +195,25 @@ Gives us:
169
195
(id: "3", count: 99, filled: 99)
170
196
]"""
171
197
```
198
+
199
+
### `proc dumpHook()` Can be used to serializer into custom representation.
200
+
201
+
Just like reading custom data types you can also write faster data types with `dumpHook`. Using `Fraction` from the above example:
0 commit comments