Skip to content

Commit 43daa57

Browse files
committed
Add postHook.
1 parent df4d768 commit 43daa57

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ doAssert v.colorBlend == "red"
5151

5252
### `proc newHook()` Can be used to populate default values.
5353

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.
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 initialize the object's defaults before the main parsing happens.
5555

5656
```nim
5757
type
@@ -68,6 +68,23 @@ doAssert v.id == "123"
6868
doAssert v.visible == "yes"
6969
```
7070

71+
### `proc postHook()` Can be used to run code after the object is fully parsed.
72+
73+
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:
74+
75+
```nim
76+
type Sizer = object
77+
size: int
78+
originalSize: int
79+
80+
proc postHook(v: var Sizer) =
81+
v.originalSize = v.size
82+
83+
var sizer = fromJson[Sizer]("""{"size":10}""")
84+
doAssert sizer.size == 10
85+
doAssert sizer.originalSize == 10
86+
```
87+
7188
### `proc enumHook()` Can be used to parse enums.
7289

7390
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.

src/jsony.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ proc parseHook*[T: object|ref object](s: string, i: var int, v: var T) =
271271
inc i
272272
else:
273273
break
274+
when compiles(postHook(v)):
275+
postHook(v)
274276
eatChar(s, i, '}')
275277

276278
proc parseHook*[T](s: string, i: var int, v: var Table[string, T]) =

tests/test_objects.nim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,14 @@ proc renameHook(v: var Node, fieldName: var string) =
152152

153153
var node = fromJson[Node]("""{"type":"root"}""")
154154
doAssert node.kind == "root"
155+
156+
type Sizer = object
157+
size: int
158+
originalSize: int
159+
160+
proc postHook(v: var Sizer) =
161+
v.originalSize = v.size
162+
163+
var sizer = fromJson[Sizer]("""{"size":10}""")
164+
doAssert sizer.size == 10
165+
doAssert sizer.originalSize == 10

0 commit comments

Comments
 (0)