JSONSerialization.ReadingOptions

介绍 ReadingOptions

这三个 case

  • mutableContainers 得到一个可变对象

  • mutableLeaves JSON 的叶子节点如果是 String 类型,会被创建成可变类型的 String

  • fragmentsAllowed 除了 Map 和 Arrary 这两个数据结构可以作为 JSON 顶层,还允许其他类型作为顶层,比如 string,number,null,bool 。

    @available(iOS 5.0, *)
    public struct ReadingOptions : OptionSet {

        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, introduced: 5.0, deprecated: 100000, renamed: "JSONSerialization.ReadingOptions.fragmentsAllowed")
        public static var allowFragments: JSONSerialization.ReadingOptions { get }
    }

验证

一,mutableContainers

验证所有类型后,得到结论,Map 和 Arrary 类型转换后可以得到可变类型,其他类型 比如,单独的 string,单独的 number 都不可以。

1.得到可变对象 __NSDictionaryM

let str = "{\"name\":\"henry\"}"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

// 输出
__NSDictionaryM
{
    name = henry;
}

2.得到不可变对象 __NSSingleEntryDictionaryI

let str = "{\"name\":\"henry\"}"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = []
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

// 输出
__NSSingleEntryDictionaryI
{
    name = henry;
}

二,allowFragments

验证后,得到结论是: bool,number,null,string,map,arrary 都可以做 JSON 的最外层结构。

  1. bool 类型

let str = "true"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
__NSCFBoolean
1
  1. number 类型

let str = "1.2"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
NSDecimalNumber
1.2
  1. string 类型

let str = "\"1.2\""
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
NSTaggedPointerString
1.2
  1. null 类型

let str = "null"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
NSNull
<null>
  1. arrary 类型

let str = "[\"1\"]"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
__NSArrayM
(
    1
)
  1. object 类型

let str = "{\"1\":\"2\"}"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
__NSDictionaryM
{
    1 = 2;
}

三,mutableLeaves

苹果官方文档描述这个 Case: Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString.

翻译过来就是: JSON 的叶子数据,如果是 string 类型,转换过来会被转换成 NSMutableString,但是,我不论是在 swift 还是 oc 都是不行,确实大家也都遇到了。 比如 https://link.jianshu.com/?t=http://stackoverflow.com/questions/19345864/nsjsonreadingmutableleaves-option-is-not-working

做个实验:

let str = "{\"1\":\"2\"}"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments,.mutableLeaves]
let json = try! JSONSerialization.jsonObject(with: data!, options: options) as! [String: Any]
print(type(of: json["1"]!))
print(json)

//输出
NSTaggedPointerString
["1": 2]

确实没有像文档说的那样会创建一个 NSMutableString,不过对我们使用 swift 的童鞋木有啥影响。

最后更新于