001 /**
002 * Copyright (C) 2009-2011 the original author or authors.
003 * See the notice.md file distributed with this work for additional
004 * information regarding copyright ownership.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.codehaus.jackson.map.jsontype;
020
021 import org.codehaus.jackson.annotate.JsonTypeInfo;
022 import org.codehaus.jackson.type.JavaType;
023
024 /**
025 * Interface that defines standard API for converting types
026 * to type identifiers and vice versa. Used by type resolvers
027 * ({@link org.codehaus.jackson.map.TypeSerializer},
028 * {@link org.codehaus.jackson.map.TypeDeserializer}) for converting
029 * between type and matching id; id is stored in JSON and needed for
030 * creating instances of proper subtypes when deserializing values.
031 *
032 * @since 1.5
033 */
034 public interface TypeIdResolver
035 {
036 /*
037 /**********************************************************
038 /* Initialization/configuration methods
039 /**********************************************************
040 */
041
042 /**
043 * Method that will be called once before any type resolution calls;
044 * used to initialize instance with configuration. This is necessary
045 * since instances may be created via reflection, without ability to
046 * call specific constructor to pass in configuration settings.
047 *
048 * @param baseType Base type for which this id resolver instance is
049 * used
050 */
051 public void init(JavaType baseType);
052
053 /*
054 /**********************************************************
055 /* Conversions between types and type ids
056 /**********************************************************
057 */
058
059 /**
060 * Method called to serialize type of the type of given value
061 * as a String to include in serialized JSON content.
062 */
063 public String idFromValue(Object value);
064
065 /**
066 * Alternative method used for determining type from combination of
067 * value and type, using suggested type (that serializer provides)
068 * and possibly value of that type. Most common implementation will
069 * use suggested type as is.
070 *
071 * @since 1.8
072 */
073 public String idFromValueAndType(Object value, Class<?> suggestedType);
074
075 /**
076 * Method called to resolve type from given type identifier.
077 */
078 public JavaType typeFromId(String id);
079
080 /*
081 /**********************************************************
082 /* Accessors for metadata
083 /**********************************************************
084 */
085
086 /**
087 * Accessor for mechanism that this resolver uses for determining
088 * type id from type. Mostly informational; not required to be called
089 * or used.
090 */
091 public JsonTypeInfo.Id getMechanism();
092 }