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

org.codehaus.jackson.map.deser.std.ClassDeserializer Maven / Gradle / Ivy

Go to download

Data Mapper package is a high-performance data binding package built on Jackson JSON processor

There is a newer version: 1.9.13
Show newest version
package org.codehaus.jackson.map.deser.std;

import java.io.IOException;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.annotate.JacksonStdImpl;

/**
 * 
 * @since 1.9 (renamed from 'org.codehaus.jackson.map.deser.StdDeserializer#ClassDeserializer')
 */
@JacksonStdImpl
public class ClassDeserializer
    extends StdScalarDeserializer>
{
    public ClassDeserializer() { super(Class.class); }

    @Override
    public Class deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException
    {
        JsonToken curr = jp.getCurrentToken();
        // Currently will only accept if given simple class name
        if (curr == JsonToken.VALUE_STRING) {
            String className = jp.getText();
            // [JACKSON-597]: support primitive types (and void)
            if (className.indexOf('.') < 0) {
                if ("int".equals(className)) return Integer.TYPE;
                if ("long".equals(className)) return Long.TYPE;
                if ("float".equals(className)) return Float.TYPE;
                if ("double".equals(className)) return Double.TYPE;
                if ("boolean".equals(className)) return Boolean.TYPE;
                if ("byte".equals(className)) return Byte.TYPE;
                if ("char".equals(className)) return Character.TYPE;
                if ("short".equals(className)) return Short.TYPE;
                if ("void".equals(className)) return Void.TYPE;
            }
            try {
                return Class.forName(jp.getText());
            } catch (ClassNotFoundException e) {
                throw ctxt.instantiationException(_valueClass, e);
            }
        }
        throw ctxt.mappingException(_valueClass, curr);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy