From 96ae7e130e10bccc70950912c1d1a5543168dae0 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Tue, 13 Feb 2024 17:38:18 +1100 Subject: [PATCH] fix: change serializer funcs to procs (#7) * change serializer funcs to procs `%` could have side effects if called from chronicles * clean up --- serde/json/deserializer.nim | 2 -- serde/json/serializer.nim | 30 +++++++++++++++--------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/serde/json/deserializer.nim b/serde/json/deserializer.nim index a4069a8..72d6896 100644 --- a/serde/json/deserializer.nim +++ b/serde/json/deserializer.nim @@ -234,8 +234,6 @@ proc fromJson*(_: type JsonNode, json: string): ?!JsonNode = proc fromJson*[T: ref object or object](_: type T, bytes: openArray[byte]): ?!T = let json = string.fromBytes(bytes) - static: - echo "typeof json after parse: ", typeof json T.fromJson(json) proc fromJson*[T: ref object or object](_: type T, json: string): ?!T = diff --git a/serde/json/serializer.nim b/serde/json/serializer.nim index 16d04eb..db9d2e6 100644 --- a/serde/json/serializer.nim +++ b/serde/json/serializer.nim @@ -23,28 +23,28 @@ export types logScope: topics = "json serialization" -func `%`*(s: string): JsonNode = +proc `%`*(s: string): JsonNode = newJString(s) -func `%`*(n: uint): JsonNode = +proc `%`*(n: uint): JsonNode = if n > cast[uint](int.high): newJString($n) else: newJInt(BiggestInt(n)) -func `%`*(n: int): JsonNode = +proc `%`*(n: int): JsonNode = newJInt(n) -func `%`*(n: BiggestUInt): JsonNode = +proc `%`*(n: BiggestUInt): JsonNode = if n > cast[BiggestUInt](BiggestInt.high): newJString($n) else: newJInt(BiggestInt(n)) -func `%`*(n: BiggestInt): JsonNode = +proc `%`*(n: BiggestInt): JsonNode = newJInt(n) -func `%`*(n: float): JsonNode = +proc `%`*(n: float): JsonNode = if n != n: newJString("nan") elif n == Inf: @@ -54,10 +54,10 @@ func `%`*(n: float): JsonNode = else: newJFloat(n) -func `%`*(b: bool): JsonNode = +proc `%`*(b: bool): JsonNode = newJBool(b) -func `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode = +proc `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode = if keyVals.len == 0: return newJArray() let jObj = newJObject() @@ -68,13 +68,13 @@ func `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode = template `%`*(j: JsonNode): JsonNode = j -func `%`*[T](table: Table[string, T] | OrderedTable[string, T]): JsonNode = +proc `%`*[T](table: Table[string, T] | OrderedTable[string, T]): JsonNode = let jObj = newJObject() for k, v in table: jObj[k] = ? %v jObj -func `%`*[T](opt: Option[T]): JsonNode = +proc `%`*[T](opt: Option[T]): JsonNode = if opt.isSome: %(opt.get) else: @@ -125,22 +125,22 @@ proc `%`*[T: object or ref object](obj: T): JsonNode = proc `%`*(o: enum): JsonNode = % $o -func `%`*(stint: StInt | StUint): JsonNode = +proc `%`*(stint: StInt | StUint): JsonNode = %stint.toString -func `%`*(cstr: cstring): JsonNode = +proc `%`*(cstr: cstring): JsonNode = % $cstr -func `%`*(arr: openArray[byte]): JsonNode = +proc `%`*(arr: openArray[byte]): JsonNode = %arr.to0xHex -func `%`*[T](elements: openArray[T]): JsonNode = +proc `%`*[T](elements: openArray[T]): JsonNode = let jObj = newJArray() for elem in elements: jObj.add(%elem) jObj -func `%`*[T: distinct](id: T): JsonNode = +proc `%`*[T: distinct](id: T): JsonNode = type baseType = T.distinctBase %baseType(id)