<commit_before>//
// JsonModelTransformerHttpSerializer
// Legacy
//
// Copyright (c) 2015 <NAME>.
// License: MIT, https://github.com/eugeneego/legacy/blob/master/LICENSE
//

import Foundation

public struct JsonModelLightTransformerHttpSerializer<T: LightTransformer>: HttpSerializer {
    public typealias Value = T.T

    public let contentType = "application/json"

    public let transformer: T

    public init(transformer: T) {
        self.transformer = transformer
    }

    public func serialize(_ value: Value?) -> Data? {
        return transformer.to(any: value).flatMap { try? JSONSerialization.data(withJSONObject: $0, options: []) }
    }

    public func deserialize(_ data: Data?) -> Value? {
        return transformer.from(any: data.flatMap { try? JSONSerialization.jsonObject(with: $0, options: .allowFragments) })
    }
}

public struct JsonModelForwardTransformerHttpSerializer<T: ForwardTransformer>: HttpSerializer where T.Source == Any {
    public typealias Value = T.Destination

    public let contentType = "application/json"

    public let transformer: T

    public init(transformer: T) {
        self.transformer = transformer
    }

    public func serialize(_ value: Value?) -> Data? {
        return nil
    }

    public func deserialize(_ data: Data?) -> Value? {
        guard
            let data = data,
            let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments),
            let value = transformer.transform(source: json).value
        else { return nil }

        return value
    }
}

public struct JsonModelBackwardTransformerHttpSerializer<T: BackwardTransformer>: HttpSerializer where T.Source == Any {
    public typealias Value = T.Destination

    public let contentType = "application/json"

    public let transformer: T

    public init(transformer: T) {
        self.transformer = transformer
    }

    public func serialize(_ value: Value?) -> Data? {
        guard
            let value = value,
            let json = transformer.transform(destination: value).value,
            let data = try? JSONSerialization.data(withJSONObject: json, options: [])
        else { return nil }

        return data
    }

    public func deserialize(_ data: Data?) -> Value? {
        return nil
    }
}

public struct JsonModelFullTransformerHttpSerializer<T: FullTransformer>: HttpSerializer where T.Source == Any {
    public typealias Value = T.Destination

    public let contentType = "application/json"

    public let transformer: T

    public init(transformer: T) {
        self.transformer = transformer
    }

    public func serialize(_ value: Value?) -> Data? {
        guard
            let value = value,
            let json = transformer.transform(destination: value).value,
            let data = try? JSONSerialization.data(withJSONObject: json, options: [])
        else { return nil }

        return data
    }

    public func deserialize(_ data: Data?) -> Value? {
        guard
            let data = data,
            let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments),
            let value = transformer.transform(source: json).value
        else { return nil }

        return value<commit_msg>Updated json model serializer to support empty http responses.<commit_after>//
// JsonModelTransformerHttpSerializer
// Legacy
//
// Copyright (c) 2015 <NAME>.
// License: MIT, https://github.com/eugeneego/legacy/blob/master/LICENSE
//

import Foundation

public struct JsonModelLightTransformerHttpSerializer<T: LightTransformer>: HttpSerializer {
    public typealias Value = T.T

    public let contentType = "application/json"

    public let transformer: T

    public init(transformer: T) {
        self.transformer = transformer
    }

    public func serialize(_ value: Value?) -> Data? {
        return transformer.to(any: value).flatMap { try? JSONSerialization.data(withJSONObject: $0, options: []) }
    }

    public func deserialize(_ data: Data?) -> Value? {
        return transformer.from(any: data.flatMap { try? JSONSerialization.jsonObject(with: $0, options: .allowFragments) })
    }
}

public struct JsonModelForwardTransformerHttpSerializer<T: ForwardTransformer>: HttpSerializer where T.Source == Any {
    public typealias Value = T.Destination

    public let contentType = "application/json"

    public let transformer: T

    public init(transformer: T) {
        self.transformer = transformer
    }

    public func serialize(_ value: Value?) -> Data? {
        return nil
    }

    public func deserialize(_ data: Data?) -> Value? {
        guard let data = data, !data.isEmpty else { return transformer.transform(source: ()).value }
        guard
            let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments),
            let value = transformer.transform(source: json).value
        else { return nil }

        return value
    }
}

public struct JsonModelBackwardTransformerHttpSerializer<T: BackwardTransformer>: HttpSerializer where T.Source == Any {
    public typealias Value = T.Destination

    public let contentType = "application/json"

    public let transformer: T

    public init(transformer: T) {
        self.transformer = transformer
    }

    public func serialize(_ value: Value?) -> Data? {
        guard
            let value = value,
            let json = transformer.transform(destination: value).value,
            let data = try? JSONSerialization.data(withJSONObject: json, options: [])
        else { return nil }

        return data
    }

    public func deserialize(_ data: Data?) -> Value? {
        return nil
    }
}

public struct JsonModelFullTransformerHttpSerializer<T: FullTransformer>: HttpSerializer where T.Source == Any {
    public typealias Value = T.Destination

    public let contentType = "application/json"

    public let transformer: T

    public init(transformer: T) {
        self.transformer = transformer
    }

    public func serialize(_ value: Value?) -> Data? {
        guard
            let value = value,
            let json = transformer.transform(destination: value).value,
            let data = try? JSONSerialization.data(withJSONObject: json, options: [])
        else { return nil }

        return data
    }

    public func deserialize(_ data: Data?) -> Value? {
        guard let data = data, !data.isEmpty else { return transformer.transform(source: ()).value }
        guard
            let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments),
            let value = transformer.transform(source: json).value
        else { return nil }

        return value
{'fraction_non_alphanumeric': 0.09543071161048688, 'fraction_numerical': 0.0017977528089887641, 'mean_word_length': 2.7463524130190797, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 2, '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': '2771363', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1725, 'n_words': 527}