Skip to content

Commit

Permalink
fix: change serializer funcs to procs (#7)
Browse files Browse the repository at this point in the history
* change serializer funcs to procs

`%` could have side effects if called from chronicles

* clean up
  • Loading branch information
emizzle authored Feb 13, 2024
1 parent ae23a5f commit 96ae7e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
2 changes: 0 additions & 2 deletions serde/json/deserializer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
30 changes: 15 additions & 15 deletions serde/json/serializer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 96ae7e1

Please sign in to comment.