Handle objects without fields gracefully
This commit is contained in:
parent
6d3fae7df2
commit
daa60be371
|
@ -141,21 +141,23 @@ proc readImpl(r: var JsonReader, value: var auto) =
|
|||
type T = value.type
|
||||
r.skipToken tkCurlyLe
|
||||
|
||||
let fields = T.fieldReadersTable(JsonReader)
|
||||
var expectedFieldPos = 0
|
||||
while r.lexer.tok == tkString:
|
||||
let reader = findFieldReader(fields[], r.lexer.strVal, expectedFieldPos)
|
||||
r.lexer.next()
|
||||
r.skipToken tkColon
|
||||
if reader != nil:
|
||||
reader(value, r)
|
||||
else:
|
||||
const typeName = typetraits.name(T)
|
||||
r.unexpectedField(r.lexer.strVal, typeName)
|
||||
if r.lexer.tok == tkComma:
|
||||
when T.totalSerializedFields > 0:
|
||||
let fields = T.fieldReadersTable(JsonReader)
|
||||
var expectedFieldPos = 0
|
||||
while r.lexer.tok == tkString:
|
||||
let reader = findFieldReader(fields[], r.lexer.strVal, expectedFieldPos)
|
||||
r.lexer.next()
|
||||
else:
|
||||
break
|
||||
r.skipToken tkColon
|
||||
if reader != nil:
|
||||
reader(value, r)
|
||||
else:
|
||||
const typeName = typetraits.name(T)
|
||||
r.unexpectedField(r.lexer.strVal, typeName)
|
||||
if r.lexer.tok == tkComma:
|
||||
r.lexer.next()
|
||||
else:
|
||||
break
|
||||
|
||||
r.skipToken tkCurlyRi
|
||||
|
||||
else:
|
||||
|
|
|
@ -158,6 +158,8 @@ proc writeImpl(w: var JsonWriter, value: auto) =
|
|||
{.error: "Failed to convert to JSON an unsupported type: " & typeName.}
|
||||
|
||||
proc toJson*(v: auto, pretty = false, typeAnnotations = false): string =
|
||||
mixin writeValue
|
||||
|
||||
var s = init StringOutputStream
|
||||
var w = JsonWriter.init(addr s, pretty, typeAnnotations)
|
||||
w.writeValue v
|
||||
|
|
Loading…
Reference in New Issue