diff --git a/Sources/AnyCodable/AnyCodable.swift b/Sources/AnyCodable/AnyCodable.swift index 7d25ed1..7429525 100644 --- a/Sources/AnyCodable/AnyCodable.swift +++ b/Sources/AnyCodable/AnyCodable.swift @@ -1,3 +1,4 @@ +import Foundation /** A type-erased `Codable` value. @@ -58,6 +59,12 @@ extension AnyCodable: Equatable { return lhs == rhs case let (lhs as [AnyCodable], rhs as [AnyCodable]): return lhs == rhs + case let (lhs as [String: Any], rhs as [String: Any]): + return NSDictionary(dictionary: lhs) == NSDictionary(dictionary: rhs) + case let (lhs as [Any], rhs as [Any]): + return NSArray(array: lhs) == NSArray(array: rhs) + case is (NSNull, NSNull): + return true default: return false } diff --git a/Tests/AnyCodableTests/AnyCodableTests.swift b/Tests/AnyCodableTests/AnyCodableTests.swift index 2cb74d1..df374f3 100644 --- a/Tests/AnyCodableTests/AnyCodableTests.swift +++ b/Tests/AnyCodableTests/AnyCodableTests.swift @@ -48,6 +48,36 @@ class AnyCodableTests: XCTestCase { XCTAssertEqual(dictionary["null"]?.value as! NSNull, NSNull()) } + func testJSONDecodingEquatable() throws { + let json = """ + { + "boolean": true, + "integer": 42, + "double": 3.141592653589793, + "string": "string", + "array": [1, 2, 3], + "nested": { + "a": "alpha", + "b": "bravo", + "c": "charlie" + }, + "null": null + } + """.data(using: .utf8)! + + let decoder = JSONDecoder() + let dictionary1 = try decoder.decode([String: AnyCodable].self, from: json) + let dictionary2 = try decoder.decode([String: AnyCodable].self, from: json) + + XCTAssertEqual(dictionary1["boolean"], dictionary2["boolean"]) + XCTAssertEqual(dictionary1["integer"], dictionary2["integer"]) + XCTAssertEqual(dictionary1["double"], dictionary2["double"]) + XCTAssertEqual(dictionary1["string"], dictionary2["string"]) + XCTAssertEqual(dictionary1["array"], dictionary2["array"]) + XCTAssertEqual(dictionary1["nested"], dictionary2["nested"]) + XCTAssertEqual(dictionary1["null"], dictionary2["null"]) + } + func testJSONEncoding() throws { let someCodable = AnyCodable(SomeCodable(string: "String", int: 100, bool: true, hasUnderscore: "another string"))