nimbus-eth2/beacon_chain/networking/libp2p_json_serialization.nim

39 lines
1.4 KiB
Nim
Raw Permalink Normal View History

# beacon_chain
2022-07-29 10:53:42 +00:00
# Copyright (c) 2018-2022 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
2022-07-29 10:53:42 +00:00
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import libp2p/[peerid, multiaddress], json_serialization
export json_serialization
proc writeValue*(writer: var JsonWriter, value: PeerId) {.
raises: [Defect, IOError].} =
writer.writeValue $value
proc readValue*(reader: var JsonReader, value: var PeerId) {.
raises: [Defect, IOError, SerializationError].} =
let res = PeerId.init reader.readValue(string)
2020-07-01 11:41:40 +00:00
if res.isOk:
value = res.get()
else:
raiseUnexpectedValue(reader, $res.error)
proc writeValue*(writer: var JsonWriter, value: MultiAddress) {.
raises: [Defect, IOError].} =
writer.writeValue $value
proc readValue*(reader: var JsonReader, value: var MultiAddress) {.
raises: [Defect, IOError, SerializationError].} =
2020-07-01 11:41:40 +00:00
let res = MultiAddress.init reader.readValue(string)
if res.isOk:
value = res.value
2020-06-05 15:08:50 +00:00
else:
2020-07-01 11:41:40 +00:00
raiseUnexpectedValue(reader, $res.error)