Skip to content

Commit

Permalink
Allow Codable compliant values in json's init(any). (#302)
Browse files Browse the repository at this point in the history
* Allow Codable compliant values in json's init(any).

* updated test
  • Loading branch information
bsneed authored Feb 28, 2024
1 parent 33f07bd commit 813b657
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/Segment/Utilities/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public enum JSON: Equatable {
public init<T: Codable>(with value: T) throws {
let encoder = JSONSafeEncoder.default
let json = try encoder.encode(value)
let output = try JSONSerialization.jsonObject(with: json)
let output = try JSONSerialization.jsonObject(with: json, options: .fragmentsAllowed)
try self.init(output)
}

Expand Down Expand Up @@ -113,6 +113,8 @@ public enum JSON: Equatable {
self = .object(try object.mapValues(JSON.init))
case let json as JSON:
self = json
case let codable as Codable:
self = try Self.init(with: codable)
// we don't work with whatever is being supplied
default:
throw JSONError.nonJSONType(type: "\(value.self)")
Expand Down
55 changes: 55 additions & 0 deletions Tests/Segment-Tests/JSON_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,59 @@ class JSONTests: XCTestCase {
XCTFail()
}
}

func testJSONCodableDict() throws {
enum StringEnum: String, Codable {
case test1
case test2
case test3
}

enum IntEnum: Int, Codable {
case test1
case test2
case test3
}

struct SubStruct: Codable {
var x: Int = 23
}

struct CodableStruct: Codable {
var a: Int = 47
var b: String = "hello"
var c: SubStruct = SubStruct()
}

let dict: [String: Any] = [
"uuid": UUID(),
"strEnum": StringEnum.test2,
"intEnum": IntEnum.test2,
"struct": CodableStruct()
]

do {
let json = try JSON(dict)
print(json.prettyPrint())

let strEnum: String? = json[keyPath: "strEnum"]
XCTAssertEqual(strEnum, "test2")

let intEnum: Int? = json[keyPath: "intEnum"]
XCTAssertEqual(intEnum, 1)

let b: String? = json[keyPath: "struct.b"]
XCTAssertEqual(b, "hello")

let x: Int? = json[keyPath: "struct.c.x"]
XCTAssertEqual(x, 23)

let uuid: String? = json[keyPath: "uuid"]
XCTAssertEqual(uuid!.count, 36)

} catch {
print(error)
XCTFail()
}
}
}

0 comments on commit 813b657

Please sign in to comment.