diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 845df33..a7a0dab 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,8 +7,8 @@ on: jobs: build: - runs-on: macos-latest + steps: - uses: actions/checkout@v4 - name: Select Xcode diff --git a/JsonDecodeProtectionTests/NumberArrayProtectionTest.swift b/JsonDecodeProtectionTests/NumberArrayProtectionTest.swift index 8ad28f0..bc67ec8 100644 --- a/JsonDecodeProtectionTests/NumberArrayProtectionTest.swift +++ b/JsonDecodeProtectionTests/NumberArrayProtectionTest.swift @@ -67,4 +67,26 @@ final class NumberArrayProtectionTest: XCTestCase XCTAssertEqual(actual, expect) } + + func testNumberObjectProtectionWithEmptyString() throws + { + // Arrange + self.jsonString = """ + { + "subObjects": "[{\\"name\\": \\"Jo\\", \\"number\\": 233}, {\\"name\\": \\"Ana\\", \\"number\\": 4565}]", + "dices": "" + } + """ + let jsonData: Data = self.jsonString.data(using: .utf8)! + let jsonDecoder = JSONDecoder() + + // Act + let object = try jsonDecoder.decode(SomeObject.self, from: jsonData) + + // Assert + let actual: Array? = object.dices + let expect: Array? = nil + + XCTAssertEqual(actual, expect) + } } diff --git a/JsonDecodeProtectionTests/ObjectProtectionTest.swift b/JsonDecodeProtectionTests/ObjectProtectionTest.swift index cfa8080..079daab 100644 --- a/JsonDecodeProtectionTests/ObjectProtectionTest.swift +++ b/JsonDecodeProtectionTests/ObjectProtectionTest.swift @@ -21,13 +21,19 @@ struct SomeObject: Decodable extension SomeObject { - struct SubObject: Decodable + struct SubObject: Decodable, Equatable { private(set) var name: String? private(set) var number: Int? + + static + func == (lhs: SomeObject.SubObject, rhs: SomeObject.SubObject) -> Bool + { + lhs.name == rhs.name && lhs.number == rhs.number + } } } @@ -90,4 +96,27 @@ final class ObjectProtectionTest: XCTestCase XCTAssertEqual(actual, expect) } + + func testObjectProtectionWithEmptyString() throws + { + // Arrange + self.jsonString = """ + { + "subObjects": "", + "dices": "[1, 5, 1]" + } + """ + + let jsonData: Data = self.jsonString.data(using: .utf8)! + let jsonDecoder = JSONDecoder() + + // Act + let object = try jsonDecoder.decode(SomeObject.self, from: jsonData) + + // Assert + let actual: Array? = object.subObjects + let expect: Array? = nil + + XCTAssertEqual(actual, expect) + } } diff --git a/README.md b/README.md index a22f684..dc16d65 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ * File > Swift Packages > Add Package Dependency * Add https://github.com/Darktt/JsonProtection -* Select "Up to Next Major" with "1.2.0" +* Select "Up to Next Major" with "1.2.1" ## 功能說明 diff --git a/SourceCode/JsonProtection/NumberArrayProtection.swift b/SourceCode/JsonProtection/NumberArrayProtection.swift index e6edcca..5879123 100644 --- a/SourceCode/JsonProtection/NumberArrayProtection.swift +++ b/SourceCode/JsonProtection/NumberArrayProtection.swift @@ -26,6 +26,27 @@ struct NumberArrayProtection where Element: Decodable, Element: NumberT init() { } } +// MARK: - Private Methods - + +private +extension NumberArrayProtection +{ + private + func decode(with string: String) throws -> Array? + { + guard !string.isEmpty, + let data: Data = string.data(using: .utf8) else { + + return nil + } + + let jsonDecoder = JSONDecoder() + let wrapperValue = try jsonDecoder.decode(NumbersProtection.self, from: data) + + return wrapperValue.wrappedValue + } +} + // MARK: - Conform Protocols - extension NumberArrayProtection: Decodable @@ -46,20 +67,6 @@ extension NumberArrayProtection: Decodable self.wrappedValue = wrappedValue } - - private - func decode(with string: String) throws -> Array? - { - guard let data: Data = string.data(using: .utf8) else { - - return nil - } - - let jsonDecoder = JSONDecoder() - let wrapperValue = try jsonDecoder.decode(NumbersProtection.self, from: data) - - return wrapperValue.wrappedValue - } } extension NumberArrayProtection: CustomStringConvertible diff --git a/SourceCode/JsonProtection/ObjectProtection.swift b/SourceCode/JsonProtection/ObjectProtection.swift index 8c0a5a6..6fdf9de 100644 --- a/SourceCode/JsonProtection/ObjectProtection.swift +++ b/SourceCode/JsonProtection/ObjectProtection.swift @@ -88,7 +88,8 @@ extension ObjectProtection { func decode(with string: String) throws -> Value? { - guard let data: Data = string.data(using: .utf8) else { + guard !string.isEmpty, + let data: Data = string.data(using: .utf8) else { return nil }