All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jayway.restassured.internal.mapping.JacksonMapping.groovy Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2011 the original author or authors.
 *
 * 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 com.jayway.restassured.internal.mapping

import com.jayway.restassured.internal.http.CharsetExtractor
import org.codehaus.jackson.JsonEncoding
import org.codehaus.jackson.JsonGenerator
import org.codehaus.jackson.map.ObjectMapper
import org.codehaus.jackson.map.type.TypeFactory
import org.codehaus.jackson.type.JavaType

class JacksonMapping {
    def serialize(Object object, String contentType) {
        JsonEncoding jsonEncoding = getEncoding(contentType);
        def mapper = new ObjectMapper();
        def stream = new ByteArrayOutputStream();
        JsonGenerator jsonGenerator = mapper.getJsonFactory().createJsonGenerator(stream, jsonEncoding)
        mapper.writeValue(jsonGenerator, object);
        return stream.toString()
    }

    def deserialize(Object object, String charset, Class cls) {
        def mapper = new ObjectMapper();
        JavaType javaType = TypeFactory.type(cls)
        return mapper.readValue(object, javaType);
    }

    private JsonEncoding getEncoding(String contentType) {
        def foundEncoding = JsonEncoding.UTF8
        if(contentType != null) {
            def charset = CharsetExtractor.getCharsetFromContentType(contentType)
            if(charset != null) {
                for (JsonEncoding encoding : JsonEncoding.values()) {
                    if (charset.equals(encoding.getJavaName())) {
                        foundEncoding = encoding
                        break
                    }
                }
            }
        }
        return foundEncoding;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy