From cb695d175fd809f6262f1ebc329e7c0b1d4505f5 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Wed, 29 Apr 2020 10:21:20 +0200 Subject: [PATCH] reader support for stringlikes --- json_serialization/lexer.nim | 2 +- json_serialization/reader.nim | 21 ++++++++++++++++++++- tests/test_serialization.nim | 10 +++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/json_serialization/lexer.nim b/json_serialization/lexer.nim index 71e14e9..f47de06 100644 --- a/json_serialization/lexer.nim +++ b/json_serialization/lexer.nim @@ -1,6 +1,6 @@ import unicode, - faststreams/input_stream, stew/objects, + faststreams/input_stream, types export diff --git a/json_serialization/reader.nim b/json_serialization/reader.nim index a32a07d..e98d20a 100644 --- a/json_serialization/reader.nim +++ b/json_serialization/reader.nim @@ -169,6 +169,12 @@ func maxAbsValue(T: type[SomeInteger]): uint64 {.compileTime.} = proc isNotNilCheck[T](x: ref T not nil) {.compileTime.} = discard proc isNotNilCheck[T](x: ptr T not nil) {.compileTime.} = discard +# this construct catches `array[N, char]` which otherwise won't decompose into +# openArray[char] - we treat any array-like thing-of-characters as a string in +# the output +template isCharArray[N](v: array[N, char]): bool = true +template isCharArray(v: auto): bool = false + proc readValue*(r: var JsonReader, value: var auto) = mixin readValue @@ -178,7 +184,20 @@ proc readValue*(r: var JsonReader, value: var auto) = r.requireToken tkString value = r.lexer.strVal r.lexer.next() - + elif value is seq[char]: + r.requireToken tkString + value.setLen(r.lexer.strVal.len) + for i in 0..