Skip to content

Commit e9cbdeb

Browse files
committed
improve JSONValue
1 parent bdea31a commit e9cbdeb

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

FlyingFox/Sources/JSON/JSONValue.swift

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@
3131

3232
import Foundation
3333

34-
public indirect enum JSONValue: @unchecked Sendable, Equatable {
34+
public indirect enum JSONValue: Sendable, Hashable {
3535
case object([String: JSONValue])
3636
case array([JSONValue])
3737
case string(String)
3838
case number(Double)
3939
case boolean(Bool)
4040
case null
4141

42-
public func getValue(for path: String) throws -> JSONValue {
43-
try getValue(for: JSONPath(parsing: path))
42+
public subscript(jsonPath: String) -> JSONValue? {
43+
get { try? getValue(for: jsonPath) }
44+
}
45+
46+
public func getValue(for jsonPath: String) throws -> JSONValue {
47+
try getValue(for: JSONPath(parsing: jsonPath))
4448
}
4549

4650
public func getValue(for path: JSONPath) throws -> JSONValue {
@@ -197,34 +201,26 @@ public extension JSONValue {
197201
}
198202
}
199203

200-
public func == (lhs: JSONValue, rhs: String) -> Bool {
201-
lhs == JSONValue.string(rhs)
202-
}
203-
204-
public func != (lhs: JSONValue, rhs: String) -> Bool {
205-
!(lhs == rhs)
206-
}
207-
208-
public func == (lhs: JSONValue, rhs: Double) -> Bool {
209-
lhs == JSONValue.number(rhs)
210-
}
211-
212-
public func != (lhs: JSONValue, rhs: Double) -> Bool {
213-
!(lhs == rhs)
214-
}
215-
216-
public func == (lhs: JSONValue, rhs: Bool) -> Bool {
217-
lhs == JSONValue.boolean(rhs)
204+
extension JSONValue: ExpressibleByStringLiteral {
205+
public init(stringLiteral value: String) {
206+
self = .string(value)
207+
}
218208
}
219209

220-
public func != (lhs: JSONValue, rhs: Bool) -> Bool {
221-
!(lhs == rhs)
210+
extension JSONValue: ExpressibleByBooleanLiteral {
211+
public init(booleanLiteral value: Bool) {
212+
self = .boolean(value)
213+
}
222214
}
223215

224-
public func == (lhs: JSONValue, rhs: some BinaryInteger) -> Bool {
225-
lhs == JSONValue.number(Double(rhs))
216+
extension JSONValue: ExpressibleByIntegerLiteral {
217+
public init(integerLiteral value: Int) {
218+
self = .number(Double(value))
219+
}
226220
}
227221

228-
public func != (lhs: JSONValue, rhs: some BinaryInteger) -> Bool {
229-
!(lhs == rhs)
222+
extension JSONValue: ExpressibleByFloatLiteral {
223+
public init(floatLiteral value: Double) {
224+
self = .number(value)
225+
}
230226
}

FlyingFox/Tests/JSON/JSONBodyPatternTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct JSONBodyPatternTests {
3838
@Test
3939
func pattern_MatchesJSONPath() async throws {
4040
// given
41-
let pattern = JSONBodyPattern { try $0.getValue(for: "$.name") == "fish" }
41+
let pattern = JSONBodyPattern { $0["$.name"] == "fish" }
4242

4343
// when then
4444
#expect(pattern.evaluate(json: #"{"name": "fish"}"#))
@@ -53,7 +53,7 @@ struct JSONBodyPatternTests {
5353
// given
5454
let route = HTTPRoute(
5555
"POST /fish",
56-
jsonBody: { try $0.getValue(for: "$.food") == "chips" }
56+
jsonBody: { $0["$.food"] == "chips" }
5757
)
5858

5959
// when

FlyingFox/Tests/JSON/JSONValueTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct JSONValueTests {
3838
@Test
3939
func json_string() throws {
4040
// given
41-
let val = try JSONValue("fish")
41+
let val = try JSONValue("fish" as Any)
4242

4343
// then
4444
#expect(val == "fish")
@@ -48,7 +48,7 @@ struct JSONValueTests {
4848
@Test
4949
func json_int() throws {
5050
// given
51-
let val = try JSONValue(Int(10))
51+
let val = try JSONValue(Int(10) as Any)
5252

5353
// then
5454
#expect(val == 10)
@@ -58,7 +58,7 @@ struct JSONValueTests {
5858
@Test
5959
func json_double() throws {
6060
// given
61-
let val = try JSONValue(10.5)
61+
let val = try JSONValue(10.5 as Any)
6262

6363
// then
6464
#expect(val == 10.5)
@@ -68,7 +68,7 @@ struct JSONValueTests {
6868
@Test
6969
func json_bool() throws {
7070
// given
71-
let val = try JSONValue(true)
71+
let val = try JSONValue(true as Any)
7272

7373
// then
7474
#expect(val == true)
@@ -78,7 +78,7 @@ struct JSONValueTests {
7878
@Test
7979
func json_nsnull() throws {
8080
// given
81-
let val = try JSONValue(NSNull())
81+
let val = try JSONValue(NSNull() as Any)
8282

8383
// then
8484
#expect(val == .null)

0 commit comments

Comments
 (0)