You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Foundation/NSJSONSerialization.swift
+178-1Lines changed: 178 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -97,7 +97,26 @@ public class NSJSONSerialization : NSObject {
97
97
/* Generate JSON data from a Foundation object. If the object will not produce valid JSON then an exception will be thrown. Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable. If that option is not set, the most compact possible JSON will be generated. If an error occurs, the error parameter will be set and the return value will be nil. The resulting data is a encoded in UTF-8.
/* Create a Foundation object from JSON data. Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary. Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries. Setting the NSJSONReadingMutableLeaves option will make the parser generate mutable NSString objects. If an error occurs during the parse, then the error parameter will be set and the result will be nil.
throwNSError(domain: NSCocoaErrorDomain, code:NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo:["NSDebugDescription":"Invalid object cannot be serialized"])
259
+
}
260
+
}
261
+
262
+
func serializeString(str:NSString)throws{
263
+
letstr= str.bridge()
264
+
265
+
writer("\"")
266
+
forscalarin str.unicodeScalars {
267
+
switch scalar {
268
+
case"\"":
269
+
writer("\\\"") // U+0022 quotation mark
270
+
case"\\":
271
+
writer("\\\\") // U+005C reverse solidus
272
+
// U+002F solidus not escaped
273
+
case"\u{8}":
274
+
writer("\\b") // U+0008 backspace
275
+
case"\u{c}":
276
+
writer("\\f") // U+000C form feed
277
+
case"\n":
278
+
writer("\\n") // U+000A line feed
279
+
case"\r":
280
+
writer("\\r") // U+000D carriage return
281
+
case"\t":
282
+
writer("\\t") // U+0009 tab
283
+
case"\u{0}"..."\u{f}":
284
+
writer("\\u000\(String(scalar.value, radix:16))") // U+0000 to U+000F
285
+
case"\u{10}"..."\u{1f}":
286
+
writer("\\u00\(String(scalar.value, radix:16))") // U+0010 to U+001F
287
+
default:
288
+
writer(String(scalar))
289
+
}
290
+
}
291
+
writer("\"")
292
+
}
293
+
294
+
func serializeNumber(num:NSNumber)throws{
295
+
if num.doubleValue.isInfinite || num.doubleValue.isNaN {
296
+
throwNSError(domain: NSCocoaErrorDomain, code:NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo:["NSDebugDescription":"Number cannot be infinity or NaN"])
297
+
}
298
+
299
+
// Cannot detect type information (e.g. bool) as there is no objCType property on NSNumber in Swift
throwNSError(domain: NSCocoaErrorDomain, code:NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo:["NSDebugDescription":"NSDictionary key must be NSString"])
0 commit comments