title: Using JSONSerialization() to dynamically figure out boolean values
body: <p>I am getting a JSON string from the server (or file).</p>
  
  <p>I want to parse that JSON string and <strong>dynamically</strong> figure out each of the value types.</p>
  
  <p>However, when it comes to boolean values, <code>JSONSerialization</code> just converts the value to <code>0</code> or <code>1</code>, and the code can't differentiate whether "0" is a <code>Double</code>, <code>Int</code>, or <code>Bool</code>.  </p>
  
  <p>I want to recognize whether the value is a <code>Bool</code> without explicitly knowing that a specific key corresponds to a <code>Bool</code> value. What am I doing wrong, or what could I do differently?</p>
  
  <pre><code>// What currently is happening:
  let jsonString = "{\"boolean_key\" : true}"
  let jsonData = jsonString.data(using: .utf8)!
  let json = try! JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as! [String:Any]
  
  json["boolean_key"] is Double // true
  json["boolean_key"] is Int // true
  json["boolean_key"] is Bool // true
  
  // What I would like to happen is below (the issue doesn't happen if I don't use JSONSerialization):
  let customJson: [String:Any] = [
      "boolean_key" : true
  ]
  
  customJson["boolean_key"] is Double // false
  customJson["boolean_key"] is Int // false
  customJson["boolean_key"] is Bool // true
  </code></pre>
  
  <p>Related:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/13615451/how-do-i-get-nsjsonserialization-to-output-a-boolean-as-true-or-false">How do I get NSJSONSerialization to output a boolean as true or false?</a></li>
  <li><a href="https://stackoverflow.com/questions/30215680/is-there-a-correct-way-to-determine-that-an-nsnumber-is-derived-from-a-bool-usin/30223989#30223989">Is there a correct way to determine that an NSNumber is derived from a Bool using Swift?</a></li>
  </ul>