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
+48-4Lines changed: 48 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,15 @@ With this library you can parse json your way, from the mess you get to the obje
8
8
9
9
Current standard module first parses json into JsonNodes and then turns the JsonNodes into objects you want. This is slower and creates unnecessary work for the garbage collector. This library skips the JsonNodes and creates the objects you want directly.
10
10
11
+
## Can parse most object types:
12
+
13
+
* numbers and strings
14
+
* objects and ref objects
15
+
* enums
16
+
* tuples
17
+
* seq and arrays
18
+
* tables
19
+
11
20
## Not strict.
12
21
13
22
Extra json fields are ignored and missing json fields keep their default values. Json is never exactly what you want.
@@ -20,9 +29,26 @@ var v = fromJson[Entry1](s)
20
29
doAssert v.color == ""
21
30
```
22
31
32
+
## Snake_case or CamelCase
33
+
34
+
Nim usually uses camalCase for its variables, while a bunch of json in the wild uses snake_case. This library will convert snake_case to camalCase for you when reading json.
35
+
36
+
```nim
37
+
type Entry4 = object
38
+
colorBlend: string
39
+
40
+
var v = fromJson[Entry4]("""{"colorBlend":"red"}""")
41
+
doAssert v.colorBlend == "red"
42
+
43
+
v = fromJson[Entry4]("""{"color_blend":"red"}""")
44
+
doAssert v.colorBlend == "red"
45
+
```
46
+
23
47
## Has hooks.
24
48
25
-
### `proc newHook(foo: var ...)` Can be used to populate default values.
49
+
### `proc newHook()` Can be used to populate default values.
50
+
51
+
Some times absence of a field means it should have a default value. Normally hits would just be Nim's default value for the variable type. But with the newHook() you can setup the object with defaults before the main parsing happens.
26
52
27
53
```nim
28
54
type
@@ -39,15 +65,17 @@ doAssert v.id == "123"
39
65
doAssert v.visible == "yes"
40
66
```
41
67
42
-
### `proc enumHook[...](v: string): ...` Can be used to parse enums.
68
+
### `proc enumHook()` Can be used to parse enums.
69
+
70
+
In wild json enums name almost never match to nim enum names that usually have a prefix. The enumHook() allows you to rename the enums to your internal names.
### `proc renameHook()` Can be used to rename fields at run time.
91
+
92
+
In wild json field names can be reserved words such as type, class, or array. With the renameHook you can rename fields to what you want on the type you need.
93
+
94
+
```nim
95
+
type Node = ref object
96
+
kind: string
97
+
98
+
proc renameHook(v: var Node, fieldName: var string) =
0 commit comments