87 lines
2.6 KiB
Swift
87 lines
2.6 KiB
Swift
import Foundation
|
|
|
|
/// A lossless, Sendable JSON representation used at protocol boundaries that
|
|
/// intentionally carry provider-defined payloads.
|
|
public enum JSONValue: Codable, Equatable, Sendable {
|
|
case null
|
|
case bool(Bool)
|
|
case number(Double)
|
|
case string(String)
|
|
case array([JSONValue])
|
|
case object([String: JSONValue])
|
|
|
|
public init(from decoder: any Decoder) throws {
|
|
let container = try decoder.singleValueContainer()
|
|
if container.decodeNil() {
|
|
self = .null
|
|
} else if let value = try? container.decode(Bool.self) {
|
|
self = .bool(value)
|
|
} else if let value = try? container.decode(Double.self) {
|
|
self = .number(value)
|
|
} else if let value = try? container.decode(String.self) {
|
|
self = .string(value)
|
|
} else if let value = try? container.decode([JSONValue].self) {
|
|
self = .array(value)
|
|
} else {
|
|
self = .object(try container.decode([String: JSONValue].self))
|
|
}
|
|
}
|
|
|
|
public func encode(to encoder: any Encoder) throws {
|
|
var container = encoder.singleValueContainer()
|
|
switch self {
|
|
case .null:
|
|
try container.encodeNil()
|
|
case let .bool(value):
|
|
try container.encode(value)
|
|
case let .number(value):
|
|
try container.encode(value)
|
|
case let .string(value):
|
|
try container.encode(value)
|
|
case let .array(value):
|
|
try container.encode(value)
|
|
case let .object(value):
|
|
try container.encode(value)
|
|
}
|
|
}
|
|
|
|
public subscript(key: String) -> JSONValue? {
|
|
guard case let .object(object) = self else { return nil }
|
|
return object[key]
|
|
}
|
|
|
|
public var stringValue: String? {
|
|
guard case let .string(value) = self else { return nil }
|
|
return value
|
|
}
|
|
|
|
public static func encode<T: Encodable & Sendable>(
|
|
_ value: T,
|
|
encoder: JSONEncoder = .t3
|
|
) throws -> JSONValue {
|
|
let data = try encoder.encode(value)
|
|
return try JSONDecoder.t3.decode(JSONValue.self, from: data)
|
|
}
|
|
|
|
public func decode<T: Decodable & Sendable>(
|
|
_ type: T.Type,
|
|
decoder: JSONDecoder = .t3
|
|
) throws -> T {
|
|
try decoder.decode(type, from: JSONEncoder.t3.encode(self))
|
|
}
|
|
}
|
|
|
|
extension JSONEncoder {
|
|
public static var t3: JSONEncoder {
|
|
let encoder = JSONEncoder()
|
|
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
|
|
return encoder
|
|
}
|
|
}
|
|
|
|
extension JSONDecoder {
|
|
public static var t3: JSONDecoder {
|
|
JSONDecoder()
|
|
}
|
|
}
|