JSONSerialization ReadingOptions
2023-08-27 本文已影响0人
tom__zhu
这两个值通常在序列化和反序列化时被使用,例如Data和JSON互转。先看一下他们各自有哪些选项可用。
extension JSONSerialization {
@available(iOS 5.0, *)
public struct ReadingOptions : OptionSet, @unchecked Sendable {
public init(rawValue: UInt)
public static var mutableContainers: JSONSerialization.ReadingOptions { get }
public static var mutableLeaves: JSONSerialization.ReadingOptions { get }
public static var fragmentsAllowed: JSONSerialization.ReadingOptions { get }
@available(iOS 15.0, *)
public static var json5Allowed: JSONSerialization.ReadingOptions { get }
@available(iOS 15.0, *)
public static var topLevelDictionaryAssumed: JSONSerialization.ReadingOptions { get }
@available(iOS, introduced: 5.0, deprecated: 100000, renamed: "JSONSerialization.ReadingOptions.fragmentsAllowed")
public static var allowFragments: JSONSerialization.ReadingOptions { get }
}
@available(iOS 5.0, *)
public struct WritingOptions : OptionSet, @unchecked Sendable {
public init(rawValue: UInt)
public static var prettyPrinted: JSONSerialization.WritingOptions { get }
/* Sorts dictionary keys for output using [NSLocale systemLocale]. Keys are compared using NSNumericSearch. The specific sorting method used is subject to change.
*/
@available(iOS 11.0, *)
public static var sortedKeys: JSONSerialization.WritingOptions { get }
public static var fragmentsAllowed: JSONSerialization.WritingOptions { get }
@available(iOS 13.0, *)
public static var withoutEscapingSlashes: JSONSerialization.WritingOptions { get }
}
}
上面是Xcode接口文档的描述。
ReadingOptions
使用场景
ReadingOptions的作用是用来反序列化Data时的参数
示例
let object: Any = try JSONSerialization.jsonObject(with: data, options: opt)
解释
- mutableContainers:得到一个可变的容器对象(Array, Dictionary)
- mutableLeaves:得到一个可变的叶子节点(JSON叶节点如果是String,按理会得到可变的String)
- fragmentsAllowed:除了Array,Dictionary以外,允许其他类型作为顶层,例如String、Number、Null、BooL(常用)
- json5Allowed:支持JSON5的语法#
- topLevelDictionaryAssumed:允许最外层没有括号包裹的JSON数据的解析
- allowFragments:会被废弃,建议fragmentsAllowed
WritingOptions
使用场景
WritingOptions的作用是用来序列化JSON时的参数
示例
let jsonObjc = JSONSerialization.data(withJSONObject: json, options: [])
解释
- prettyPrinted : 转换的JSON对象会通过换行和缩进,使JSON美观易读
- sortedKeys : 转换后的JSON对象会按照Key升序排序(内外层都排序)
- fragmentsAllowed : 允许除了Array、Dictionary的对象作为最外层
- withoutEscapingSlashes : 去除转换后JSON中的转移字符"/"
举例
- prettyPrinted
let json: [String: Any] = ["name": "tom", "address": ["province":"sh", "city":"sh"], "c":["c":"123","a":"321"]]
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted])
let jsonString = String.init(data: jsonData, encoding: String.Encoding.utf8)
print(jsonString!)
} catch { }
输出:
{
"address" : {
"province" : "sh",
"city" : "sh"
},
"name" : "tom",
"c" : {
"c" : "123",
"a" : "321"
}
}
- sortedKeys
内外层按照Key排序
let json: [String: Any] = ["name": "tom", "address": ["province":"sh", "city":"sh"], "c":["c":"123","a":"321"]]
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options: [.sortedKeys,.prettyPrinted])
let jsonString = String.init(data: jsonData, encoding: String.Encoding.utf8)
print(jsonString!)
} catch { }
输出:
{
"address" : {
"city" : "sh",
"province" : "sh"
},
"c" : {
"a" : "321",
"c" : "123"
},
"name" : "tom"
}
- fragmentsAllowed
JSON最外层可以使用除Array、Dictionary的类型。 例如 Number、String、Bool、Null。否则默认只允许Dictionary、Array作为JSON最外层
let json = 123456
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options: [.sortedKeys,.prettyPrinted])
let jsonString = String.init(data: jsonData, encoding: String.Encoding.utf8)
print(jsonString!)
} catch { }
输出:
123456
- withoutEscapingSlashes
struct User: Codable {
let name: String
let age: String
}
let user = User(username: "Tom", profileURL: "http://iqiyi.com")
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .withoutEscapingSlashes
let json = try? jsonEncoder.encode(user)
if let data = json, let str = String(data: data, encoding: .utf8) {
print(str)
}
输出:
{"profileURL":"http://iqiyi.com","username":"Tom"} // 这里没有转义字符