diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 1fca5da..6a6627a 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -8,8 +8,8 @@ PODS: - FBSnapshotTestCase/SwiftSupport (2.1.4): - FBSnapshotTestCase/Core - HandyJSON (5.0.0-beta.1) - - SmartCodable/Core (5.1.2) - - SmartCodable/Inherit (5.1.2): + - SmartCodable/Core (5.1.3) + - SmartCodable/Inherit (5.1.3): - SmartCodable/Core - SnapKit (5.6.0) @@ -41,7 +41,7 @@ SPEC CHECKSUMS: CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980 FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978 - SmartCodable: 275755f95fd7019ce6b626f75517af5f2cf55bc7 + SmartCodable: 45aa200d57f9f88eb48c0d2dcb555553ae9f8d44 SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25 PODFILE CHECKSUM: fbd909bd1bf3747400cd705163fad38954f6a18f diff --git a/Example/SmartCodable.xcodeproj/project.pbxproj b/Example/SmartCodable.xcodeproj/project.pbxproj index bdb5f0f..e00e9b0 100644 --- a/Example/SmartCodable.xcodeproj/project.pbxproj +++ b/Example/SmartCodable.xcodeproj/project.pbxproj @@ -142,6 +142,7 @@ 99F9E8A62BB5139900D68037 /* MixDecodingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99F9E8A52BB5139900D68037 /* MixDecodingViewController.swift */; }; 99FA1D502DF06B7D00560A05 /* DecodingLogViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99FA1D4F2DF06B7D00560A05 /* DecodingLogViewController.swift */; }; AF374D4C62AA92DE0D9ED2B7 /* Pods_SmartCodable_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5EB62463C4F6AEFEAA2A5E1E /* Pods_SmartCodable_Tests.framework */; }; + DA0F36412E9BA4A10049EB49 /* MultilineStringTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA0F36402E9BA4A10049EB49 /* MultilineStringTest.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -292,6 +293,7 @@ AD8215B5E8E50F277C1C4356 /* Pods-SmartCodable_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SmartCodable_Example.release.xcconfig"; path = "Target Support Files/Pods-SmartCodable_Example/Pods-SmartCodable_Example.release.xcconfig"; sourceTree = ""; }; AFEADC35BC2373F1DC69B73B /* Pods-SmartCodable_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SmartCodable_Tests.release.xcconfig"; path = "Target Support Files/Pods-SmartCodable_Tests/Pods-SmartCodable_Tests.release.xcconfig"; sourceTree = ""; }; BC656B63C9031B22DEB5C8EB /* Pods-SmartCodable_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SmartCodable_Example.debug.xcconfig"; path = "Target Support Files/Pods-SmartCodable_Example/Pods-SmartCodable_Example.debug.xcconfig"; sourceTree = ""; }; + DA0F36402E9BA4A10049EB49 /* MultilineStringTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultilineStringTest.swift; sourceTree = ""; }; DB7246035010143E03C7616B /* Pods-SmartCodable_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SmartCodable_Tests.debug.xcconfig"; path = "Target Support Files/Pods-SmartCodable_Tests/Pods-SmartCodable_Tests.debug.xcconfig"; sourceTree = ""; }; EBE2539D7D4ED401A0AF07CA /* Pods_SmartCodable_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SmartCodable_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; }; F415A4253833C1D4B5A35964 /* SmartCodable.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = SmartCodable.podspec; path = ../SmartCodable.podspec; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.ruby; }; @@ -382,6 +384,7 @@ 607FACE81AFB9204008FA782 /* Tests */ = { isa = PBXGroup; children = ( + DA0F36402E9BA4A10049EB49 /* MultilineStringTest.swift */, 99D17BB82B9F0CF9008ADAAB /* PerformanceTest.swift */, 99AE7D892B3EC02E00B474E5 /* Resource */, 607FACEB1AFB9204008FA782 /* Tests.swift */, @@ -1132,6 +1135,7 @@ 99D17BBA2B9F0CF9008ADAAB /* PerformanceTest.swift in Sources */, 99328D972B5138170074036A /* AreaTest.swift in Sources */, 607FACEC1AFB9204008FA782 /* Tests.swift in Sources */, + DA0F36412E9BA4A10049EB49 /* MultilineStringTest.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Example/Tests/MultilineStringTest.swift b/Example/Tests/MultilineStringTest.swift new file mode 100644 index 0000000..f625dd9 --- /dev/null +++ b/Example/Tests/MultilineStringTest.swift @@ -0,0 +1,54 @@ +// +// MultilineStringTest.swift +// SmartCodable +// +// Created by Zero.D.Saber on 2025/10/11. +// Copyright © 2025 CocoaPods. All rights reserved. +// + +import SmartCodable +import Testing + +private struct User: SmartCodable { + var name: String = "" + var age: Int = 0 +} + +struct MultilineStringTest { + @Test(.bug("https://github.com/iAmMccc/SmartCodable/issues/116")) + func decodeMultilingDictStr() { + let jsonString1 = """ + { + "name": "Sam", + "age": 22 + } + """ + let user1 = User.deserialize(from: jsonString1) + #expect(user1?.name == "Sam") + #expect(user1?.age == 22) + + + let jsonString2 = """ + "name": "Zero", + "age": 100 + + """ + let user2 = User.deserialize(from: jsonString2) + #expect(user2 == nil) + } + + @Test("array case") + func decodeMultilingArrayStr() { + let jsonString = """ + [ + { + "name": "Zero", + "age": "99" + } + ] + """ + let users = [User].deserialize(from: jsonString) + #expect(users?.first?.name == "Zero") + #expect(users?.first?.age == 99) + } +} diff --git a/Sources/SmartCodable/Core/JSONDecoder/Decoder/KeysMapper.swift b/Sources/SmartCodable/Core/JSONDecoder/Decoder/KeysMapper.swift index 41ca3cd..26f4f18 100644 --- a/Sources/SmartCodable/Core/JSONDecoder/Decoder/KeysMapper.swift +++ b/Sources/SmartCodable/Core/JSONDecoder/Decoder/KeysMapper.swift @@ -127,7 +127,6 @@ extension Dictionary { extension String { func toJSONObject() -> Any? { - guard starts(with: "{") || starts(with: "[") else { return nil } return data(using: .utf8).flatMap { try? JSONSerialization.jsonObject(with: $0) } } }