@available(iOS 15.0, OSX 12.0, tvOS 11.0, watchOS 8.0, Xcode 13.0 *)

JSON5 specification has been around for few years now. Finally Apple brings support of this standard to it’s platforms developers. Starting from iOS 15.0 (and other platforms obviously) JSONSerialization and JSONDecoder now support decoding from JSON5.

In order to leverage this functionality using JSONDecoder object, there is a need to explicitly turn it on via allowsJSON5 boolean property. Minimum implementation example can be:

import Foundation

struct GroceryProduct: Codable {
    var name: String
    var points: Int
    var description: String?
}

let json = """
{
    "name": "Durian",
    // JSON5 comment
    "points": +600,
    "description": 'A fruit with a "distinctive" scent.' // pay attention to quotes here
}
""".data(using: .utf8)!

let decoder = JSONDecoder()
decoder.allowsJSON5 = true
let product = try decoder.decode(GroceryProduct.self, from: json)

print(product)

Output is:

GroceryProduct(name: "Durian", points: 600, description: Optional("A fruit with a \"distinctive\" scent."))

But what’s a big deal of enabling this support? For me the most valuable specification of JSON5 is comments:

// This is a single line comment.

/* This is a multi-
   line comment. */

Some other features are: quotes for strings, hexadecimal digits, leading decimal point, positive sign and more. You can already see usage some of them in example below.

Thanks for reading.


Source code

You can find SWIFT5.playground file on GitHub repo.

Questions or proposals? Reach me out at Linkedin