// Path: http-builder-ng-core/src/main/java/groovyx/net/http/NativeHandlers.java
// public static boolean handleRawUpload(final ChainedHttpConfig config, final ToServer ts) {
//     final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
//     final Object body = request.actualBody();
//     final Charset charset = request.actualCharset();
// 
//     try {
//         if (body instanceof File) {
//             ts.toServer(new FileInputStream((File) body));
//             return true;
//         } else if (body instanceof Path) {
//             ts.toServer(Files.newInputStream((Path) body));
//             return true;
//         } else if (body instanceof byte[]) {
//             ts.toServer(new ByteArrayInputStream((byte[]) body));
//             return true;
//         } else if (body instanceof InputStream) {
//             ts.toServer((InputStream) body);
//             return true;
//         } else if (body instanceof Reader) {
//             ts.toServer(new ReaderInputStream((Reader) body, charset));
//             return true;
//         } else {
//             return false;
//         }
//     } catch (IOException e) {
//         throw new TransportingException(e);
//     }
// }


import com.fasterxml.jackson.databind.ObjectMapper;
import groovyx.net.http.*;
import java.io.IOException;
import java.io.StringWriter;
import static groovyx.net.http.NativeHandlers.Encoders.handleRawUpload;
/**
 * Copyright (C) 2017 HttpBuilder-NG Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovyx.net.http.optional;




/**
 * Parser and Encoder methods for handling JSON content using the https://github.com/FasterXML/jackson[Jackson] JSON library.
 */
public class Jackson {

    static final String OBJECT_MAPPER_ID = "0w4XJJnlTNK8dvISuCDTlsusPQE=";

    /**
     * Used to parse the server response content using the Jackson JSON parser.
     *
     * @param config the configuration
     * @param fromServer the server content accessor
     * @return the parsed object
     */
    @SuppressWarnings("WeakerAccess")
    public static Object parse(final ChainedHttpConfig config, final FromServer fromServer) {
        try {
            final ObjectMapper mapper = (ObjectMapper) config.actualContext(fromServer.getContentType(), OBJECT_MAPPER_ID);
            return mapper.readValue(fromServer.getReader(), config.getChainedResponse().getType());
        } catch (IOException e) {
            throw new TransportingException(e);
        }
    }

    /**
     * Used to encode the request content using the Jackson JSON encoder.
     *
     * @param config the configuration
     * @param ts the server request content accessor
     */
    @SuppressWarnings("WeakerAccess")
    public static void encode(final ChainedHttpConfig config, final ToServer ts) {
        try {
// Path: http-builder-ng-core/src/main/java/groovyx/net/http/NativeHandlers.java
// public static boolean handleRawUpload(final ChainedHttpConfig config, final ToServer ts) {
//     final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
//     final Object body = request.actualBody();
//     final Charset charset = request.actualCharset();
// 
//     try {
//         if (body instanceof File) {
//             ts.toServer(new FileInputStream((File) body));
//             return true;
//         } else if (body instanceof Path) {
//             ts.toServer(Files.newInputStream((Path) body));
//             return true;
//         } else if (body instanceof byte[]) {
//             ts.toServer(new ByteArrayInputStream((byte[]) body));
//             return true;
//         } else if (body instanceof InputStream) {
//             ts.toServer((InputStream) body);
//             return true;
//         } else if (body instanceof Reader) {
//             ts.toServer(new ReaderInputStream((Reader) body, charset));
//             return true;
//         } else {
//             return false;
//         }
//     } catch (IOException e) {
//         throw new TransportingException(e);
//     }
// }


// Path: http-builder-ng-core/src/main/java/groovyx/net/http/optional/Jackson.java
import com.fasterxml.jackson.databind.ObjectMapper;
import groovyx.net.http.*;
import java.io.IOException;
import java.io.StringWriter;
import static groovyx.net.http.NativeHandlers.Encoders.handleRawUpload;

/**
 * Copyright (C) 2017 HttpBuilder-NG Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovyx.net.http.optional;




/**
 * Parser and Encoder methods for handling JSON content using the https://github.com/FasterXML/jackson[Jackson] JSON library.
 */
public class Jackson {

    static final String OBJECT_MAPPER_ID = "0w4XJJnlTNK8dvISuCDTlsusPQE=";

    /**
     * Used to parse the server response content using the Jackson JSON parser.
     *
     * @param config the configuration
     * @param fromServer the server content accessor
     * @return the parsed object
     */
    @SuppressWarnings("WeakerAccess")
    public static Object parse(final ChainedHttpConfig config, final FromServer fromServer) {
        try {
            final ObjectMapper mapper = (ObjectMapper) config.actualContext(fromServer.getContentType(), OBJECT_MAPPER_ID);
            return mapper.readValue(fromServer.getReader(), config.getChainedResponse().getType());
        } catch (IOException e) {
            throw new TransportingException(e);
        }
    }

    /**
     * Used to encode the request content using the Jackson JSON encoder.
     *
     * @param config the configuration
     * @param ts the server request content accessor
     */
    @SuppressWarnings("WeakerAccess")
    public static void encode(final ChainedHttpConfig config, final ToServer ts) {
        try {
            if (handleRawUpload(config, ts)) {