JSONParser
public struct JSONParser
A pure Swift JSON parser. This parser is much faster than the
NSJSONSerialization
-based parser (due to the overhead of having to
dynamically cast the Objective-C objects to determine their type); however,
it is much newer and has restrictions that the NSJSONSerialization
parser
does not. Two restrictions in particular are that it requires UTF-8 data as
input and it does not allow trailing commas in arrays or dictionaries.
-
Decode the root element of the
JSON
stream. This may be any fragment or a structural element, per RFC 7159.The beginning bytes are used to determine the stream’s encoding.
JSONParser
currently only supports UTF-8 encoding, with or without a byte-order mark.Throws
JSONParser.Error
for any decoding failures, including a source location if needed.Declaration
Swift
public mutating func parse() throws -> JSON
-
Creates an instance of
JSON
from UTF-8 encodedData
. - parameter data: An instance ofData
to parseJSON
from. - throws: AnyJSONParser.Error
that arises during decoding. - seealso: JSONParser.parse()Declaration
Swift
public static func createJSON(from data: Data) throws -> JSON
Parameters
data
An instance of
Data
to parseJSON
from.