<commit_before>// Copyright (C) 2016 <NAME>, Inc. Licensed under the MIT license WITHOUT ANY WARRANTY.

import Foundation

// MARK: - Serialize Options

/// An `OptionSet` used to represent the different options available for serializing `JSON` with `null` values.
/// * `.nullSkipsKey` - Skip keys with `null` values
public struct SerializeOptions: OptionSet {
    public let rawValue: Int
    public init(rawValue: Int) {
        self.rawValue = rawValue
    }
    
    /// Skip keys with `null` values
    public static let nullSkipsKey = SerializeOptions(rawValue: 1 << 0)
}

// MARK: - Serialize JSON

extension JSON {

    /// Attempt to serialize `JSON` into an `Data`.
    /// - returns: A byte-stream containing the `JSON` ready for wire transfer.
    /// - throws: Errors that arise from `JSONSerialization`.
    /// - see: Foundation.JSONSerialization
    public func serialize(options: SerializeOptions = SerializeOptions(rawValue: 0)) throws -> Data {
        return try JSONSerialization.data(withJSONObject: toJSONSerializationValue(options: options), options: [])
    }
    
    /// Attempt to serialize `JSON` into a `String`.
    /// - returns: A `String` containing the `JSON`.
    /// - throws: A `JSON.Error.StringSerializationError` or errors that arise from `JSONSerialization`.
    /// - see: Foundation.JSONSerialization
    public func serializeString(options: SerializeOptions = SerializeOptions(rawValue: 0)) throws -> String {
        let data = try self.serialize(options: options)
        guard let json = String(data: data, encoding: String.Encoding.utf8) else {
            throw Error.stringSerializationError
        }
        return json
    }

    /// A function to help with the serialization of `JSON`.
    /// - returns: An `Any` suitable for `JSONSerialization`'s use.
    private func toJSONSerializationValue(options: SerializeOptions = SerializeOptions(rawValue: 0)) -> Any {
        switch self {
        case .array(let jsonArray):
            return jsonArray.map { $0.toJSONSerializationValue() }
        case .dictionary(let jsonDictionary):
            var cocoaDictionary = Swift.Dictionary<Swift.String, Any>(minimumCapacity: jsonDictionary.count)<commit_msg>Simplify syntax for default options value<commit_after>// Copyright (C) 2016 <NAME>, Inc. Licensed under the MIT license WITHOUT ANY WARRANTY.

import Foundation

// MARK: - Serialize Options

/// An `OptionSet` used to represent the different options available for serializing `JSON` with `null` values.
/// * `.nullSkipsKey` - Skip keys with `null` values
public struct SerializeOptions: OptionSet {
    public let rawValue: Int
    public init(rawValue: Int) {
        self.rawValue = rawValue
    }
    
    /// Skip keys with `null` values
    public static let nullSkipsKey = SerializeOptions(rawValue: 1 << 0)
}

// MARK: - Serialize JSON

extension JSON {

    /// Attempt to serialize `JSON` into an `Data`.
    /// - returns: A byte-stream containing the `JSON` ready for wire transfer.
    /// - throws: Errors that arise from `JSONSerialization`.
    /// - see: Foundation.JSONSerialization
    public func serialize(options: SerializeOptions = []) throws -> Data {
        return try JSONSerialization.data(withJSONObject: toJSONSerializationValue(options: options), options: [])
    }
    
    /// Attempt to serialize `JSON` into a `String`.
    /// - returns: A `String` containing the `JSON`.
    /// - throws: A `JSON.Error.StringSerializationError` or errors that arise from `JSONSerialization`.
    /// - see: Foundation.JSONSerialization
    public func serializeString(options: SerializeOptions = []) throws -> String {
        let data = try self.serialize(options: options)
        guard let json = String(data: data, encoding: String.Encoding.utf8) else {
            throw Error.stringSerializationError
        }
        return json
    }

    /// A function to help with the serialization of `JSON`.
    /// - returns: An `Any` suitable for `JSONSerialization`'s use.
    private func toJSONSerializationValue(options: SerializeOptions = []) -> Any {
        switch self {
        case .array(let jsonArray):
            return jsonArray.map { $0.toJSONSerializationValue() }
        case .dictionary(let jsonDictionary):
            var cocoaDictionary = Swift.Dictionary<Swift.String, Any>(minimumCapacity: jsonDictionary.count)
{'fraction_non_alphanumeric': 0.10551613641565576, 'fraction_numerical': 0.00434882124055848, 'mean_word_length': 3.0128558310376494, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '2858734', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1141, 'n_words': 392}