     }
 }
 
-//MARK: - Deserialization
+//MARK: - Encoding Detection
+
 internal extension NSJSONSerialization {
     
-    static func JSONObjectWithString(string: String) throws -> Any {
-        let parser = JSONDeserializer.UnicodeParser(viewSkippingBOM: string.unicodeScalars)
-        if let (object, _) = try JSONDeserializer.parseObject(parser) {
-            return object
-        }
-        else if let (array, _) = try JSONDeserializer.parseArray(parser) {
-            return array
-        }
-        throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo: [
-            "NSDebugDescription" : "JSON text did not start with array or object and option to allow fragments not set."
-        ])
-    }
-}
-
-//MARK: - Encoding Detection
-
-internal extension NSJSONSerialization {
-    
     /// Detect the encoding format of the NSData contents
-    class func detectEncoding(data: NSData) -> NSStringEncoding {
-        let bytes = UnsafePointer<UInt8>(data.bytes)
-        let length = data.length
-        if let encoding = parseBOM(bytes, length: length) {
-            return encoding
-        }
-        
+    class func detectEncoding(bytes: UnsafePointer<UInt8>, _ length: Int) -> NSStringEncoding {
+
         if length >= 4 {
             switch (bytes[0], bytes[1], bytes[2], bytes[3]) {
             case (0, 0, 0, _):

// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//


public struct NSJSONReadingOptions : OptionSetType {
    public let rawValue : UInt

    /// - Experiment: Note that the return type of this function is different than on Darwin Foundation (Any instead of AnyObject). This is likely to change once we have a more complete story for bridging in place.
    public class func JSONObjectWithData(data: NSData, options opt: NSJSONReadingOptions) throws -> Any {
        
        let bytes = UnsafePointer<UInt8>(data.bytes)
        let encoding: NSStringEncoding
        let buffer: UnsafeBufferPointer<UInt8>
        if let detected = parseBOM(bytes, length: data.length) {
            encoding = detected.encoding
            buffer = UnsafeBufferPointer(start: bytes.advancedBy(detected.skipLength), count: data.length - detected.skipLength)
        }
        else {
            encoding = detectEncoding(bytes, data.length)
            buffer = UnsafeBufferPointer(start: bytes, count: data.length)
        }

        let source = JSONReader.UnicodeSource(buffer: buffer, encoding: encoding)
        let reader = JSONReader(source: source)
        if let (object, _) = try reader.parseObject(0) {
            return object
        }
        else if let (array, _) = try reader.parseArray(0) {
            return array
        }
        throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo: [
            "NSDebugDescription" : "JSON text did not start with array or object and option to allow fragments not set."
        ])
    }
    
    /* Write JSON data into a stream. The stream should be opened and configured. The return value is the number of bytes written to the stream, or 0 on error. All other behavior of this method is the same as the dataWithJSONObject:options:error: method.
