Skip to content

Commit 7042b47

Browse files
authored
Handle Set properly when given to JSON instance. (#201)
* Updated JSON to accept any collection type as an array * Handle set explicitly.
1 parent c281065 commit 7042b47

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Sources/Segment/Utilities/JSON.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public enum JSON: Equatable {
5959
self = .string(string)
6060
case let bool as Bool:
6161
self = .bool(bool)
62-
case let array as [Any]:
62+
case let aSet as Set<AnyHashable>:
63+
self = .array(try aSet.map(JSON.init))
64+
case let array as Array<Any>:
6365
self = .array(try array.map(JSON.init))
6466
case let object as [String: Any]:
6567
self = .object(try object.mapValues(JSON.init))

Tests/Segment-Tests/JSON_Tests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ class JSONTests: XCTestCase {
5252
}
5353
}
5454

55+
func testJSONCollectionTypes() throws {
56+
let testSet: Set = ["1", "2", "3"]
57+
let traits = try! JSON(["type": NSNull(), "preferences": ["bwack"], "key": testSet])
58+
let jsonSet = traits["key"]
59+
XCTAssertNotNil(jsonSet)
60+
let array = jsonSet!.arrayValue!
61+
XCTAssertNotNil(array)
62+
XCTAssertEqual(array.count, 3)
63+
}
64+
5565
func testJSONNil() throws {
5666
let traits = try JSON(["type": NSNull(), "preferences": ["bwack"], "key": nil])
5767
let encoder = JSONEncoder()

0 commit comments

Comments
 (0)