mirror of
https://github.com/logos-storage/nim-serde.git
synced 2026-01-07 16:13:06 +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
|
type baseType = T.distinctBase
|
||||||
% baseType(id)
|
% 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 =
|
proc toJsnImpl(x: NimNode): NimNode =
|
||||||
case x.kind
|
case x.kind
|
||||||
|
|||||||
@ -93,4 +93,27 @@ suite "json serialization - serialize":
|
|||||||
"myint": 1
|
"myint": 1
|
||||||
}""".flatten
|
}""".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