mirror of
https://github.com/status-im/nim-json-serialization.git
synced 2025-02-19 13:34:19 +00:00
treat array-of-char as string
This commit is contained in:
parent
bed0e403a4
commit
e485b74a10
@ -124,9 +124,16 @@ proc writeIterable*(w: var JsonWriter, collection: auto) =
|
|||||||
|
|
||||||
append ']'
|
append ']'
|
||||||
|
|
||||||
proc writeArray[T](w: var JsonWriter, elements: openarray[T]) =
|
proc writeArray*[T](w: var JsonWriter, elements: openarray[T]) =
|
||||||
writeIterable(w, elements)
|
writeIterable(w, elements)
|
||||||
|
|
||||||
|
# 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 isStringLike(v: string|cstring|openArray[char]|seq[char]): bool = true
|
||||||
|
template isStringLike[N](v: array[N, char]): bool = true
|
||||||
|
template isStringLike(v: auto): bool = false
|
||||||
|
|
||||||
proc writeValue*(w: var JsonWriter, value: auto) =
|
proc writeValue*(w: var JsonWriter, value: auto) =
|
||||||
mixin enumInstanceSerializedFields, writeValue, writeFieldIMPL
|
mixin enumInstanceSerializedFields, writeValue, writeFieldIMPL
|
||||||
|
|
||||||
@ -140,7 +147,7 @@ proc writeValue*(w: var JsonWriter, value: auto) =
|
|||||||
append "null"
|
append "null"
|
||||||
else:
|
else:
|
||||||
writeValue(w, value[])
|
writeValue(w, value[])
|
||||||
elif value is string|cstring:
|
elif isStringLike(value):
|
||||||
append '"'
|
append '"'
|
||||||
|
|
||||||
template addPrefixSlash(c) =
|
template addPrefixSlash(c) =
|
||||||
|
@ -149,3 +149,8 @@ suite "toJson tests":
|
|||||||
check:
|
check:
|
||||||
$original == $decoded
|
$original == $decoded
|
||||||
|
|
||||||
|
test "openArray[char]":
|
||||||
|
check:
|
||||||
|
"abc" == Json.decode(Json.encode(['a', 'b', 'c']), string)
|
||||||
|
"abc" == Json.decode(Json.encode(@['a', 'b', 'c']), string)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user