Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #50: add skipHook to skip fields of object when serializing #57

Merged
merged 4 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,33 @@ Gives us:
"10/13"
```

### `proc skipHook*()` Can be used to skip fields when serializing an object

If you want to skip some fields when serializing an object you can declare a `skipHook*()`

```nim
type
Conn = object
id: int
Foo = object
a: int
password: string
b: float
conn: Conn

proc skipHook*(T: typedesc[Foo], key: static string): bool =
key in ["password", "conn"]

var v = Foo(a:1, password: "12345", b:0.5, conn: Conn(id: 1))
let s = v.toJson()
```

Gives us:

```
"{"a":1,"b":0.5}"
```

## Static writing with `toStaticJson`.

Sometimes you have some json, and you want to write it in a static way. There is a special function for that:
Expand Down
20 changes: 15 additions & 5 deletions src/jsony.nim
Original file line number Diff line number Diff line change
Expand Up @@ -796,11 +796,21 @@ proc dumpHook*(s: var string, v: object) =
else:
# Normal objects.
for k, e in v.fieldPairs:
if i > 0:
s.add ','
s.dumpKey(k)
s.dumpHook(e)
inc i
when compiles(skipHook(type(v), k)):
when skipHook(type(v), k):
discard
else:
if i > 0:
s.add ','
s.dumpKey(k)
s.dumpHook(e)
inc i
else:
if i > 0:
s.add ','
s.dumpKey(k)
s.dumpHook(e)
inc i
s.add '}'

proc dumpHook*[N, T](s: var string, v: array[N, t[T]]) =
Expand Down
4 changes: 2 additions & 2 deletions tests/all.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

import test_arrays, test_char, test_enums, test_errors, test_fast_numbers,
test_json_in_json, test_numbers, test_objects, test_options, test_parseHook,
test_rawjson, test_refs, test_sets, test_strings, test_tables, test_tojson, test_tuples
echo "all tests pass"
test_rawjson, test_refs, test_sets, test_skipHook, test_strings, test_tables, test_tojson, test_tuples
echo "all tests pass"
17 changes: 17 additions & 0 deletions tests/test_skipHook.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import jsony

type
Conn = object
id: int
Foo = object
a: int
password: string
b: float
conn: Conn

proc skipHook(T: typedesc[Foo], key: static string): bool =
key in ["password", "conn"]

let v = Foo(a:1, password: "12345", b:0.6, conn: Conn(id: 1))
doAssert v.toJson() ==
"""{"a":1,"b":0.6}"""