Open History to see the last commit
1import Foundation
2
3/// A lossless, Sendable JSON representation used at protocol boundaries that
4/// intentionally carry provider-defined payloads.
5public enum JSONValue: Codable, Equatable, Hashable, Sendable {
6 case null
7 case bool(Bool)
8 case integer(Int64)
9 case unsignedInteger(UInt64)
10 case number(Double)
11 case string(String)
12 case array([JSONValue])
13 case object([String: JSONValue])
14
15 public init(from decoder: any Decoder) throws {
16 let container = try decoder.singleValueContainer()
17 if container.decodeNil() {
18 self = .null
19 } else if let value = try? container.decode(Bool.self) {
20 self = .bool(value)
21 } else if let value = try? container.decode(Int64.self) {
22 let double = Double(value)
23 self = Int64(exactly: double) == value ? .number(double) : .integer(value)
24 } else if let value = try? container.decode(UInt64.self) {
25 let double = Double(value)
26 self = UInt64(exactly: double) == value
27 ? .number(double)
28 : .unsignedInteger(value)
29 } else if let value = try? container.decode(Double.self) {
30 self = .number(value)
31 } else if let value = try? container.decode(String.self) {
32 self = .string(value)
33 } else if let value = try? container.decode([JSONValue].self) {
34 self = .array(value)
35 } else {
36 self = .object(try container.decode([String: JSONValue].self))
37 }
38 }
39
40 public func encode(to encoder: any Encoder) throws {
41 var container = encoder.singleValueContainer()
42 switch self {
43 case .null:
44 try container.encodeNil()
45 case let .bool(value):
46 try container.encode(value)
47 case let .integer(value):
48 try container.encode(value)
49 case let .unsignedInteger(value):
50 try container.encode(value)
51 case let .number(value):
52 try container.encode(value)
53 case let .string(value):
54 try container.encode(value)
55 case let .array(value):
56 try container.encode(value)
57 case let .object(value):
58 try container.encode(value)
59 }
60 }
61
62 public subscript(key: String) -> JSONValue? {
63 guard case let .object(object) = self else { return nil }
64 return object[key]
65 }
66
67 public var stringValue: String? {
68 guard case let .string(value) = self else { return nil }
69 return value
70 }
71
72 public var boolValue: Bool? {
73 guard case let .bool(value) = self else { return nil }
74 return value
75 }
76
77 public static func encode<T: Encodable & Sendable>(
78 _ value: T,
79 encoder: JSONEncoder = .t3
80 ) throws -> JSONValue {
81 let data = try encoder.encode(value)
82 return try JSONDecoder.t3.decode(JSONValue.self, from: data)
83 }
84
85 public func decode<T: Decodable & Sendable>(
86 _ type: T.Type,
87 decoder: JSONDecoder = .t3
88 ) throws -> T {
89 // The intermediate bytes are discarded immediately, so skip the
90 // deterministic-output formatting the wire encoder pays for.
91 try decoder.decode(type, from: JSONEncoder.t3Intermediate.encode(self))
92 }
93}
94
95// Encoders and decoders are configured once and never mutated afterwards, so
96// shared instances are safe for concurrent use and avoid rebuilding coder
97// state on every RPC message.
98extension JSONEncoder {
99 /// Deterministic output for wire payloads.
100 public static let t3: JSONEncoder = {
101 let encoder = JSONEncoder()
102 encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
103 return encoder
104 }()
105
106 /// Throwaway intermediate encoding (JSONValue -> concrete type bridging).
107 static let t3Intermediate: JSONEncoder = {
108 let encoder = JSONEncoder()
109 encoder.outputFormatting = [.withoutEscapingSlashes]
110 return encoder
111 }()
112}
113
114extension JSONDecoder {
115 public static let t3 = JSONDecoder()
116}