Example usage for com.google.gson JsonElement getAsByte

List of usage examples for com.google.gson JsonElement getAsByte

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsByte.

Prototype

public byte getAsByte() 

Source Link

Document

convenience method to get this element as a primitive byte value.

Usage

From source file:org.uorm.serializer.DefaultJsonSerializer.java

License:Apache License

@Override
public Map<String, Object> deserialize2(Class<?> cls, String json) throws Exception {
    JsonStreamParser parser = new JsonStreamParser(json);
    if (parser.hasNext()) {
        JsonObject jsonobj = parser.next().getAsJsonObject();
        Set<Entry<String, JsonElement>> jset = jsonobj.entrySet();
        if (!jset.isEmpty()) {
            Map<String, PropertyDescriptor> propMap = ObjectMappingCache.getInstance()
                    .getObjectPropertyMap(cls);
            Map<String, Object> instance = new HashMap<String, Object>();
            for (Entry<String, JsonElement> entry : jset) {
                String name = entry.getKey();
                JsonElement val = entry.getValue();
                if (!val.isJsonNull()) {
                    PropertyDescriptor descriptor = propMap.get(name);
                    if (descriptor != null) {
                        Class<?> memberType = descriptor.getPropertyType();
                        if (memberType == String.class) {
                            instance.put(name, val.getAsString());
                        } else if (memberType == Integer.class || memberType == Integer.TYPE) {
                            instance.put(name, val.getAsInt());
                        } else if (memberType == Byte.class || memberType == Byte.TYPE) {
                            instance.put(name, val.getAsByte());
                        } else if (memberType == Double.class || memberType == Double.TYPE) {
                            instance.put(name, val.getAsDouble());
                        } else if (memberType == Float.class || memberType == Float.TYPE) {
                            instance.put(name, val.getAsFloat());
                        } else if (memberType == Long.class || memberType == Long.TYPE) {
                            instance.put(name, val.getAsLong());
                        } else if (memberType == Short.class || memberType == Short.TYPE) {
                            instance.put(name, val.getAsShort());
                        } else if (memberType == Boolean.class || memberType == Boolean.TYPE) {
                            instance.put(name, val.getAsBoolean());
                        } else if (memberType == BigDecimal.class) {
                            instance.put(name, val.getAsBigDecimal());
                        } else if (memberType == BigInteger.class) {
                            instance.put(name, val.getAsBigInteger());
                        } else if (memberType == byte[].class) {
                            instance.put(name, val.getAsString().getBytes());
                        } else if (memberType == java.sql.Timestamp.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.sql.Timestamp(time));
                            }/*from  w  ww .  j a  v a2s  . c om*/
                        } else if (memberType == java.sql.Date.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.sql.Date(time));
                            }
                        } else if (memberType == java.util.Date.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.util.Date(time));
                            }
                        } else {//default String
                            instance.put(name, val.getAsString());
                        }
                    } else {//String
                        instance.put(name, val.getAsString());
                    }
                }
            }
            return instance;
        }
    }
    return null;
}