mirror of
https://github.com/status-im/nim-json-serialization.git
synced 2025-02-19 13:34:19 +00:00
Handle reserved keyword type
as field name
This commit is contained in:
parent
4ffd0b3af3
commit
f3d8f822bd
@ -167,7 +167,7 @@ proc readValue*(r: var JsonReader, value: var auto) =
|
|||||||
r.skipToken tkBracketRi
|
r.skipToken tkBracketRi
|
||||||
|
|
||||||
elif value is (object or tuple):
|
elif value is (object or tuple):
|
||||||
type T = value.type
|
type T = type(value)
|
||||||
r.skipToken tkCurlyLe
|
r.skipToken tkCurlyLe
|
||||||
|
|
||||||
when T.totalSerializedFields > 0:
|
when T.totalSerializedFields > 0:
|
||||||
@ -192,4 +192,3 @@ proc readValue*(r: var JsonReader, value: var auto) =
|
|||||||
else:
|
else:
|
||||||
const typeName = typetraits.name(value.type)
|
const typeName = typetraits.name(value.type)
|
||||||
{.error: "Failed to convert to JSON an unsupported type: " & typeName.}
|
{.error: "Failed to convert to JSON an unsupported type: " & typeName.}
|
||||||
|
|
||||||
|
@ -28,6 +28,10 @@ type
|
|||||||
Invalid = object
|
Invalid = object
|
||||||
distance: Mile
|
distance: Mile
|
||||||
|
|
||||||
|
Reserved = object
|
||||||
|
# Using Nim reserved keyword
|
||||||
|
`type`: string
|
||||||
|
|
||||||
template reject(code) =
|
template reject(code) =
|
||||||
static: doAssert(not compiles(code))
|
static: doAssert(not compiles(code))
|
||||||
|
|
||||||
@ -102,3 +106,8 @@ suite "toJson tests":
|
|||||||
expect JsonReaderError:
|
expect JsonReaderError:
|
||||||
discard Json.decode(jsonValue, uint64, mode = Portable)
|
discard Json.decode(jsonValue, uint64, mode = Portable)
|
||||||
|
|
||||||
|
test "Using Nim reserved keyword `type`":
|
||||||
|
let r = Reserved(`type`: "uint8")
|
||||||
|
check:
|
||||||
|
r.toJSON == """{"type":"uint8"}"""
|
||||||
|
r == Json.decode("""{"type":"uint8"}""", Reserved)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user