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

How to omit items that have default values? #84

Open
iffy opened this issue Jan 2, 2024 · 2 comments
Open

How to omit items that have default values? #84

iffy opened this issue Jan 2, 2024 · 2 comments

Comments

@iffy
Copy link

iffy commented Jan 2, 2024

I have a situation where JSON strings omit default values. For instance in the code below, if interval is omitted or less than 0, it should be set to 100. What can I do with dumpHook/skipHook to omit default values when I serialize my objects?

import jsony, std/json, std/unittest

type
  Thing = object
    name*: string
    interval*: int

proc newHook*(x: var Thing) =
  if x.interval == 0:
    x.interval = 100

test "defaultvalue":
  let original = """{"name":"bob"}"""
  let thing = original.fromJson(Thing)
  check thing.interval == 100
  let serialized = thing.toJson()
  check parseJson(serialized) == parseJson(original)

test "specificvalue":
  let original = """{"name":"bob", "interval": 200}"""
  let thing = original.fromJson(Thing)
  check thing.interval == 200
  let serialized = thing.toJson()
  check parseJson(serialized) == parseJson(original)

The output of the above is:

parseJson(serialized) was {"name":"bob","interval":100}
parseJson(original) was {"name":"bob"}
[FAILED] defaultvalue
[OK] specificvalue
@pietroppeter
Copy link
Contributor

I think #85 if merged could provide a solution :)

@georgelemon
Copy link

georgelemon commented Apr 2, 2024

import std/json, pkg/jsony

type
  Thing = object
    name*: string
    interval*: int
  
  AnotherThing = object
    name*: string
    interval*: int

proc newHook(x: var Thing) =
  if x.interval == 0:
    x.interval = 100

proc skipHook(T: typedesc[Thing], v: int, key: string): bool =
  if key == "interval":
    result = v == 100

block:
  let
    original = """{"name":"bob"}"""
    thing = jsony.fromJson(original, Thing)
    serialized = jsony.toJson(thing)
  
  assert thing.interval == 100
  assert serialized == original
  assert jsony.fromJson(serialized) == jsony.fromJson(original)
  assert json.parseJson(serialized) == json.parseJson(original)

block:
  let
    original = """{"name":"bob", "interval": 200}"""
    thing = original.fromJson(Thing)
    serialized = jsony.toJson(thing)

  assert thing.interval == 200
  assert serialized != original
  assert jsony.fromJson(serialized) == jsony.fromJson(original)
  assert json.parseJson(serialized) == json.parseJson(original)

block:
  let
    original = """{"name": "bob"}"""
    athing = jsony.fromJson(original, AnotherThing)
    serialized = jsony.toJson(athing)
  assert serialized == """{"name":"bob","interval":0}""" # not covered by skipHook
  assert jsony.fromJson(serialized) != jsony.fromJson(original)
  assert json.parseJson(serialized) != json.parseJson(original)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants