nim-json-rpc/tests/private/stintjson.nim
andri lim e0b077fea4
Upgrade rpc router internals (#178)
* Upgrade rpc router internals

* use new chronos asyncraises

* Fix style mismatch

* Fix nim v2 compilation error

* Addresing review

* Remove unnecessary custom serializer and let the library do the work

* fix error message

* Update readme.md
2024-01-03 20:06:53 +07:00

45 lines
1.4 KiB
Nim

import
std/json,
stint,
../../json_rpc/private/jrpc_conv
{.push gcsafe, raises: [].}
template stintStr(n: UInt256|Int256): JsonNode =
var s = n.toHex
if s.len mod 2 != 0: s = "0" & s
s = "0x" & s
%s
proc `%`*(n: UInt256): JsonNode = n.stintStr
proc `%`*(n: Int256): JsonNode = n.stintStr
proc writeValue*(w: var JsonWriter[JrpcConv], val: UInt256)
{.gcsafe, raises: [IOError].} =
writeValue(w, val.stintStr)
proc writeValue*(w: var JsonWriter[JrpcConv], val: ref UInt256)
{.gcsafe, raises: [IOError].} =
writeValue(w, val[].stintStr)
proc readValue*(r: var JsonReader[JrpcConv], v: var UInt256)
{.gcsafe, raises: [JsonReaderError].} =
## Allows UInt256 to be passed as a json string.
## Expects base 16 string, starting with "0x".
try:
let hexStr = r.readValue string
if hexStr.len > 64 + 2: # including "0x"
raise newException(ValueError, "Value for '" & $v.type & "' too long for UInt256: " & $hexStr.len)
v = hexStr.parse(StUint[256], 16) # TODO: Handle errors
except Exception as err:
r.raiseUnexpectedValue("Error deserializing for '" & $v.type & "' stream: " & err.msg)
proc readValue*(r: var JsonReader[JrpcConv], v: var ref UInt256)
{.gcsafe, raises: [JsonReaderError].} =
## Allows ref UInt256 to be passed as a json string.
## Expects base 16 string, starting with "0x".
readValue(r, v[])
{.pop.}