Skip to content

Commit d627a0e

Browse files
authored
Merge pull request #57 from pietroppeter/add-skip-hook-skip-fields
fix #50: add skipHook to skip fields of object when serializing
2 parents ea811be + d20d1bb commit d627a0e

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,33 @@ Gives us:
250250
"10/13"
251251
```
252252

253+
### `proc skipHook*()` Can be used to skip fields when serializing an object
254+
255+
If you want to skip some fields when serializing an object you can declare a `skipHook*()`
256+
257+
```nim
258+
type
259+
Conn = object
260+
id: int
261+
Foo = object
262+
a: int
263+
password: string
264+
b: float
265+
conn: Conn
266+
267+
proc skipHook*(T: typedesc[Foo], key: static string): bool =
268+
key in ["password", "conn"]
269+
270+
var v = Foo(a:1, password: "12345", b:0.5, conn: Conn(id: 1))
271+
let s = v.toJson()
272+
```
273+
274+
Gives us:
275+
276+
```
277+
"{"a":1,"b":0.5}"
278+
```
279+
253280
## Static writing with `toStaticJson`.
254281

255282
Sometimes you have some json, and you want to write it in a static way. There is a special function for that:

src/jsony.nim

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,11 +796,21 @@ proc dumpHook*(s: var string, v: object) =
796796
else:
797797
# Normal objects.
798798
for k, e in v.fieldPairs:
799-
if i > 0:
800-
s.add ','
801-
s.dumpKey(k)
802-
s.dumpHook(e)
803-
inc i
799+
when compiles(skipHook(type(v), k)):
800+
when skipHook(type(v), k):
801+
discard
802+
else:
803+
if i > 0:
804+
s.add ','
805+
s.dumpKey(k)
806+
s.dumpHook(e)
807+
inc i
808+
else:
809+
if i > 0:
810+
s.add ','
811+
s.dumpKey(k)
812+
s.dumpHook(e)
813+
inc i
804814
s.add '}'
805815

806816
proc dumpHook*[N, T](s: var string, v: array[N, t[T]]) =

tests/all.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
import test_arrays, test_char, test_enums, test_errors, test_fast_numbers,
66
test_json_in_json, test_numbers, test_objects, test_options, test_parseHook,
7-
test_rawjson, test_refs, test_sets, test_strings, test_tables, test_tojson, test_tuples
8-
echo "all tests pass"
7+
test_rawjson, test_refs, test_sets, test_skipHook, test_strings, test_tables, test_tojson, test_tuples
8+
echo "all tests pass"

tests/test_skipHook.nim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import jsony
2+
3+
type
4+
Conn = object
5+
id: int
6+
Foo = object
7+
a: int
8+
password: string
9+
b: float
10+
conn: Conn
11+
12+
proc skipHook(T: typedesc[Foo], key: static string): bool =
13+
key in ["password", "conn"]
14+
15+
let v = Foo(a:1, password: "12345", b:0.6, conn: Conn(id: 1))
16+
doAssert v.toJson() ==
17+
"""{"a":1,"b":0.6}"""

0 commit comments

Comments
 (0)