diff --git a/json_serialization/lexer.nim b/json_serialization/lexer.nim index ea63a2f..48ab5b7 100644 --- a/json_serialization/lexer.nim +++ b/json_serialization/lexer.nim @@ -1,5 +1,5 @@ import - strutils, unicode, + unicode, faststreams/input_stream, stew/objects, types @@ -55,8 +55,6 @@ type strVal*: string const - pageSize = 4096 - powersOfTen = [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22] # TODO: this table should be much larger @@ -276,7 +274,7 @@ proc scanNumber(lexer: var JsonLexer) = lexer.tok = tkInt let scannedValue = lexer.scanInt() checkForNonPortableInt scannedValue - lexer.intVal = int64(scannedValue) + lexer.intVal = int64(scannedValue) * sign if lexer.stream[].eof: return c = lexer.stream[].peek() if c == '.': diff --git a/json_serialization/reader.nim b/json_serialization/reader.nim index f6529f0..09e97b0 100644 --- a/json_serialization/reader.nim +++ b/json_serialization/reader.nim @@ -144,7 +144,7 @@ iterator readObject*(r: var JsonReader, KeyType: typedesc, ValueType: typedesc): proc readValue*(r: var JsonReader, value: var auto) = mixin readValue - let tok = r.lexer.tok + let tok {.used.} = r.lexer.tok when value is string: r.requireToken tkString diff --git a/json_serialization/writer.nim b/json_serialization/writer.nim index f853805..ca0cb77 100644 --- a/json_serialization/writer.nim +++ b/json_serialization/writer.nim @@ -127,9 +127,6 @@ proc writeArray[T](w: var JsonWriter, elements: openarray[T]) = proc writeValue*(w: var JsonWriter, value: auto) = mixin enumInstanceSerializedFields, writeValue, writeFieldIMPL - template addChar(c) = - append c - when value is JsonNode: append if w.hasPrettyOutput: value.pretty else: $value @@ -141,11 +138,11 @@ proc writeValue*(w: var JsonWriter, value: auto) = else: writeValue(w, value[]) elif value is string|cstring: - addChar '"' + append '"' template addPrefixSlash(c) = - addChar '\\' - addChar c + append '\\' + append c for c in value: case c @@ -165,9 +162,9 @@ proc writeValue*(w: var JsonWriter, value: auto) = # This is potentially a bug in Nim's json module. append $ord(c) of '\\': addPrefixSlash '\\' - else: addChar c + else: append c - addChar '"' + append '"' elif value is bool: append if value: "true" else: "false" elif value is enum: diff --git a/tests/test_serialization.nim b/tests/test_serialization.nim index c8cb08f..917554d 100644 --- a/tests/test_serialization.nim +++ b/tests/test_serialization.nim @@ -26,7 +26,7 @@ type # properly when it's placed in another module: Meter.borrowSerialization int -template reject(code) = +template reject(code) {.used.} = static: doAssert(not compiles(code)) proc `==`(lhs, rhs: Meter): bool = @@ -45,11 +45,11 @@ proc newSimple(x: int, y: string, d: Meter): ref Simple = result.y = y result.distance = d -when false: - # The compiler cannot handle this check at the moment - # {.fatal.} seems fatal even in `compiles` context - var invalid = Invalid(distance: Mile(100)) - reject invalid.toJson +var invalid = Invalid(distance: Mile(100)) +# The compiler cannot handle this check at the moment +# {.fatal.} seems fatal even in `compiles` context +when false: reject invalid.toJson +else: discard invalid suite "toJson tests": test "encode primitives":