nim-json-serialization/json_serialization/std/tables.nim

35 lines
956 B
Nim
Raw Normal View History

import stew/shims/tables, ../../json_serialization/[reader, writer, lexer]
export tables
type
TableType = OrderedTable | Table
proc writeValue*(
writer: var JsonWriter, value: TableType) {.raises: [IOError].} =
writer.beginRecord()
for key, val in value:
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) =
2023-06-05 08:23:36 +00:00
try:
type KeyType = type(value.keys)
type ValueType = type(value.values)
value = init TableType
for key, val in readObject(reader, string, ValueType):
value[to(key, KeyType)] = val
except ValueError as ex:
reader.raiseUnexpectedValue("TableType: " & ex.msg)