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
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,14 @@
1
1
# JSONy - A loose, direct to object json parser with hooks.
2
2
3
+
`nimble install jsony`
4
+
3
5
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.
4
6
5
7
With this library you can parse json your way, from the mess you get to the objects you want.
6
8
7
9
## Fast/No garbage.
8
10
9
-
Current 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.
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.
10
12
11
13
## Can parse most object types:
12
14
@@ -30,7 +32,7 @@ var v = fromJson[Entry1](s)
30
32
doAssert v.color == ""
31
33
```
32
34
33
-
## Snake_case or CamelCase
35
+
## Converts snake_case to camelCase.
34
36
35
37
Nim usually uses `camelCase` for its variables, while a bunch of json in the wild uses `snake_case`. This library will convert `snake_case` to `camelCase` for you when reading json.
36
38
@@ -49,7 +51,7 @@ doAssert v.colorBlend == "red"
49
51
50
52
### `proc newHook()` Can be used to populate default values.
51
53
52
-
Some times absence of a field means it should have a default value. Normally this 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.
54
+
Sometimes the absence of a field means it should have a default value. Normally this would just be Nim's default value for the variable type but that isn't always what you want. With the newHook() you can set initialize the object your defaults before the main parsing happens.
53
55
54
56
```nim
55
57
type
@@ -68,7 +70,7 @@ doAssert v.visible == "yes"
68
70
69
71
### `proc enumHook()` Can be used to parse enums.
70
72
71
-
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.
73
+
In the wild json enum names almost never match to Nim enum names which 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.
92
94
93
-
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.
95
+
In the 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.
94
96
95
97
```nim
96
98
type Node = ref object
@@ -118,7 +120,7 @@ proc parseHook(s: string, i: var int, v: var DateTime) =
118
120
var dt = fromJson[DateTime](""" "2020-01-01 00:00:00" """)
119
121
```
120
122
121
-
Some times json gives you an object of entries with their id as keys, but you might want it as a sequence with ids inside the objects, again you can do anything with `parseHook()`:
123
+
Sometimes json gives you an object of entries with their id as keys, but you might want it as a sequence with ids inside the objects. You can handle this and many other scenarios with `parseHook()`:
0 commit comments