<commit_before>//
//  JSONValue.swift
//  Crust
//
//  Created by <NAME> on 2014-06-21.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

/// Represents a JSON value.
enum JSONValue: Equatable, Hashable {
	case object(Dictionary<String, JSONValue>)
	case array(Array<JSONValue>)
	case string(String)
	case number(NSNumber)
	case boolean(Bool)
	case null

	var hashValue: Int {
		get {
			switch self {
			case let .object(dict):
				return dict.count.hashValue

			case let .array(arr):
				return arr.count.hashValue

			case let .string(str):
				return str.hashValue

			case let .number(num):
				return num.hashValue

			case let .boolean(b):
				return b.hashValue

			case let .null:
				return 0
			}
		}
	}

	/// Converts a dynamically-typed Objective-C object (such as you might get
	/// back from `NSJSONSerialization`) into a `JSONValue`.
	static func fromObject(obj: AnyObject) -> Result<JSONValue> {
		if let d = obj as? Dictionary<String, AnyObject> {
			var verifiedDictionary = Dictionary<String, JSONValue>()

			for (key, value: AnyObject) in d {
				switch fromObject(value) {
				case let .success(newValue):
					verifiedDictionary[key] = newValue
				
				case let .error(error):
					return .error(error)
				}<commit_msg>Allow optional subscripting through JSONValue<commit_after>//
//  JSONValue.swift
//  Crust
//
//  Created by <NAME> on 2014-06-21.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

/// Represents a JSON value.
enum JSONValue: Equatable, Hashable {
	case object(Dictionary<String, JSONValue>)
	case array(Array<JSONValue>)
	case string(String)
	case number(NSNumber)
	case boolean(Bool)
	case null

	var hashValue: Int {
		get {
			switch self {
			case let .object(dict):
				return dict.count.hashValue

			case let .array(arr):
				return arr.count.hashValue

			case let .string(str):
				return str.hashValue

			case let .number(num):
				return num.hashValue

			case let .boolean(b):
				return b.hashValue

			case let .null:
				return 0
			}
		}
	}

	subscript(key: String) -> JSONValue? {
		switch self {
		case let .object(dict):
			return dict[key]

		default:
			return nil
		}
	}

	/// Converts a dynamically-typed Objective-C object (such as you might get
	/// back from `NSJSONSerialization`) into a `JSONValue`.
	static func fromObject(obj: AnyObject) -> Result<JSONValue> {
		if let d = obj as? Dictionary<String, AnyObject> {
			var verifiedDictionary = Dictionary<String, JSONValue>()

			for (key, value: AnyObject) in d {
				switch fromObject(value) {
				case let .success(newValue):
					verifiedDictionary[key] = newValue
				
				case let .error(error):
					return .error(error)
				}
{'fraction_non_alphanumeric': 0.11328413284132842, 'fraction_numerical': 0.00959409594095941, 'mean_word_length': 3.0402384500745154, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 20, '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': '1163337', 'n_tokens_mistral': 1037, 'n_tokens_neox': 853, 'n_words': 265}