Search in sources :

Example 1 with EnumDeserializer

use of com.alibaba.fastjson.parser.deserializer.EnumDeserializer in project uavstack by uavorg.

the class TypeUtils method castToEnum.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T castToEnum(Object obj, Class<T> clazz, ParserConfig mapping) {
    try {
        if (obj instanceof String) {
            String name = (String) obj;
            if (name.length() == 0) {
                return null;
            }
            if (mapping == null) {
                mapping = ParserConfig.getGlobalInstance();
            }
            ObjectDeserializer derializer = mapping.getDeserializer(clazz);
            if (derializer instanceof EnumDeserializer) {
                EnumDeserializer enumDeserializer = (EnumDeserializer) derializer;
                return (T) enumDeserializer.getEnumByHashCode(TypeUtils.fnv1a_64(name));
            }
            return (T) Enum.valueOf((Class<? extends Enum>) clazz, name);
        }
        if (obj instanceof BigDecimal) {
            int ordinal = intValue((BigDecimal) obj);
            Object[] values = clazz.getEnumConstants();
            if (ordinal < values.length) {
                return (T) values[ordinal];
            }
        }
        if (obj instanceof Number) {
            int ordinal = ((Number) obj).intValue();
            Object[] values = clazz.getEnumConstants();
            if (ordinal < values.length) {
                return (T) values[ordinal];
            }
        }
    } catch (Exception ex) {
        throw new JSONException("can not cast to : " + clazz.getName(), ex);
    }
    throw new JSONException("can not cast to : " + clazz.getName());
}
Also used : JSONException(com.alibaba.fastjson.JSONException) BigDecimal(java.math.BigDecimal) ParseException(java.text.ParseException) JSONException(com.alibaba.fastjson.JSONException) AccessControlException(java.security.AccessControlException) EnumDeserializer(com.alibaba.fastjson.parser.deserializer.EnumDeserializer) JSONObject(com.alibaba.fastjson.JSONObject) AccessibleObject(java.lang.reflect.AccessibleObject) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Example 2 with EnumDeserializer

use of com.alibaba.fastjson.parser.deserializer.EnumDeserializer in project fastjson by alibaba.

the class TypeUtils method castToEnum.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T castToEnum(Object obj, Class<T> clazz, ParserConfig mapping) {
    try {
        if (obj instanceof String) {
            String name = (String) obj;
            if (name.length() == 0) {
                return null;
            }
            if (mapping == null) {
                mapping = ParserConfig.getGlobalInstance();
            }
            ObjectDeserializer deserializer = mapping.getDeserializer(clazz);
            if (deserializer instanceof EnumDeserializer) {
                EnumDeserializer enumDeserializer = (EnumDeserializer) deserializer;
                return (T) enumDeserializer.getEnumByHashCode(TypeUtils.fnv1a_64(name));
            }
            return (T) Enum.valueOf((Class<? extends Enum>) clazz, name);
        }
        if (obj instanceof BigDecimal) {
            int ordinal = intValue((BigDecimal) obj);
            Object[] values = clazz.getEnumConstants();
            if (ordinal < values.length) {
                return (T) values[ordinal];
            }
        }
        if (obj instanceof Number) {
            int ordinal = ((Number) obj).intValue();
            Object[] values = clazz.getEnumConstants();
            if (ordinal < values.length) {
                return (T) values[ordinal];
            }
        }
    } catch (Exception ex) {
        throw new JSONException("can not cast to : " + clazz.getName(), ex);
    }
    throw new JSONException("can not cast to : " + clazz.getName());
}
Also used : JSONException(com.alibaba.fastjson.JSONException) BigDecimal(java.math.BigDecimal) ParseException(java.text.ParseException) JSONException(com.alibaba.fastjson.JSONException) AccessControlException(java.security.AccessControlException) EnumDeserializer(com.alibaba.fastjson.parser.deserializer.EnumDeserializer) JSONObject(com.alibaba.fastjson.JSONObject) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Example 3 with EnumDeserializer

use of com.alibaba.fastjson.parser.deserializer.EnumDeserializer in project fastjson by alibaba.

the class EnumParserTest method test_error_3.

public void test_error_3() throws Exception {
    Exception error = null;
    try {
        String text = "4";
        DefaultJSONParser parser = new DefaultJSONParser(text);
        new EnumDeserializer(Object.class).deserialze(parser, Object.class, null);
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : EnumDeserializer(com.alibaba.fastjson.parser.deserializer.EnumDeserializer) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 4 with EnumDeserializer

use of com.alibaba.fastjson.parser.deserializer.EnumDeserializer in project fastjson by alibaba.

the class EnumParserTest method test_error_4.

public void test_error_4() throws Exception {
    Exception error = null;
    try {
        String text = "true";
        DefaultJSONParser parser = new DefaultJSONParser(text);
        new EnumDeserializer(Object.class).deserialze(parser, Object.class, null);
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : EnumDeserializer(com.alibaba.fastjson.parser.deserializer.EnumDeserializer) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Aggregations

EnumDeserializer (com.alibaba.fastjson.parser.deserializer.EnumDeserializer)4 JSONException (com.alibaba.fastjson.JSONException)2 JSONObject (com.alibaba.fastjson.JSONObject)2 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)2 ObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)2 BigDecimal (java.math.BigDecimal)2 AccessControlException (java.security.AccessControlException)2 ParseException (java.text.ParseException)2 AccessibleObject (java.lang.reflect.AccessibleObject)1