From 8c1a8ef8d9fd1705d4e8640b4c30df2caee76881 Mon Sep 17 00:00:00 2001 From: Viktor Kirilov Date: Thu, 11 Jun 2020 13:36:26 +0300 Subject: [PATCH] serialization for tuples - just like for objects --- json_rpc/jsonmarshal.nim | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/json_rpc/jsonmarshal.nim b/json_rpc/jsonmarshal.nim index 59829e9..97f711d 100644 --- a/json_rpc/jsonmarshal.nim +++ b/json_rpc/jsonmarshal.nim @@ -42,7 +42,7 @@ proc fromJson*[T: enum](n: JsonNode, argName: string, result: var T) = result = n.getInt().T # This can't be forward declared: https://github.com/nim-lang/Nim/issues/7868 -proc fromJson*[T: object](n: JsonNode, argName: string, result: var T) = +proc fromJson*[T: object|tuple](n: JsonNode, argName: string, result: var T) = n.kind.expect(JObject, argName) for k, v in fieldPairs(result): if v is Option and not n.hasKey(k): @@ -50,6 +50,13 @@ proc fromJson*[T: object](n: JsonNode, argName: string, result: var T) = else: fromJson(n[k], k, v) +# same as `proc `%`*[T: object](o: T): JsonNode` in json.nim from the stdlib +# TODO this PR removes the need for this: https://github.com/nim-lang/Nim/pull/14638 +proc `%`*[T: tuple](o: T): JsonNode = + ## Construct JsonNode from tuples and objects. + result = newJObject() + for k, v in o.fieldPairs: result[k] = %v + proc fromJson*[T](n: JsonNode, argName: string, result: var Option[T]) = # Allow JNull for options if n.kind != JNull: