     }
 
     func valueForPathFragment(fragment: JSONPathType, detectNull: Swift.Bool) throws -> JSON {
-        switch (self, fragment) {
-        case let (.Dictionary(dict), key as Swift.String):
-            guard let next = dict[key] else {
-                throw Error.KeyNotFound(key: key)
-            }
-            return next
-        case let (.Array(array), index as Swift.Int):
-            guard array.startIndex.advancedBy(index, limit: array.endIndex) != array.endIndex else {
-                throw Error.IndexOutOfBounds(index: index)
-            }
-            return array[index]
-        case let (.Null, badFragment) where detectNull:
-            throw SubscriptError.SubscriptIntoNull(badFragment)
+        switch self {
+        case .Null where detectNull:
+            throw SubscriptError.SubscriptIntoNull(fragment)
+        case let .Dictionary(dict):
+            return try fragment.valueInDictionary(dict)
+        case let .Array(array):
+            return try fragment.valueInArray(array)
         default:
             throw Error.UnexpectedSubscript(type: fragment.dynamicType)
         }

//  Copyright © 2015 Big Nerd Ranch. All rights reserved.
//

// MARK: Subscripting core

public protocol JSONPathType {}
extension String: JSONPathType {}
extension Int: JSONPathType {}

private extension JSON {


        } catch Error.IndexOutOfBounds where ifNotFound {
            return nil
        } catch Error.KeyNotFound where ifNotFound {
            return nil
        } catch SubscriptError.SubscriptIntoNull(_ as Swift.String) where ifNotFound {
            return nil
