com.arpnetworking.jackson.BuilderDeserializer Maven / Gradle / Ivy
/**
* Copyright 2014 Groupon.com
*
* 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.arpnetworking.jackson;
import com.arpnetworking.commons.builder.Builder;
import com.arpnetworking.steno.Logger;
import com.arpnetworking.steno.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.base.Objects;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Set;
/**
* Deserialize JSON into an instance of a specified type T
given a
* builder that creates instances of that type (or a subtype). In order to use
* this deserializer with an ObjectMapper
be sure to register the
* transitive closure of builder deserializers (e.g. type-builder pairs) that
* are required to deserialize an instance of the root type.
*
* TODO(vkoskela): This is _duplicated_ from tsd-core and should find its way to a common utility package.
*
* @param The type this deserializer supports.
*
* @author Ville Koskela (vkoskela at groupon dot com)
*/
public final class BuilderDeserializer extends JsonDeserializer {
/**
* Static factory method.
*
* @param The type this deserializer supports.
* @param builderClass The builder class for the supported type.
* @return Instance of BuilderDeserializer
.
*/
public static BuilderDeserializer extends S> of(final Class extends Builder extends S>> builderClass) {
// TODO(vkoskela): Add caching to avoid unnecessary instances [MAI-114]
return new BuilderDeserializer(builderClass);
}
/**
* Register builder deserializers for the the transitive closure of builders
* anchored by the target class in the provided SimpleModule
.
*
* @param targetModule The SimpleModule
to register in.
* @param targetClass The root of the type tree to traverse.
*/
public static void addTo(final SimpleModule targetModule, final Class> targetClass) {
final Map, JsonDeserializer>> typeToDeserializer = Maps.newHashMap();
addTo(Sets.newHashSet(), typeToDeserializer, targetClass);
for (final Map.Entry, JsonDeserializer>> entry : typeToDeserializer.entrySet()) {
@SuppressWarnings("unchecked")
final Class
© 2015 - 2025 Weber Informatics LLC | Privacy Policy