mirror of
https://github.com/logos-storage/nim-serde.git
synced 2026-01-04 06:33:12 +00:00
Fix deserialization of openArray[byte] (#2)
This commit is contained in:
parent
d113c1e158
commit
1b77afcbf1
@ -229,13 +229,15 @@ proc fromJson*[T: ref object or object](_: type T, json: JsonNode): ?!T =
|
|||||||
|
|
||||||
success(res)
|
success(res)
|
||||||
|
|
||||||
proc fromJson*[T: ref object or object](_: type T, bytes: seq[byte]): ?!T =
|
|
||||||
let json = ?parse(string.fromBytes(bytes))
|
|
||||||
T.fromJson(json)
|
|
||||||
|
|
||||||
proc fromJson*(_: type JsonNode, jsn: string): ?!JsonNode =
|
proc fromJson*(_: type JsonNode, jsn: string): ?!JsonNode =
|
||||||
return parser.parseJson(jsn)
|
return parser.parseJson(jsn)
|
||||||
|
|
||||||
|
proc fromJson*[T: ref object or object](_: type T, bytes: openArray[byte]): ?!T =
|
||||||
|
let json = string.fromBytes(bytes)
|
||||||
|
static:
|
||||||
|
echo "typeof json after parse: ", typeof json
|
||||||
|
T.fromJson(json)
|
||||||
|
|
||||||
proc fromJson*[T: ref object or object](_: type T, jsn: string): ?!T =
|
proc fromJson*[T: ref object or object](_: type T, jsn: string): ?!T =
|
||||||
let jsn = ?parser.parseJson(jsn) # full qualification required in-module only
|
let jsn = ?parser.parseJson(jsn) # full qualification required in-module only
|
||||||
T.fromJson(jsn)
|
T.fromJson(jsn)
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import std/options
|
|||||||
import std/unittest
|
import std/unittest
|
||||||
import pkg/stint
|
import pkg/stint
|
||||||
import pkg/serde
|
import pkg/serde
|
||||||
|
import pkg/stew/byteutils
|
||||||
import pkg/questionable
|
import pkg/questionable
|
||||||
import pkg/questionable/results
|
import pkg/questionable/results
|
||||||
|
|
||||||
@ -137,3 +138,18 @@ suite "json serialization - deserialize":
|
|||||||
let deserialized = !MyRef.fromJson(json)
|
let deserialized = !MyRef.fromJson(json)
|
||||||
check deserialized.mystring == expected.mystring
|
check deserialized.mystring == expected.mystring
|
||||||
check deserialized.myint == expected.myint
|
check deserialized.myint == expected.myint
|
||||||
|
|
||||||
|
test "deserializes openArray[byte]":
|
||||||
|
type MyRef = ref object
|
||||||
|
mystring: string
|
||||||
|
myint: int
|
||||||
|
|
||||||
|
let expected = MyRef(mystring: "abc", myint: 1)
|
||||||
|
let byteArray = """{
|
||||||
|
"mystring": "abc",
|
||||||
|
"myint": 1
|
||||||
|
}""".toBytes
|
||||||
|
|
||||||
|
let deserialized = !MyRef.fromJson(byteArray)
|
||||||
|
check deserialized.mystring == expected.mystring
|
||||||
|
check deserialized.myint == expected.myint
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user