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 encoded data.

    Declaration

    Swift

    static func parse(utf8 data: Data) throws -> JSON
  • Creates an instance of JSON from string.

    Declaration

    Swift

    static func parse(_ string: String) throws -> JSON
  • Creates an instance of JSON from UTF-8 encoded Data. - parameter data: An instance of Data to parse JSON from. - throws: Any JSONParser.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 parse JSON from.

  • Enumeration describing possible errors that occur while parsing a JSON document. Most errors include an associated offset, representing the offset into the UTF-8 characters making up the document where the error occurred.

    See more

    Declaration

    Swift

    public enum Error: Swift.Error