Skip to content

Handle Set properly when given to JSON instance. #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Sources/Segment/Utilities/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public enum JSON: Equatable {
self = .string(string)
case let bool as Bool:
self = .bool(bool)
case let array as [Any]:
case let aSet as Set<AnyHashable>:
self = .array(try aSet.map(JSON.init))
case let array as Array<Any>:
self = .array(try array.map(JSON.init))
case let object as [String: Any]:
self = .object(try object.mapValues(JSON.init))
Expand Down
10 changes: 10 additions & 0 deletions Tests/Segment-Tests/JSON_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ class JSONTests: XCTestCase {
}
}

func testJSONCollectionTypes() throws {
let testSet: Set = ["1", "2", "3"]
let traits = try! JSON(["type": NSNull(), "preferences": ["bwack"], "key": testSet])
let jsonSet = traits["key"]
XCTAssertNotNil(jsonSet)
let array = jsonSet!.arrayValue!
XCTAssertNotNil(array)
XCTAssertEqual(array.count, 3)
}

func testJSONNil() throws {
let traits = try JSON(["type": NSNull(), "preferences": ["bwack"], "key": nil])
let encoder = JSONEncoder()
Expand Down