<commit_before>//
//  JSON.swift
//  Swift-idl
//
//  Created by <NAME> on 2018/04/24.
//  Copyright (c) 2018 Safx Developers. All rights reserved.
//

import Foundation


struct AnyCodingKeys: CodingKey {
    var stringValue: String

    init?(stringValue: String) {
        self.stringValue = stringValue
    }

    var intValue: Int?

    init?(intValue: Int) {
        self.init(stringValue: "\(intValue)")
        self.intValue = intValue
    }
}


enum JSON {
    case null
    case boolean(Bool)
    case number(Double)
    case string(String)
    indirect case array([JSON])
    indirect case object([String:JSON])
}


extension JSON: Decodable {
    init(from decoder: Decoder) throws {
        if let container = try? decoder.container(keyedBy: AnyCodingKeys.self) {
            self = JSON(from: container)
        } else if let container = try? decoder.unkeyedContainer() {
            self = JSON(from: container)
        } else {
            throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "malformed JSON"))
        }
    }

    private init(from container: KeyedDecodingContainer<AnyCodingKeys>) {
        var d: [String: JSON] = [:]
        for key in container.allKeys {
            if let value = try? container.decode(Bool.self, forKey: key) {
                d[key.stringValue] = .boolean(value)
            } else if let value = try? container.decode(Double.self, forKey: key) {
                d[key.stringValue] = .number(value)
            } else if let value = try? container.decode(String.self, forKey: key) {
                d[key.stringValue] = .string(value)
            } else if let value = try? container.nestedContainer(keyedBy: AnyCodingKeys.self, forKey: key) {
                d[key.stringValue] = JSON(from: value)
            } else if let value = try? container.nestedUnkeyedContainer(forKey: key) {
                d[key.stringValue] = JSON(from: value)
            }
        }
        self = .object(d)
    }

    private init(from container: UnkeyedDecodingContainer) {
        var container = container
        var a: [JSON] = []
        while !container.isAtEnd {
            if let value = try? container.decode(Bool.self) {
                a.append(.boolean(value))
            } else if let value = try? container.decode(Double.self) {
                a.append(.number(value))
            } else if let value = try? container.decode(String.self) {
                a.append(.string(value))
            } else if let value = try? container.nestedContainer(keyedBy: AnyCodingKeys.self) {
                a.append(JSON(from: value))
            } else if let value = try? container.nestedUnkeyedContainer() {
                a.append(JSON(from: value))
            }
        }
        self = .array(a)
    }
}


extension JSON: Encodable {

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case let .array(array):
            try container.encode(array)
        case let .object(object):
            try container.encode(object)
        case let .string(string):
            try container.encode(string)
        case let .number(number):<commit_msg>add public<commit_after>//
//  JSON.swift
//  Swift-idl
//
//  Created by Safx Developer on 2018/04/24.
//  Copyright (c) 2018 Safx Developers. All rights reserved.
//

import Foundation


struct AnyCodingKeys: CodingKey {
    var stringValue: String

    init?(stringValue: String) {
        self.stringValue = stringValue
    }

    var intValue: Int?

    init?(intValue: Int) {
        self.init(stringValue: "\(intValue)")
        self.intValue = intValue
    }
}


public enum JSON {
    case null
    case boolean(Bool)
    case number(Double)
    case string(String)
    indirect case array([JSON])
    indirect case object([String:JSON])
}


extension JSON: Decodable {
    public init(from decoder: Decoder) throws {
        if let container = try? decoder.container(keyedBy: AnyCodingKeys.self) {
            self = JSON(from: container)
        } else if let container = try? decoder.unkeyedContainer() {
            self = JSON(from: container)
        } else {
            throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "malformed JSON"))
        }
    }

    private init(from container: KeyedDecodingContainer<AnyCodingKeys>) {
        var d: [String: JSON] = [:]
        for key in container.allKeys {
            if let value = try? container.decode(Bool.self, forKey: key) {
                d[key.stringValue] = .boolean(value)
            } else if let value = try? container.decode(Double.self, forKey: key) {
                d[key.stringValue] = .number(value)
            } else if let value = try? container.decode(String.self, forKey: key) {
                d[key.stringValue] = .string(value)
            } else if let value = try? container.nestedContainer(keyedBy: AnyCodingKeys.self, forKey: key) {
                d[key.stringValue] = JSON(from: value)
            } else if let value = try? container.nestedUnkeyedContainer(forKey: key) {
                d[key.stringValue] = JSON(from: value)
            }
        }
        self = .object(d)
    }

    private init(from container: UnkeyedDecodingContainer) {
        var container = container
        var a: [JSON] = []
        while !container.isAtEnd {
            if let value = try? container.decode(Bool.self) {
                a.append(.boolean(value))
            } else if let value = try? container.decode(Double.self) {
                a.append(.number(value))
            } else if let value = try? container.decode(String.self) {
                a.append(.string(value))
            } else if let value = try? container.nestedContainer(keyedBy: AnyCodingKeys.self) {
                a.append(JSON(from: value))
            } else if let value = try? container.nestedUnkeyedContainer() {
                a.append(JSON(from: value))
            }
        }
        self = .array(a)
    }
}


extension JSON: Encodable {

    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case let .array(array):
            try container.encode(array)
        case let .object(object):
            try container.encode(object)
        case let .string(string):
            try container.encode(string)
        case let .number(number):
{'fraction_non_alphanumeric': 0.10874922982131854, 'fraction_numerical': 0.0036968576709796672, 'mean_word_length': 2.1156429942418424, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6470811', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1832, 'n_words': 468}