JSON Objects and Serialization

In Tutorial 4, Store Search example processes JSON data from iTune using method, JSONSerialization.jsonObject(with:, options:). Two questions:

  1. For the option parameter, the code used []. What does that indicate? Noticed other examples on Ray Wenderlich site used JSONSerialization.ReadingOptions(rawValue: 0). What other options could we use and what are the differences? Searched Apple online help but didn’t find useful info.
  2. Looked into the data returned by JSONSerialization.jjsonObject(with:, options:). For an ebook it has a genres component in the format of genres = ( History, “Arts & Entertainment”, Photography, … ); Is ( ) the Swift’s way noting an array in a dictionary?

Many thanks.

  1. If you search for the documentation on NSJSONSerialization, particularly its nested types NSJSONSerialization.WritingOptions and NSJSONSerialization.ReadingOptions, you’ll discover both of them conform to OptionSet.
    You can think of an option set as a combination value for multiple binary properties. So in the same way a number between 0 and 255 can be represented in binary as eight bits, you can combine eight binary settings into a number between 0 and 255. An option set with a raw value of 0 corresponds to all the binary settings being set to false. In Swift you can write an option set as though it was an array of all the options you want to set to true, and the empty list again corresponds to all the options being false, i.e. a raw value of 0.
  2. I think it’s just how Swift prints arrays to the console. It’s not JSON’s notation, but then JSON stands for JavaScript Object Notation, so it’s not a standard Swift naturally uses. (You’ll also note that “History” lacks its quotation marks because it’s all one word, but “Arts & Entertainment” has them because it has spaces and would be harder to identify as one item.)

Many thank for your response, narrativium!

  1. What option to use in NSJSONSerialization.ReadingOptions() would depend on the options used on the feeding side of the data. Probably most of the time no option would be the case. Just wondering when we would use options other than [ ] to serve a purpose in the context of NSJSONSerialization. Obviously this would start from NSJSONSerialization.WritingOptions().
  2. I agree that the ( ) notation for array in the parsed jsonDictionary is due to the print statement of Swift. It’s not defined in json notation and doesn’t seem injected in JSONSerialization.ReadingOptions(), https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSJSONSerialization.swift

Thanks, Again.