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
+55-7Lines changed: 55 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
# JSONy - A loose, direct to object json parser with hooks.
2
2
3
-
Real world json is never what you want. It might have extra fields that you don't care about. It might have missing fields signifying default values. It might change or grow new fields at any moment. Json might use camelCase or snake_case. It might use inconsistent naming.
3
+
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
4
5
5
With this library you can parse json your way, from the mess you get to the objects you want.
6
6
7
-
## No garbage.
7
+
## Fast/No garbage.
8
8
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.
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.
10
10
11
11
## Can parse most object types:
12
12
@@ -16,10 +16,11 @@ Current standard module first parses json into JsonNodes and then turns the Json
16
16
* tuples
17
17
* seq and arrays
18
18
* tables
19
+
* and `parseHook()` enables you to parse any type!
19
20
20
21
## Not strict.
21
22
22
-
Extra json fields are ignored and missing json fields keep their default values. Json is never exactly what you want.
23
+
Extra json fields are ignored and missing json fields keep their default values.
23
24
24
25
```nim
25
26
type Entry1 = object
@@ -31,7 +32,7 @@ doAssert v.color == ""
31
32
32
33
## Snake_case or CamelCase
33
34
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
+
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.
### `proc newHook()` Can be used to populate default values.
50
51
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.
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.
52
53
53
54
```nim
54
55
type
55
56
Foo5 = object
56
57
visible: string
57
58
id: string
58
59
proc newHook(foo: var Foo5) =
59
-
# Populates the object before its deserialized.
60
+
# Populates the object before its fully deserialized.
60
61
foo.visible = "yes"
61
62
62
63
var s = """{"id":"123"}"""
@@ -102,3 +103,50 @@ proc renameHook(v: var Node, fieldName: var string) =
102
103
var node = fromJson[Node]("""{"type":"root"}""")
103
104
doAssert node.kind == "root"
104
105
```
106
+
107
+
### `proc parseHook()` Can be used to do anything.
108
+
109
+
Json can't store dates, so they are usually stored as strings. You can use
110
+
`parseHook()` to override default parsing and parse date times as a string:
111
+
112
+
```nim
113
+
proc parseHook(s:string, i:var int, v: var DateTime) =
114
+
var str: string
115
+
parseHook(s, i, str)
116
+
v = parse(str, "yyyy-MM-dd hh:mm:ss")
117
+
118
+
var dt = fromJson[DateTime](""" "2020-01-01 00:00:00" """)
119
+
```
120
+
121
+
Some times json gives you a 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()`:
122
+
123
+
```nim
124
+
type Entry = object
125
+
id: string
126
+
count: int
127
+
filled: int
128
+
129
+
let data = """{
130
+
"1": {"count":12, "filled": 11},
131
+
"2": {"count":66, "filled": 0},
132
+
"3": {"count":99, "filled": 99}
133
+
}"""
134
+
135
+
proc parseHook(s:string, i:var int, v: var seq[Entry]) =
0 commit comments