Skip to content

Commit 3b9084c

Browse files
authored
Merge pull request #119 from faimin/main
Fix #116, json string misjudgment
2 parents f2b7cc0 + 5cc6930 commit 3b9084c

4 files changed

Lines changed: 61 additions & 4 deletions

File tree

Example/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ PODS:
88
- FBSnapshotTestCase/SwiftSupport (2.1.4):
99
- FBSnapshotTestCase/Core
1010
- HandyJSON (5.0.0-beta.1)
11-
- SmartCodable/Core (5.1.2)
12-
- SmartCodable/Inherit (5.1.2):
11+
- SmartCodable/Core (5.1.3)
12+
- SmartCodable/Inherit (5.1.3):
1313
- SmartCodable/Core
1414
- SnapKit (5.6.0)
1515

@@ -41,7 +41,7 @@ SPEC CHECKSUMS:
4141
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
4242
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
4343
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
44-
SmartCodable: 275755f95fd7019ce6b626f75517af5f2cf55bc7
44+
SmartCodable: 45aa200d57f9f88eb48c0d2dcb555553ae9f8d44
4545
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25
4646

4747
PODFILE CHECKSUM: fbd909bd1bf3747400cd705163fad38954f6a18f

Example/SmartCodable.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
99F9E8A62BB5139900D68037 /* MixDecodingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99F9E8A52BB5139900D68037 /* MixDecodingViewController.swift */; };
143143
99FA1D502DF06B7D00560A05 /* DecodingLogViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99FA1D4F2DF06B7D00560A05 /* DecodingLogViewController.swift */; };
144144
AF374D4C62AA92DE0D9ED2B7 /* Pods_SmartCodable_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5EB62463C4F6AEFEAA2A5E1E /* Pods_SmartCodable_Tests.framework */; };
145+
DA0F36412E9BA4A10049EB49 /* MultilineStringTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA0F36402E9BA4A10049EB49 /* MultilineStringTest.swift */; };
145146
/* End PBXBuildFile section */
146147

147148
/* Begin PBXContainerItemProxy section */
@@ -292,6 +293,7 @@
292293
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 = "<group>"; };
293294
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 = "<group>"; };
294295
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 = "<group>"; };
296+
DA0F36402E9BA4A10049EB49 /* MultilineStringTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultilineStringTest.swift; sourceTree = "<group>"; };
295297
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 = "<group>"; };
296298
EBE2539D7D4ED401A0AF07CA /* Pods_SmartCodable_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SmartCodable_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
297299
F415A4253833C1D4B5A35964 /* SmartCodable.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = SmartCodable.podspec; path = ../SmartCodable.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
@@ -382,6 +384,7 @@
382384
607FACE81AFB9204008FA782 /* Tests */ = {
383385
isa = PBXGroup;
384386
children = (
387+
DA0F36402E9BA4A10049EB49 /* MultilineStringTest.swift */,
385388
99D17BB82B9F0CF9008ADAAB /* PerformanceTest.swift */,
386389
99AE7D892B3EC02E00B474E5 /* Resource */,
387390
607FACEB1AFB9204008FA782 /* Tests.swift */,
@@ -1132,6 +1135,7 @@
11321135
99D17BBA2B9F0CF9008ADAAB /* PerformanceTest.swift in Sources */,
11331136
99328D972B5138170074036A /* AreaTest.swift in Sources */,
11341137
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */,
1138+
DA0F36412E9BA4A10049EB49 /* MultilineStringTest.swift in Sources */,
11351139
);
11361140
runOnlyForDeploymentPostprocessing = 0;
11371141
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// MultilineStringTest.swift
3+
// SmartCodable
4+
//
5+
// Created by Zero.D.Saber on 2025/10/11.
6+
// Copyright © 2025 CocoaPods. All rights reserved.
7+
//
8+
9+
import SmartCodable
10+
import Testing
11+
12+
private struct User: SmartCodable {
13+
var name: String = ""
14+
var age: Int = 0
15+
}
16+
17+
struct MultilineStringTest {
18+
@Test(.bug("https://github.com/iAmMccc/SmartCodable/issues/116"))
19+
func decodeMultilingDictStr() {
20+
let jsonString1 = """
21+
{
22+
"name": "Sam",
23+
"age": 22
24+
}
25+
"""
26+
let user1 = User.deserialize(from: jsonString1)
27+
#expect(user1?.name == "Sam")
28+
#expect(user1?.age == 22)
29+
30+
31+
let jsonString2 = """
32+
"name": "Zero",
33+
"age": 100
34+
35+
"""
36+
let user2 = User.deserialize(from: jsonString2)
37+
#expect(user2 == nil)
38+
}
39+
40+
@Test("array case")
41+
func decodeMultilingArrayStr() {
42+
let jsonString = """
43+
[
44+
{
45+
"name": "Zero",
46+
"age": "99"
47+
}
48+
]
49+
"""
50+
let users = [User].deserialize(from: jsonString)
51+
#expect(users?.first?.name == "Zero")
52+
#expect(users?.first?.age == 99)
53+
}
54+
}

Sources/SmartCodable/Core/JSONDecoder/Decoder/KeysMapper.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ extension Dictionary {
127127

128128
extension String {
129129
func toJSONObject() -> Any? {
130-
guard starts(with: "{") || starts(with: "[") else { return nil }
131130
return data(using: .utf8).flatMap { try? JSONSerialization.jsonObject(with: $0) }
132131
}
133132
}

0 commit comments

Comments
 (0)