From cb7e7a3caef6e39d0645d3873579bff950e96418 Mon Sep 17 00:00:00 2001 From: coffeepots Date: Wed, 9 May 2018 13:07:32 +0100 Subject: [PATCH] Add converters to and from common `stint` types and `byte` --- eth-rpc/server/jsonconverters.nim | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 eth-rpc/server/jsonconverters.nim diff --git a/eth-rpc/server/jsonconverters.nim b/eth-rpc/server/jsonconverters.nim new file mode 100644 index 0000000..43d5889 --- /dev/null +++ b/eth-rpc/server/jsonconverters.nim @@ -0,0 +1,25 @@ +import json, stint + +iterator bytes*(i: UInt256|Int256): byte = + let b = cast[ptr array[32, byte]](i.unsafeaddr) + var pos = 0 + while pos < 32: + yield b[pos] + pos += 1 + +proc `%`*(n: UInt256): JsonNode = + ## Generic constructor for JSON data. Creates a new `JInt JsonNode`. + result = newJArray() + for elem in n.bytes: + result.add(%int(elem)) + +proc `%`*(n: Int256): JsonNode = + ## Generic constructor for JSON data. Creates a new `JInt JsonNode`. + result = newJArray() + for elem in n.bytes: + result.add(%int(elem)) + +proc `%`*(n: byte): JsonNode = + ## Generic constructor for JSON data. Creates a new `JInt JsonNode`. + result = newJInt(int(n)) +