mirror of
https://github.com/logos-storage/nim-serde.git
synced 2026-01-04 22:53:09 +00:00
Add prettified option for toJson
This commit is contained in:
parent
1a506be83a
commit
d877872127
@ -125,7 +125,11 @@ func `%`*[T: distinct](id: T): JsonNode =
|
||||
type baseType = T.distinctBase
|
||||
% baseType(id)
|
||||
|
||||
proc toJson*[T](item: T): string = $(%item)
|
||||
proc toJson*[T](item: T, pretty = false): string =
|
||||
if pretty:
|
||||
(%item).pretty
|
||||
else:
|
||||
$(%item)
|
||||
|
||||
proc toJsnImpl(x: NimNode): NimNode =
|
||||
case x.kind
|
||||
|
||||
@ -93,4 +93,27 @@ suite "json serialization - serialize":
|
||||
"myint": 1
|
||||
}""".flatten
|
||||
|
||||
check $obj == expected
|
||||
check $obj == expected
|
||||
|
||||
test "serializes to string with toJson":
|
||||
type MyObj = object
|
||||
mystring {.serialize.}: string
|
||||
myint {.serialize.}: int
|
||||
|
||||
let obj = MyObj(mystring: "abc", myint: 1)
|
||||
let expected = """{"mystring":"abc","myint":1}"""
|
||||
|
||||
check obj.toJson == expected
|
||||
|
||||
test "serializes prettied to string with toJson":
|
||||
type MyObj = object
|
||||
mystring {.serialize.}: string
|
||||
myint {.serialize.}: int
|
||||
|
||||
let obj = MyObj(mystring: "abc", myint: 1)
|
||||
let expected = """{
|
||||
"mystring": "abc",
|
||||
"myint": 1
|
||||
}"""
|
||||
|
||||
check obj.toJson(pretty=true) == expected
|
||||
Loading…
x
Reference in New Issue
Block a user