From cbec44d339c962f2010bf796af0b1ba1a8c3fa7f Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Mon, 25 Mar 2019 01:11:54 +0200 Subject: [PATCH] Handle more standard-libary types --- json_serialization/writer.nim | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/json_serialization/writer.nim b/json_serialization/writer.nim index 7d3d303..3383517 100644 --- a/json_serialization/writer.nim +++ b/json_serialization/writer.nim @@ -1,6 +1,6 @@ import typetraits, - faststreams/output_stream, serialization + faststreams/output_stream, serialization, json type JsonWriterState = enum @@ -15,6 +15,8 @@ type nestingLevel*: int # read-only state: JsonWriterState + JsonString* = distinct string + proc init*(T: type JsonWriter, stream: OutputStreamVar, pretty = false, typeAnnotations = false): T = result.stream = stream @@ -23,13 +25,9 @@ proc init*(T: type JsonWriter, stream: OutputStreamVar, result.nestingLevel = if pretty: 0 else: -1 result.state = RecordExpected -proc writeImpl(w: var JsonWriter, value: auto) - -template writeValue*(w: var JsonWriter, v: auto) = - writeImpl(w, v) - proc beginRecord*(w: var JsonWriter, T: type) proc beginRecord*(w: var JsonWriter) +proc writeValue*(w: var JsonWriter, value: auto) template append(x: untyped) = w.stream.append x @@ -121,11 +119,21 @@ proc writeArray[T](w: var JsonWriter, elements: openarray[T]) = append ']' -proc writeImpl(w: var JsonWriter, value: auto) = +proc writeValue*(w: var JsonWriter, value: auto) = template addChar(c) = append c - when value is string: + when value is JsonNode: + append if w.hasPrettyOutput: value.pretty + else: $value + elif value is JsonString: + append string(value) + elif value is ref: + if value == nil: + append "null" + else: + writeValue(w, value[]) + elif value is string|cstring: addChar '"' template addPrefixSlash(c) = @@ -154,7 +162,6 @@ proc writeImpl(w: var JsonWriter, value: auto) = else: addChar c addChar '"' - elif value is bool: append if value: "true" else: "false" elif value is enum: