Handle reserved keyword type as field name

This commit is contained in:
Mamy André-Ratsimbazafy 2019-05-28 17:42:18 +02:00
parent 4ffd0b3af3
commit f3d8f822bd
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
2 changed files with 10 additions and 2 deletions

View File

@ -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.}

View File

@ -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)