nim-json-serialization/tests/test_json_flavor.nim

42 lines
1.0 KiB
Nim
Raw Permalink Normal View History

2021-03-18 11:01:06 +00:00
import
strutils,
serialization,
../json_serialization
Json.createFlavor StringyJson
proc writeValue*(w: var JsonWriter[StringyJson], val: SomeInteger) =
writeValue(w, $val)
proc readValue*(r: var JsonReader[StringyJson], v: var SomeSignedInt) =
try:
v = type(v) parseBiggestInt readValue(r, string)
except ValueError as err:
r.raiseUnexpectedValue("A signed integer encoded as string")
proc readValue*(r: var JsonReader[StringyJson], v: var SomeUnsignedInt) =
try:
v = type(v) parseBiggestUInt readValue(r, string)
except ValueError as err:
r.raiseUnexpectedValue("An unsigned integer encoded as string")
type
Container = object
name: string
x: int
y: uint64
list: seq[int64]
let c = Container(name: "c", x: -10, y: 20, list: @[1'i64, 2, 25])
let encoded = StringyJson.encode(c)
echo "Encoded: ", encoded
let decoded = try:
StringyJson.decode(encoded, Container)
except SerializationError as err:
echo err.formatMsg("<encoded>")
quit 1
echo "Decoded: ", decoded