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
+22-18Lines changed: 22 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,7 +86,11 @@ doAssert v.colorBlend == "red"
86
86
87
87
## Has hooks.
88
88
89
-
### `proc newHook()` Can be used to populate default values.
89
+
Hooks are a powerful concept that allows you to parse json "your way" and is the main idea behind `jsony`!
90
+
91
+
* Note: that hooks need to be exported to where you are parsing the json so that the parsing system can pick them up.
92
+
93
+
### `proc newHook*()` Can be used to populate default values.
90
94
91
95
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 initialize the object's defaults before the main parsing happens.
92
96
@@ -95,7 +99,7 @@ type
95
99
Foo5 = object
96
100
visible: string
97
101
id: string
98
-
proc newHook(foo: var Foo5) =
102
+
proc newHook*(foo: var Foo5) =
99
103
# Populates the object before its fully deserialized.
100
104
foo.visible = "yes"
101
105
@@ -105,7 +109,7 @@ doAssert v.id == "123"
105
109
doAssert v.visible == "yes"
106
110
```
107
111
108
-
### `proc postHook()` Can be used to run code after the object is fully parsed.
112
+
### `proc postHook*()` Can be used to run code after the object is fully parsed.
109
113
110
114
Some times we need run some code after the object is created. For example to set other values based on values that where set but are not part of the json data. Maybe to sanitize the object or convert older versions to new versions. Here I need to retain the original size as I will be messing with the object's regular size:
111
115
@@ -114,25 +118,25 @@ type Sizer = object
114
118
size: int
115
119
originalSize: int
116
120
117
-
proc postHook(v: var Sizer) =
121
+
proc postHook*(v: var Sizer) =
118
122
v.originalSize = v.size
119
123
120
124
var sizer = """{"size":10}""".fromJson(Sizer)
121
125
doAssert sizer.size == 10
122
126
doAssert sizer.originalSize == 10
123
127
```
124
128
125
-
### `proc enumHook()` Can be used to parse enums.
129
+
### `proc enumHook*()` Can be used to parse enums.
126
130
127
-
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.
131
+
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.
151
+
### `proc renameHook*()` Can be used to rename fields at run time.
148
152
149
-
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.
153
+
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.
150
154
151
155
```nim
152
156
type Node = ref object
153
157
kind: string
154
158
155
-
proc renameHook(v: var Node, fieldName: var string) =
159
+
proc renameHook*(v: var Node, fieldName: var string) =
156
160
if fieldName == "type":
157
161
fieldName = "kind"
158
162
159
163
var node = """{"type":"root"}""".fromJson(Node)
160
164
doAssert node.kind == "root"
161
165
```
162
166
163
-
### `proc parseHook()` Can be used to do anything.
167
+
### `proc parseHook*()` Can be used to do anything.
164
168
165
169
Json can't store dates, so they are usually stored as strings. You can use
166
-
`parseHook()` to override default parsing and parse `DateTime` as a `string`:
170
+
`parseHook*()` to override default parsing and parse `DateTime` as a `string`:
167
171
168
172
```nim
169
-
proc parseHook(s: string, i: var int, v: var DateTime) =
173
+
proc parseHook*(s: string, i: var int, v: var DateTime) =
170
174
var str: string
171
175
parseHook(s, i, str)
172
176
v = parse(str, "yyyy-MM-dd hh:mm:ss")
173
177
174
178
var dt = """ "2020-01-01 00:00:00" """.fromJson(DateTime)
175
179
```
176
180
177
-
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()`:
181
+
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*()`:
178
182
179
183
```nim
180
184
type Entry = object
@@ -188,7 +192,7 @@ let data = """{
188
192
"3": {"count":99, "filled": 99}
189
193
}"""
190
194
191
-
proc parseHook(s: string, i: var int, v: var seq[Entry]) =
195
+
proc parseHook*(s: string, i: var int, v: var seq[Entry]) =
192
196
var table: Table[string, Entry]
193
197
parseHook(s, i, table)
194
198
for k, entry in table.mpairs:
@@ -207,16 +211,16 @@ Gives us:
207
211
]"""
208
212
```
209
213
210
-
### `proc dumpHook()` Can be used to serializer into custom representation.
214
+
### `proc dumpHook*()` Can be used to serializer into custom representation.
211
215
212
-
Just like reading custom data types you can also write data types with `dumpHook`.
216
+
Just like reading custom data types you can also write data types with `dumpHook*()`.
0 commit comments