JSON5 support
@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:
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:
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.