com.jayway.restassured.internal.path.json.mapping.JsonPathJackson1ObjectDeserializer.groovy Maven / Gradle / Ivy
/*
* Copyright 2013 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.path.json.mapping
import com.jayway.restassured.mapper.ObjectDeserializationContext
import com.jayway.restassured.mapper.factory.Jackson1ObjectMapperFactory
import com.jayway.restassured.path.json.mapping.JsonPathObjectDeserializer
import org.codehaus.jackson.type.JavaType
class JsonPathJackson1ObjectDeserializer implements JsonPathObjectDeserializer {
private final Jackson1ObjectMapperFactory factory;
JsonPathJackson1ObjectDeserializer(Jackson1ObjectMapperFactory factory) {
this.factory = factory
}
private org.codehaus.jackson.map.ObjectMapper createJacksonObjectMapper(Class cls, String charset) {
return factory.create(cls, charset)
}
@Override
def T deserialize(ObjectDeserializationContext ctx) {
def object = ctx.getDataToDeserialize().asString()
def cls = ctx.getType()
def mapper = createJacksonObjectMapper(cls, ctx.getCharset())
JavaType javaType = mapper.constructType(cls)
return mapper.readValue(object, javaType) as T
}
}