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, 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 static func encode<T: Encodable & Sendable>(
73 _ value: T,
74 encoder: JSONEncoder = .t3
75 ) throws -> JSONValue {
76 let data = try encoder.encode(value)
77 return try JSONDecoder.t3.decode(JSONValue.self, from: data)
78 }
79
80 public func decode<T: Decodable & Sendable>(
81 _ type: T.Type,
82 decoder: JSONDecoder = .t3
83 ) throws -> T {
84 // The intermediate bytes are discarded immediately, so skip the
85 // deterministic-output formatting the wire encoder pays for.
86 try decoder.decode(type, from: JSONEncoder.t3Intermediate.encode(self))
87 }
88}
89
90// Encoders and decoders are configured once and never mutated afterwards, so
91// shared instances are safe for concurrent use and avoid rebuilding coder
92// state on every RPC message.
93extension JSONEncoder {
94 /// Deterministic output for wire payloads.
95 public static let t3: JSONEncoder = {
96 let encoder = JSONEncoder()
97 encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
98 return encoder
99 }()
100
101 /// Throwaway intermediate encoding (JSONValue -> concrete type bridging).
102 static let t3Intermediate: JSONEncoder = {
103 let encoder = JSONEncoder()
104 encoder.outputFormatting = [.withoutEscapingSlashes]
105 return encoder
106 }()
107}
108
109extension JSONDecoder {
110 public static let t3 = JSONDecoder()
111}