Skip to content

Commit 1b1458f

Browse files
committed
AllowFragments to be decoded
1 parent f082553 commit 1b1458f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Foundation/NSJSONSerialization.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ public class NSJSONSerialization : NSObject {
126126
else if let (array, _) = try reader.parseArray(0) {
127127
return array
128128
}
129+
else if opt.contains(.AllowFragments), let (value, _) = try reader.parseValue(0) {
130+
return value
131+
}
129132
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo: [
130133
"NSDebugDescription" : "JSON text did not start with array or object and option to allow fragments not set."
131134
])

TestFoundation/TestNSJSONSerialization.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ extension TestNSJSONSerialization {
100100
("test_deserialize_simpleEscapeSequences", test_deserialize_simpleEscapeSequences),
101101
("test_deserialize_unicodeEscapeSequence", test_deserialize_unicodeEscapeSequence),
102102
("test_deserialize_unicodeSurrogatePairEscapeSequence", test_deserialize_unicodeSurrogatePairEscapeSequence),
103+
104+
("test_deserialize_allowFragments", test_deserialize_allowFragments),
103105

104106
("test_deserialize_unterminatedObjectString", test_deserialize_unterminatedObjectString),
105107
("test_deserialize_missingObjectKey", test_deserialize_missingObjectKey),
@@ -297,6 +299,23 @@ extension TestNSJSONSerialization {
297299
}
298300
}
299301

302+
func test_deserialize_allowFragments() {
303+
let subject = "3"
304+
305+
do {
306+
for encoding in supportedEncodings {
307+
guard let data = subject.bridge().dataUsingEncoding(encoding) else {
308+
XCTFail("Unable to convert string to data")
309+
return
310+
}
311+
let result = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? Double
312+
XCTAssertEqual(result, 3)
313+
}
314+
} catch {
315+
XCTFail("Unexpected error: \(error)")
316+
}
317+
}
318+
300319
//MARK: - Parsing Errors
301320
func test_deserialize_unterminatedObjectString() {
302321
let subject = "{\"}"

0 commit comments

Comments
 (0)