title: Swifts interpretation of JSON
body: <p>I am writing a basic JSON parser, facing this strange issue where json loaded from file is different from the one that is hard coded.</p>
  
  <p>This is the content of json file that I am using</p>
  
  <pre><code>{
      "details": {
          "date": "2019-02-08T11:08:38Z",
          "busId": 4,
          "end_date": {
              "date": "2019-02-13T18:30:00Z",
              "flex": 0,
              "timezone": "Asia/Calcutta",
              "hasTime": false,
              "userDate": "2019-02-14T00:00:00Z"
          }
      }
  }
  </code></pre>
  
  <p>and the code to load the json in Swift is</p>
  
  <pre><code>func jsonFromFile(_ name: String) -&gt; [String : Any] {
      let path = Bundle.main.path(forResource: name, ofType: "json")!
      let data = try! Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
      let jsonObj = try! JSONSerialization.jsonObject(with: data, options: []) as! [String : Any]
      return jsonObj
  }
  </code></pre>
  
  <hr>
  
  <p>This is how I created a literal JSON in Swift</p>
  
  <pre><code>let data: [String : Any] = [
                  "details": [
                      "date": "2019-02-08T11:08:38Z",
                      "busId": 4,
                      "end_date": [
                          "date": "2019-02-13T18:30:00Z",
                          "flex": 0,
                          "timezone": "Asia/Calcutta",
                          "hasTime": false,
                          "userDate": "2019-02-14T00:00:00Z"
                      ]
                  ]
          ]
  </code></pre>
  
  <hr>
  
  <p>I was facing a strange issue while parsing it. After digging I found out that the internal representation of Dictionary created from JSON in file is different from the JSON created from dictionary literal.
   Here is the debug output when loaded from file
  <a href="https://i.stack.imgur.com/bQjbN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bQjbN.png" alt="This is how the dictionary looks when loaded from file"></a></p>
  
  <p>and when created from literal Swift dictionary</p>
  
  <p><a href="https://i.stack.imgur.com/6Do59.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6Do59.png" alt="From literal dictionary"></a></p>
  
  <hr>
  
  <p>Although they are of type [String : Any] they have different internal representation(note the curly brackets in first image).
  One of the problem I think could be <code>JSONSerialization.jsonObject</code> would be returning an object of type NSDictionary instead of <code>[String : Any]</code>; although they are bridged but they could have different internal implementation.</p>
  
  <p>So,
  How do I make sure that I get a same representation of JSON in <code>[String : Any]</code> irrespective of where it is loaded from.</p>
  
  <hr>
  
  <p>Update:
  I tried using <a href="https://developer.apple.com/documentation/swift/2885064-type" rel="nofollow noreferrer">type(of:)</a> in debugger. Type of first case is <code>__NSDictionaryI</code> and other one is <code>Swift.Dictionary&lt;Swift.String, Any&gt;</code>, so clearly the internal type is different. Do I straight away look for solutions that would let me convert NSDictionary to Swift.Dictionary? </p>
  
  <p>Note: The reason behind asking this question is, although they behave same in most cases, I am facing some issue while converting the variable to protocol type. It works fine for Swift.Dictionary, but the code breaks for NSDictionary(i.e. object loaded from JSON file)
  Here is the protocol</p>
  
  <pre><code>protocol Countable {
      var count: Int { get }
  }
  
  extension Array: Countable where Element: Any {
  }
  
  extension Dictionary: Countable where Key == String, Value == Any {
  }
  </code></pre>
  
  <p>So for the same variable when I write <code>if var1 is Countable</code> it returns true for first case and false for second case. Although it works fine if I write separate type check for [Any] and [String: Any]</p>