nim-json-serialization/json_serialization/std/net.nim

42 lines
1.2 KiB
Nim
Raw Normal View History

2023-12-13 09:07:57 +00:00
# json-serialization
# Copyright (c) 2019-2024 Status Research & Development GmbH
2023-12-13 09:07:57 +00:00
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
2019-08-12 14:53:04 +00:00
import
std/[net, strutils],
chronos/transports/common,
../../json_serialization
2019-08-12 14:53:04 +00:00
export
net, common
proc writeValue*(
writer: var JsonWriter, value: IpAddress) {.raises: [IOError].} =
writeValue(writer, $value)
proc readValue*(reader: var JsonReader, value: var IpAddress) =
let s = reader.readValue(string)
try:
value = parseIpAddress s
except CatchableError:
raiseUnexpectedValue(reader, "Invalid IP address")
proc writeValue*(
writer: var JsonWriter, value: Port) {.raises: [IOError].} =
writeValue(writer, uint16 value)
proc readValue*(reader: var JsonReader, value: var Port) =
value = Port reader.readValue(uint16)
proc writeValue*(
writer: var JsonWriter, value: AddressFamily) {.raises: [IOError].} =
2019-08-12 14:53:04 +00:00
writeValue(writer, $value)
proc readValue*(reader: var JsonReader, value: var AddressFamily) =
value = parseEnum[AddressFamily](reader.readValue(string))