Skip to content

extend skipHook to skip object fields based on values #85

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,42 @@ Gives us:
"{"a":1,"b":0.5}"
```

Also, `skipHook` can be used at runtime to skip fields based on their current value. For example:
```nim
type
Boo = ref object
hoo: string
x: seq[string]
y: seq[int]
i: int
b: bool

Woo = object
boo: Boo

proc skipHook(v: Boo, key: string): bool =
## Skip `nil` fields
result = v == nil

proc skipHook(v: string, key: string): bool =
## skip all fields that contains empty strings
result = v.len == 0

proc skipHook[T](v: seq[T], key: string): bool =
# skip all fields that contains empty sequences
result = v.len == 0

let w = Woo(boo: Boo())
assert w.toJson() == """{"boo":{"i":0,"b":false}}"""

let w2 = Woo()
assert w2.toJson() == "{}"

let b = Boo()
assert b.toJson() ==
"""{"i":0,"b":false}"""
```

## 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
18 changes: 18 additions & 0 deletions src/jsony.nim
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,24 @@ proc dumpHook*(s: var string, v: object) =
s.dumpKey(k)
s.dumpHook(e)
inc i
elif compiles(skipHook(type(v), e, k)):
if skipHook(type(v), e, k):
discard
else:
if i > 0:
s.add ','
s.dumpKey(k)
s.dumpHook(e)
inc i
elif compiles(skipHook(e, k)):
if skipHook(e, k):
discard
else:
if i > 0:
s.add ','
s.dumpKey(k)
s.dumpHook(e)
inc i
else:
if i > 0:
s.add ','
Expand Down
43 changes: 42 additions & 1 deletion tests/test_skipHook.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import jsony

type
Conn = object
id: int
Expand All @@ -15,3 +14,45 @@ proc skipHook(T: typedesc[Foo], key: static string): bool =
let v = Foo(a:1, password: "12345", b:0.6, conn: Conn(id: 1))
doAssert v.toJson() ==
"""{"a":1,"b":0.6}"""

type
Boo = ref object
hoo: string
x: seq[string]
y: seq[int]
i: int
b: bool

Woo = object
boo: Boo

Moo = object
boo: Boo

proc skipHook(T: typedesc[Woo], v: Boo, key: string): bool =
result = v == nil

proc skipHook(v: string, key: string): bool =
result = v.len == 0

proc skipHook[T](v: seq[T], key: string): bool =
result = v.len == 0

let w = Woo(boo: Boo())
doAssert w.toJson() ==
"""{"boo":{"i":0,"b":false}}"""
let w2 = Woo()
doAssert w2.toJson() ==
"""{}"""
let b = Boo()
doAssert b.toJson() ==
"""{"i":0,"b":false}"""

block:
let m = Moo() # not covered by `skipHook`
doAssert m.toJson() ==
"""{"boo":null}"""
block:
let m = Moo(boo: Boo(hoo: "just moo"))
doAssert m.toJson() ==
"""{"boo":{"hoo":"just moo","i":0,"b":false}}"""