 
     public var valueReader: ValueReader {
         switch type {
-        case RawValueType.integer:
+        case RawType.integer:
             // This syntax to read the bottom bits of a UInt as an Int32 is insane.
             return payload.withMemoryRebound(to: Int32.self, capacity: 1) { p in
                 return .integer(p[0])
             }
-        case RawValueType.double:
+        case RawType.double:
             let lo = UInt64(payload[0])
             let hi = UInt64(payload[1])
             let bitPattern = lo | (hi << 32)
             return .double(Float64(bitPattern: bitPattern))
-        case RawValueType.null:
+        case RawType.null:
             return .null
-        case RawValueType.bfalse:
+        case RawType.bfalse:
             return .bool(false)
-        case RawValueType.btrue:
+        case RawType.btrue:
             return .bool(true)
-        case RawValueType.string:
+        case RawType.string:
             let start = Int(payload[0])
             let end = Int(payload[1])
             // TODO: are the following two lines a single copy?
             let data = Data(input[start ..< end])
             return .string(String(data: data, encoding: .utf8)!)
-        case RawValueType.array:
+        case RawType.array:
             return .array(ArrayReader(payload: payload, input: input))
-        case RawValueType.object:
+        case RawType.object:
             return .object(ObjectReader(payload: payload, input: input))
         default:
             fatalError("Unknown sajson value type - memory corruption detected?")

import Foundation

// Keep this in sync with sajson::type
private struct RawValueType {
    static let integer: UInt8 = 0
    static let double: UInt8 = 1
    static let null: UInt8 = 2
    static let bfalse: UInt8 = 3
    static let btrue: UInt8 = 4
    static let string: UInt8 = 5
    static let array: UInt8 = 6
    static let object: UInt8 = 7
}

/// Represents a JSON value decoded from the sajson AST.  This type provides decoded access
/// to array and object elements lazily.

        return result
    }

    // MARK: Sequence

    public struct Iterator: IteratorProtocol {
        fileprivate init(objectReader: ObjectReader) {
            self.objectReader = objectReader
        }

        public mutating func next() -> (String, ValueReader)? {
            if currentIndex < objectReader.count {
                let value = objectReader[currentIndex]
                currentIndex += 1
                return value
            } else {
                return nil
            }
        }

        private var currentIndex = 0
        private let objectReader: ObjectReader
    }
    
    public func makeIterator() -> ObjectReader.Iterator {
        return Iterator(objectReader: self)
    }

    // MARK: Private

    private let payload: UnsafePointer<UInt>
    private let input: UnsafeBufferPointer<UInt8>
}

// MARK: Value

/// Represents a fully-decoded JSON parse tree.  This API is provided for convenience, but for
/// optimal performance, consider using `ValueReader` instead.
public enum Value {
    case integer(Int32)
    case double(Float64)
    case null
    case bool(Bool)
    case string(String)
    case array([Value])
    case object([String: Value])
}

// Internal type that represents a decodable sajson AST node.
private struct ASTNode {
    // Keep this in sync with sajson::type
    private struct RawType {
        static let integer: UInt8 = 0
        static let double: UInt8 = 1
        static let null: UInt8 = 2
        static let bfalse: UInt8 = 3
        static let btrue: UInt8 = 4
        static let string: UInt8 = 5
        static let array: UInt8 = 6
        static let object: UInt8 = 7
    }
    
    fileprivate init(type: UInt8, payload: UnsafePointer<UInt>, input: UnsafeBufferPointer<UInt8>) {
        self.type = type
        self.payload = payload
