blob: d8802e85cae094fe75d66430162a45e6da442d43 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
//
// IPAddress+Codable.swift
// MullvadVPN
//
// Created by pronebird on 12/06/2019.
// Copyright © 2019 Mullvad VPN AB. All rights reserved.
//
import Network
extension IPv4Address: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let ipString = try container.decode(String.self)
if let decoded = IPv4Address(ipString) {
self = decoded
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid IPv4 representation")
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(String(reflecting: self))
}
}
extension IPv6Address: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let ipString = try container.decode(String.self)
if let decoded = IPv6Address(ipString) {
self = decoded
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid IPv6 representation")
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(String(reflecting: self))
}
}
|