From d0018ed7c29271cd7246f0147f86d819e0eed5dd Mon Sep 17 00:00:00 2001 From: ytyubox Date: Wed, 31 Aug 2022 01:07:45 +0800 Subject: [PATCH 1/2] fix --- Sources/AnyCodable/AnyCodable.swift | 9 ++++++- Tests/AnyCodableTests/AnyCodableTests.swift | 30 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Sources/AnyCodable/AnyCodable.swift b/Sources/AnyCodable/AnyCodable.swift index 7d25ed1..31abff8 100644 --- a/Sources/AnyCodable/AnyCodable.swift +++ b/Sources/AnyCodable/AnyCodable.swift @@ -1,3 +1,4 @@ +import Foundation /** A type-erased `Codable` value. @@ -56,8 +57,14 @@ extension AnyCodable: Equatable { return lhs == rhs case let (lhs as [String: AnyCodable], rhs as [String: 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 [AnyCodable], rhs as [AnyCodable]): - return lhs == rhs + return NSArray(array: lhs) == NSArray(array: 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")) From f86e4473723db4a4457b38ccb13385da1f7b2a32 Mon Sep 17 00:00:00 2001 From: ytyubox Date: Wed, 31 Aug 2022 10:15:14 +0800 Subject: [PATCH 2/2] revert change of (lhs as [AnyCodable], rhs as [AnyCodable]) --- Sources/AnyCodable/AnyCodable.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/AnyCodable/AnyCodable.swift b/Sources/AnyCodable/AnyCodable.swift index 31abff8..7429525 100644 --- a/Sources/AnyCodable/AnyCodable.swift +++ b/Sources/AnyCodable/AnyCodable.swift @@ -57,10 +57,10 @@ extension AnyCodable: Equatable { return lhs == rhs case let (lhs as [String: AnyCodable], rhs as [String: AnyCodable]): 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 [AnyCodable], rhs as [AnyCodable]): - return NSArray(array: lhs) == NSArray(array: rhs) case let (lhs as [Any], rhs as [Any]): return NSArray(array: lhs) == NSArray(array: rhs) case is (NSNull, NSNull):