<commit_before>//
//  Dictionary+JSONKey.swift
//  JSONUtilities
//
//  Created by <NAME> on 05/03/2016.
//  Copyright © 2016 TechBrewers LTD. All rights reserved.
//

import Foundation

public protocol JSONRawType {}
extension Int : JSONRawType {}
extension Double : JSONRawType {}
extension Float : JSONRawType {}
extension String : JSONRawType {}
extension Bool : JSONRawType {}

// Simple protocol used to extend a JSONDictionary
public protocol StringProtocol {}
extension String: StringProtocol {}

extension Dictionary where Key: StringProtocol {
  
  // MARK: JSONRawType type
  
  /// Decode a mandatory JSON raw type
  public func jsonKey<ReturnType : JSONRawType>(key: Key) throws -> ReturnType {
    
    guard let value = self[key] as? ReturnType else {
      throw DecodingError.MandatoryKeyNotFound(key: key)
    }
    return value
  }
  
  /// Decode an optional JSON raw type
  public func jsonKey<ReturnType : JSONRawType>(key: Key) -> ReturnType? {
    return self[key] as? ReturnType
  }
  
  // MARK: [JSONRawType] type
  
  /// Decode an Array of mandatory JSON raw types
  public func jsonKey<ReturnType>(key: Key) throws -> [ReturnType] {
    guard let value = self[key] as? [ReturnType] else {
      throw DecodingError.MandatoryKeyNotFound(key: key)
    }
    return value
  }
  
  /// Decode an Array of optional JSON raw types
  public func jsonKey<ReturnType>(key: Key) -> [ReturnType]? {
    return self[key] as? [ReturnType]
  }
  
  // MARK: [String: AnyObject] type
  
  /// Decodes as a raw Dictionary with a mandatory key
  public func jsonKey(key: Key) throws -> JSONDictionary {
    
    guard let value = self[key] as? JSONDictionary else {
      throw DecodingError.MandatoryKeyNotFound(key: key)
    }
    return value
  }

  /// Decodes as a raw dictionary with an optional key
  public func jsonKey(key: Key) -> JSONDictionary? {
    return self[key] as? JSONDictionary
  }
  
  // MARK: [[String: AnyObject]] type
  
  /// Decodes as a raw dictionary array with a mandatory key
  public func jsonKey(key: Key) throws -> [JSONDictionary] {
    
    guard let value = self[key] as? [JSONDictionary] else {
      throw DecodingError.MandatoryKeyNotFound(key: key)
    }
    return value
  }
  
  /// Decodes as a raw ictionary array with an optional key
  public func jsonKey(key: Key) -> [JSONDictionary]? {
    return self[key] as? [JSONDictionary]
  }
  
  // MARK: Decodable types
  
  /// Decode a mandatory Decodable object
  public func jsonKey<ReturnType : Decodable>(key: Key) throws -> ReturnType {
    return try ReturnType(jsonDictionary: JSONDictionaryForKey(key))
  }
  
  /// Decode an optional Decodable object
  public func jsonKey<ReturnType : Decodable>(key: Key) -> ReturnType? {
    return try? ReturnType(jsonDictionary: JSONDictionaryForKey(key))
  }
  
  // MARK: [Decodable] types
  
  /// Decode an Array of mandatory Decodable objects
  public func jsonKey<ReturnType : Decodable>(key: Key) throws -> [ReturnType] {
    return decodableObjectsArray(try JSONArrayForKey(key))
  }
  <commit_msg>Removed unnecessary decoding function<commit_after>//
//  Dictionary+JSONKey.swift
//  JSONUtilities
//
//  Created by <NAME> on 05/03/2016.
//  Copyright © 2016 TechBrewers LTD. All rights reserved.
//

import Foundation

public protocol JSONRawType {}
extension Int : JSONRawType {}
extension Double : JSONRawType {}
extension Float : JSONRawType {}
extension String : JSONRawType {}
extension Bool : JSONRawType {}

// Simple protocol used to extend a JSONDictionary
public protocol StringProtocol {}
extension String: StringProtocol {}

extension Dictionary where Key: StringProtocol {
  
  // MARK: JSONRawType type
  
  /// Decode a mandatory JSON raw type
  public func jsonKey<ReturnType : JSONRawType>(key: Key) throws -> ReturnType {
    
    guard let value = self[key] as? ReturnType else {
      throw DecodingError.MandatoryKeyNotFound(key: key)
    }
    return value
  }
  
  /// Decode an optional JSON raw type
  public func jsonKey<ReturnType : JSONRawType>(key: Key) -> ReturnType? {
    return self[key] as? ReturnType
  }
  
  // MARK: [JSONRawType] type
  
  /// Decode an Array of mandatory JSON raw types
  public func jsonKey<ReturnType>(key: Key) throws -> [ReturnType] {
    guard let value = self[key] as? [ReturnType] else {
      throw DecodingError.MandatoryKeyNotFound(key: key)
    }
    return value
  }
  
  /// Decode an Array of optional JSON raw types
  public func jsonKey<ReturnType>(key: Key) -> [ReturnType]? {
    return self[key] as? [ReturnType]
  }
  
  // MARK: [String: AnyObject] type
  
  /// Decodes as a raw Dictionary with a mandatory key
  public func jsonKey(key: Key) throws -> JSONDictionary {
    
    guard let value = self[key] as? JSONDictionary else {
      throw DecodingError.MandatoryKeyNotFound(key: key)
    }
    return value
  }

  /// Decodes as a raw dictionary with an optional key
  public func jsonKey(key: Key) -> JSONDictionary? {
    return self[key] as? JSONDictionary
  }
  
  // MARK: Decodable types
  
  /// Decode a mandatory Decodable object
  public func jsonKey<ReturnType : Decodable>(key: Key) throws -> ReturnType {
    return try ReturnType(jsonDictionary: JSONDictionaryForKey(key))
  }
  
  /// Decode an optional Decodable object
  public func jsonKey<ReturnType : Decodable>(key: Key) -> ReturnType? {
    return try? ReturnType(jsonDictionary: JSONDictionaryForKey(key))
  }
  
  // MARK: [Decodable] types
  
  /// Decode an Array of mandatory Decodable objects
  public func jsonKey<ReturnType : Decodable>(key: Key) throws -> [ReturnType] {
    return decodableObjectsArray(try JSONArrayForKey(key))
  }
  
{'fraction_non_alphanumeric': 0.09779346866725508, 'fraction_numerical': 0.004236540158870256, 'mean_word_length': 3.2924242424242425, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '959694', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1674, 'n_words': 548}