From 193d1039346d1c402acc7618b3759a2b0b1279aa Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Fri, 8 May 2020 16:12:29 +0300 Subject: [PATCH] Read keys of table as string as this is only valid json, but convert them if needed --- json_serialization/std/tables.nim | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/json_serialization/std/tables.nim b/json_serialization/std/tables.nim index 39e3cef..c33ea89 100644 --- a/json_serialization/std/tables.nim +++ b/json_serialization/std/tables.nim @@ -7,13 +7,27 @@ type proc writeValue*(writer: var JsonWriter, value: TableType) = writer.beginRecord() for key, val in value: - writer.writeField key, val + writer.writeField $key, val writer.endRecord() + +template to*(a: string, b: typed): untyped = + {.error: "doesnt support keys with type " & $type(b) .} + +template to*(a: string, b: type int): int = + parseInt(a) + +template to*(a: string, b: type float): float = + parseFloat(a) + +template to*(a: string, b: type string): string = + a + + proc readValue*(reader: var JsonReader, value: var TableType) = type KeyType = type(value.keys) type ValueType = type(value.values) value = init TableType - for key, val in readObject(reader, KeyType, ValueType): - value[key] = val + for key, val in readObject(reader, string, ValueType): + value[to(key, KeyType)] = val