mirror of
https://github.com/status-im/nim-json-serialization.git
synced 2025-02-16 12:07:35 +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
|
||||
|
||||
elif value is (object or tuple):
|
||||
type T = value.type
|
||||
type T = type(value)
|
||||
r.skipToken tkCurlyLe
|
||||
|
||||
when T.totalSerializedFields > 0:
|
||||
@ -192,4 +192,3 @@ proc readValue*(r: var JsonReader, value: var auto) =
|
||||
else:
|
||||
const typeName = typetraits.name(value.type)
|
||||
{.error: "Failed to convert to JSON an unsupported type: " & typeName.}
|
||||
|
||||
|
@ -28,6 +28,10 @@ type
|
||||
Invalid = object
|
||||
distance: Mile
|
||||
|
||||
Reserved = object
|
||||
# Using Nim reserved keyword
|
||||
`type`: string
|
||||
|
||||
template reject(code) =
|
||||
static: doAssert(not compiles(code))
|
||||
|
||||
@ -102,3 +106,8 @@ suite "toJson tests":
|
||||
expect JsonReaderError:
|
||||
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