File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -306,7 +306,12 @@ private struct JSONReader {
306
306
}
307
307
308
308
func consumeStructure( ascii: UInt8 , input: Index ) throws -> Index ? {
309
- return try consumeWhitespace ( input) . flatMap ( consumeASCII ( ascii) ) . flatMap ( consumeWhitespace)
309
+ switch ascii {
310
+ case Structure . QuotationMark:
311
+ return try consumeWhitespace ( input) . flatMap ( consumeASCII ( ascii) ) // for strings we don't consume(ignore) the whitespaces after the starting Quotation Mark because they are part of string e.g in "{\"title\" : \" hello world!!\" }" the value should be " hello world!!" instead of "hello world"
312
+ default :
313
+ return try consumeWhitespace ( input) . flatMap ( consumeASCII ( ascii) ) . flatMap ( consumeWhitespace)
314
+ }
310
315
}
311
316
312
317
func consumeASCII( ascii: UInt8 ) -> ( Index ) throws -> Index ? {
Original file line number Diff line number Diff line change @@ -92,6 +92,7 @@ extension TestNSJSONSerialization {
92
92
( " test_deserialize_emptyArray " , test_deserialize_emptyArray) ,
93
93
( " test_deserialize_multiStringArray " , test_deserialize_multiStringArray) ,
94
94
( " test_deserialize_unicodeString " , test_deserialize_unicodeString) ,
95
+ ( " test_deserialize_stringWithSpacesAtStart " , test_deserialize_stringWithSpacesAtStart) ,
95
96
96
97
97
98
( " test_deserialize_values " , test_deserialize_values) ,
@@ -148,6 +149,23 @@ extension TestNSJSONSerialization {
148
149
}
149
150
}
150
151
152
+ func test_deserialize_stringWithSpacesAtStart( ) {
153
+
154
+ let subject = " { \" title \" : \" hello world!! \" } "
155
+ do {
156
+ guard let data = subject. bridge ( ) . dataUsingEncoding ( NSUTF8StringEncoding) else {
157
+ XCTFail ( " Unable to convert string to data " )
158
+ return
159
+ }
160
+ let result = try NSJSONSerialization . JSONObjectWithData ( data, options: [ ] ) as? [ String : Any ]
161
+ XCTAssertEqual ( result ? [ " title " ] as? String , " hello world!! " )
162
+ } catch {
163
+ XCTFail ( " Error thrown: \( error) " )
164
+ }
165
+
166
+
167
+ }
168
+
151
169
//MARK: - Array Deserialization
152
170
func test_deserialize_emptyArray( ) {
153
171
let subject = " [] "
You can’t perform that action at this time.
0 commit comments