0001    //
0002    //  Decoder.swift
0003    //  PMJSON
0004    //
0005    //  Created by Kevin Ballard on 10/8/15.
0006    //  Copyright © 2016 Postmates.
0007    //
0008    //  Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
0009    //  http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
0010    //  <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
0011    //  option. This file may not be copied, modified, or distributed
0012    //  except according to those terms.
0013    //
0014    
0015    extension JSON {
0016        /// Decodes a string as JSON.
0017        /// - parameters:
0018        ///   - strict: If `true`, trailing commas in arrays/objects are treated as an error.
0019        /// - throws: `JSONParserError`
0020        public static func decode(string: Swift.String, strict: Swift.Bool = false) throws -> JSON {
0021            var parser = JSONParser(string.unicodeScalars)
0022            parser.strict = strict
0023            var decoder = JSONDecoder(parser)
0024            return try decoder.decode()
0025        }
0026        
0027        /// Decodes a sequence of `UnicodeScalar`s as JSON.
0028        /// - parameters:
0029        ///   - strict: If `true`, trailing commas in arrays/objects are treated as an error.
0030        /// - throws: `JSONParserError`
0031        public static func decode
ObjectiveC.swift:26
            return try JSON.decode(UTF8Decoder(data: data), strict: strict)
<Seq: SequenceType where Seq.Generator.Element == UnicodeScalar>(scalars: Seq, strict: Swift.Bool = false) throws -> JSON { 0032 var parser = JSONParser(scalars) 0033 parser.strict = strict 0034 var decoder = JSONDecoder(parser) 0035 return try decoder.decode() 0036 } 0037 } 0038 0039 /// A JSON decoder. 0040 private struct JSONDecoder
Decoder.swift:23
        var decoder = JSONDecoder(parser)
Decoder.swift:34
        var decoder = JSONDecoder(parser)
<Seq
Decoder.swift:41
    init(_ parser: Seq) {
Decoder.swift:113
    private var gen: Seq.Generator
: SequenceType where Seq.Generator: JSONEventGenerator, Seq.Generator.Element == JSONEvent> { 0041 init
Decoder.swift:23
        var decoder = JSONDecoder(parser)
Decoder.swift:34
        var decoder = JSONDecoder(parser)
(_ parser: Seq) { 0042 gen = parser.generate() 0043 } 0044 0045 mutating func decode
Decoder.swift:24
        return try decoder.decode()
Decoder.swift:35
        return try decoder.decode()
() throws -> JSON { 0046 bump() 0047 let result = try buildValue() 0048 bump() 0049 switch token { 0050 case .None: break 0051 case .Some(.Error(let err)): throw err 0052 case .Some(let token): fatalError("unexpected token: \(token)") 0053 } 0054 return result 0055 } 0056 0057 private mutating func bump
Decoder.swift:46
        bump()
Decoder.swift:48
        bump()
Decoder.swift:78
        bump()
Decoder.swift:89
            bump()
Decoder.swift:91
            bump()
Decoder.swift:97
        bump()
Decoder.swift:104
            bump()
() { 0058 token = gen.next() 0059 } 0060 0061 private mutating func buildValue
Decoder.swift:47
        let result = try buildValue()
Decoder.swift:90
            dict[key] = try buildValue()
Decoder.swift:103
            ary.append(try buildValue())
() throws -> JSON { 0062 switch token { 0063 case .ObjectStart?: return try buildObject() 0064 case .ObjectEnd?: throw error(.InvalidSyntax) 0065 case .ArrayStart?: return try buildArray() 0066 case .ArrayEnd?: throw error(.InvalidSyntax) 0067 case .BooleanValue(let b)?: return .Bool(b) 0068 case .Int64Value(let i)?: return .Int64(i) 0069 case .DoubleValue(let d)?: return .Double(d) 0070 case .StringValue(let s)?: return .String(s) 0071 case .NullValue?: return .Null 0072 case .Error(let err)?: throw err 0073 case nil: throw error(.UnexpectedEOF) 0074 } 0075 } 0076 0077 private mutating func buildObject
Decoder.swift:63
        case .ObjectStart?: return try buildObject()
() throws -> JSON { 0078 bump() 0079 var dict: [String: JSON] = Dictionary(minimumCapacity: objectHighWaterMark) 0080 defer { objectHighWaterMark = max(objectHighWaterMark, dict.count) } 0081 while let token = self.token { 0082 let key: String 0083 switch token { 0084 case .ObjectEnd: return .Object(JSONObject(dict)) 0085 case .Error(let err): throw err 0086 case .StringValue(let s): key = s 0087 default: throw error(.NonStringKey) 0088 } 0089 bump() 0090 dict[key] = try buildValue() 0091 bump() 0092 } 0093 throw error(.UnexpectedEOF) 0094 } 0095 0096 private mutating func buildArray
Decoder.swift:65
        case .ArrayStart?: return try buildArray()
() throws -> JSON { 0097 bump() 0098 var ary: JSONArray = [] 0099 while let token = self.token { 0100 if case .ArrayEnd = token { 0101 return .Array(ary) 0102 } 0103 ary.append(try buildValue()) 0104 bump() 0105 } 0106 throw error(.UnexpectedEOF) 0107 } 0108 0109 private func error
Decoder.swift:64
        case .ObjectEnd?: throw error(.InvalidSyntax)
Decoder.swift:66
        case .ArrayEnd?: throw error(.InvalidSyntax)
Decoder.swift:73
        case nil: throw error(.UnexpectedEOF)
Decoder.swift:87
            default: throw error(.NonStringKey)
Decoder.swift:93
        throw error(.UnexpectedEOF)
Decoder.swift:106
        throw error(.UnexpectedEOF)
(code: JSONParserError.Code) -> JSONParserError { 0110 return JSONParserError(code: code, line: gen.line, column: gen.column) 0111 } 0112 0113 private var gen
Decoder.swift:42
        gen = parser.generate()
Decoder.swift:58
        token = gen.next()
Decoder.swift:110
        return JSONParserError(code: code, line: gen.line, column: gen.column)
Decoder.swift:110
        return JSONParserError(code: code, line: gen.line, column: gen.column)
: Seq.Generator 0114 private var token
Decoder.swift:49
        switch token {
Decoder.swift:58
        token = gen.next()
Decoder.swift:62
        switch token {
Decoder.swift:81
        while let token = self.token {
Decoder.swift:99
        while let token = self.token {
: JSONEvent? 0115 private var objectHighWaterMark
Decoder.swift:79
        var dict: [String: JSON] = Dictionary(minimumCapacity: objectHighWaterMark)
Decoder.swift:80
        defer { objectHighWaterMark = max(objectHighWaterMark, dict.count) }
Decoder.swift:80
        defer { objectHighWaterMark = max(objectHighWaterMark, dict.count) }
: Int = 0 0116 } 0117