• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / supabase-swift / 28257314305

26 Jun 2026 04:21PM UTC coverage: 80.889% (+0.1%) from 80.772%
28257314305

push

github

grdsdev
chore: configure release-please for beta prerelease on v3

8279 of 10235 relevant lines covered (80.89%)

34.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.35
/Sources/Helpers/AnyJSON/AnyJSON.swift
1
import Foundation
2

3
public typealias JSONObject = [String: AnyJSON]
4
public typealias JSONArray = [AnyJSON]
5

6
/// An enumeration that represents JSON-compatible values of various types.
7
public enum AnyJSON: Sendable, Codable, Hashable {
8
  /// Represents a `null` JSON value.
9
  case null
10
  /// Represents a JSON boolean value.
11
  case bool(Bool)
12
  /// Represents a JSON number (integer) value.
13
  case integer(Int)
14
  /// Represents a JSON number (floating-point) value.
15
  case double(Double)
16
  /// Represents a JSON string value.
17
  case string(String)
18
  /// Represents a JSON object (dictionary) value.
19
  case object(JSONObject)
20
  /// Represents a JSON array (list) value.
21
  case array(JSONArray)
22

23
  /// Returns the underlying Swift value corresponding to the `AnyJSON` instance.
24
  ///
25
  /// - Note: For `.object` and `.array` cases, the returned value contains recursively transformed `AnyJSON` instances.
26
  public var value: Any {
80✔
27
    switch self {
80✔
28
    case .null: NSNull()
80✔
29
    case .string(let string): string
80✔
30
    case .integer(let val): val
80✔
31
    case .double(let val): val
80✔
32
    case .object(let dictionary): dictionary.mapValues(\.value)
80✔
33
    case .array(let array): array.map(\.value)
80✔
34
    case .bool(let bool): bool
80✔
35
    }
80✔
36
  }
80✔
37

38
  public var isNil: Bool {
11✔
39
    if case .null = self {
11✔
40
      return true
4✔
41
    }
7✔
42

7✔
43
    return false
7✔
44
  }
11✔
45

46
  public var boolValue: Bool? {
11✔
47
    if case .bool(let val) = self {
11✔
48
      return val
5✔
49
    }
6✔
50
    return nil
6✔
51
  }
11✔
52

53
  public var objectValue: JSONObject? {
324✔
54
    if case .object(let dictionary) = self {
324✔
55
      return dictionary
316✔
56
    }
316✔
57
    return nil
8✔
58
  }
324✔
59

60
  public var arrayValue: JSONArray? {
25✔
61
    if case .array(let array) = self {
25✔
62
      return array
19✔
63
    }
19✔
64
    return nil
6✔
65
  }
25✔
66

67
  public var stringValue: String? {
672✔
68
    if case .string(let string) = self {
672✔
69
      return string
603✔
70
    }
603✔
71
    return nil
69✔
72
  }
672✔
73

74
  public var intValue: Int? {
14✔
75
    if case .integer(let val) = self {
14✔
76
      return val
8✔
77
    }
8✔
78
    return nil
6✔
79
  }
14✔
80

81
  public var doubleValue: Double? {
9✔
82
    if case .double(let val) = self {
9✔
83
      return val
3✔
84
    }
6✔
85
    return nil
6✔
86
  }
9✔
87

88
  public init(from decoder: any Decoder) throws {
4,688✔
89
    let container = try decoder.singleValueContainer()
4,688✔
90

4,688✔
91
    if container.decodeNil() {
4,688✔
92
      self = .null
287✔
93
    } else if let val = try? container.decode(Int.self) {
287✔
94
      self = .integer(val)
129✔
95
    } else if let val = try? container.decode(Double.self) {
129✔
96
      self = .double(val)
8✔
97
    } else if let val = try? container.decode(String.self) {
2,572✔
98
      self = .string(val)
2,572✔
99
    } else if let val = try? container.decode(Bool.self) {
2,572✔
100
      self = .bool(val)
552✔
101
    } else if let val = try? container.decode(JSONArray.self) {
552✔
102
      self = .array(val)
462✔
103
    } else if let val = try? container.decode(JSONObject.self) {
678✔
104
      self = .object(val)
678✔
105
    } else {
678✔
106
      throw DecodingError.dataCorrupted(
×
107
        .init(codingPath: decoder.codingPath, debugDescription: "Invalid JSON value.")
×
108
      )
×
109
    }
4,688✔
110
  }
4,688✔
111

112
  public func encode(to encoder: any Encoder) throws {
2,819✔
113
    var container = encoder.singleValueContainer()
2,819✔
114
    switch self {
2,819✔
115
    case .null: try container.encodeNil()
2,819✔
116
    case .array(let val): try container.encode(val)
2,819✔
117
    case .object(let val): try container.encode(val)
2,819✔
118
    case .string(let val): try container.encode(val)
2,819✔
119
    case .integer(let val): try container.encode(val)
2,819✔
120
    case .double(let val): try container.encode(val)
2,819✔
121
    case .bool(let val): try container.encode(val)
2,819✔
122
    }
2,819✔
123
  }
2,819✔
124
}
125

126
extension AnyJSON: ExpressibleByNilLiteral {
127
  public init(nilLiteral _: ()) {
68✔
128
    self = .null
68✔
129
  }
68✔
130
}
131

132
extension AnyJSON: ExpressibleByStringLiteral {
133
  public init(stringLiteral value: String) {
321✔
134
    self = .string(value)
321✔
135
  }
321✔
136
}
137

138
extension AnyJSON: ExpressibleByArrayLiteral {
139
  public init(arrayLiteral elements: AnyJSON...) {
99✔
140
    self = .array(elements)
99✔
141
  }
99✔
142
}
143

144
extension AnyJSON: ExpressibleByIntegerLiteral {
145
  public init(integerLiteral value: Int) {
467✔
146
    self = .integer(value)
467✔
147
  }
467✔
148
}
149

150
extension AnyJSON: ExpressibleByFloatLiteral {
151
  public init(floatLiteral value: Double) {
67✔
152
    self = .double(value)
67✔
153
  }
67✔
154
}
155

156
extension AnyJSON: ExpressibleByBooleanLiteral {
157
  public init(booleanLiteral value: Bool) {
70✔
158
    self = .bool(value)
70✔
159
  }
70✔
160
}
161

162
extension AnyJSON: ExpressibleByDictionaryLiteral {
163
  public init(dictionaryLiteral elements: (String, AnyJSON)...) {
226✔
164
    self = .object(Dictionary(uniqueKeysWithValues: elements))
226✔
165
  }
226✔
166
}
167

168
extension AnyJSON: CustomStringConvertible {
169
  public var description: String {
36✔
170
    String(describing: value)
36✔
171
  }
36✔
172
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc