diff --git a/json_serialization/reader.nim b/json_serialization/reader.nim index c171195..cc4553c 100644 --- a/json_serialization/reader.nim +++ b/json_serialization/reader.nim @@ -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: diff --git a/json_serialization/writer.nim b/json_serialization/writer.nim index 3bae3e5..ad7e54d 100644 --- a/json_serialization/writer.nim +++ b/json_serialization/writer.nim @@ -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