Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit 28c3246

Browse files
authored
Equatable fix (#71)
* fix * revert change of (lhs as [AnyCodable], rhs as [AnyCodable])
1 parent f9fda69 commit 28c3246

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Sources/AnyCodable/AnyCodable.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Foundation
12
/**
23
A type-erased `Codable` value.
34

@@ -58,6 +59,12 @@ extension AnyCodable: Equatable {
5859
return lhs == rhs
5960
case let (lhs as [AnyCodable], rhs as [AnyCodable]):
6061
return lhs == rhs
62+
case let (lhs as [String: Any], rhs as [String: Any]):
63+
return NSDictionary(dictionary: lhs) == NSDictionary(dictionary: rhs)
64+
case let (lhs as [Any], rhs as [Any]):
65+
return NSArray(array: lhs) == NSArray(array: rhs)
66+
case is (NSNull, NSNull):
67+
return true
6168
default:
6269
return false
6370
}

Tests/AnyCodableTests/AnyCodableTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ class AnyCodableTests: XCTestCase {
4848
XCTAssertEqual(dictionary["null"]?.value as! NSNull, NSNull())
4949
}
5050

51+
func testJSONDecodingEquatable() throws {
52+
let json = """
53+
{
54+
"boolean": true,
55+
"integer": 42,
56+
"double": 3.141592653589793,
57+
"string": "string",
58+
"array": [1, 2, 3],
59+
"nested": {
60+
"a": "alpha",
61+
"b": "bravo",
62+
"c": "charlie"
63+
},
64+
"null": null
65+
}
66+
""".data(using: .utf8)!
67+
68+
let decoder = JSONDecoder()
69+
let dictionary1 = try decoder.decode([String: AnyCodable].self, from: json)
70+
let dictionary2 = try decoder.decode([String: AnyCodable].self, from: json)
71+
72+
XCTAssertEqual(dictionary1["boolean"], dictionary2["boolean"])
73+
XCTAssertEqual(dictionary1["integer"], dictionary2["integer"])
74+
XCTAssertEqual(dictionary1["double"], dictionary2["double"])
75+
XCTAssertEqual(dictionary1["string"], dictionary2["string"])
76+
XCTAssertEqual(dictionary1["array"], dictionary2["array"])
77+
XCTAssertEqual(dictionary1["nested"], dictionary2["nested"])
78+
XCTAssertEqual(dictionary1["null"], dictionary2["null"])
79+
}
80+
5181
func testJSONEncoding() throws {
5282

5383
let someCodable = AnyCodable(SomeCodable(string: "String", int: 100, bool: true, hasUnderscore: "another string"))

0 commit comments

Comments
 (0)