diff --git a/serde/json/deserializer.nim b/serde/json/deserializer.nim index 9587de7..4d75902 100644 --- a/serde/json/deserializer.nim +++ b/serde/json/deserializer.nim @@ -229,13 +229,15 @@ proc fromJson*[T: ref object or object](_: type T, json: JsonNode): ?!T = 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 = 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 = let jsn = ?parser.parseJson(jsn) # full qualification required in-module only T.fromJson(jsn) diff --git a/tests/json/testDeserialize.nim b/tests/json/testDeserialize.nim index 3a01ef9..c0e7ef3 100644 --- a/tests/json/testDeserialize.nim +++ b/tests/json/testDeserialize.nim @@ -3,6 +3,7 @@ import std/options import std/unittest import pkg/stint import pkg/serde +import pkg/stew/byteutils import pkg/questionable import pkg/questionable/results @@ -137,3 +138,18 @@ suite "json serialization - deserialize": let deserialized = !MyRef.fromJson(json) check deserialized.mystring == expected.mystring 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