Example usage for java.util Map getClass

List of usage examples for java.util Map getClass

Introduction

In this page you can find the example usage for java.util Map getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:ca.oson.json.Oson.java

private <E> Map json2Map(FieldData objectDTO) {
    Object value = objectDTO.valueToProcess;
    Map returnObj = (Map) objectDTO.returnObj;
    Class<Map> returnType = objectDTO.returnType;
    Map defaultValue = (Map) objectDTO.defaultValue;

    if (!StringUtil.isEmpty(value)) {
        boolean map2ListStyle = this.isMap2ListStyle();

        Map<Object, Object> values = null;
        Class valueType = value.getClass();
        if (Map.class.isAssignableFrom(valueType)) {
            try {
                values = (Map<Object, Object>) value;
            } catch (Exception e) {
                // values = null; // new HashMap();
            }/*from ww w  .  j  a v a 2  s . co  m*/

        } else if (List.class.isAssignableFrom(valueType)) {
            values = ArrayToJsonMap.list2Map((List) value);
            objectDTO.valueToProcess = values;
            map2ListStyle = true;

        } else {
            try {
                values = (Map<Object, Object>) value;
            } catch (Exception e) {
            }
        }

        if (values != null && values.size() > 0) {
            Function function = objectDTO.getDeserializer();

            if (function != null) {
                try {
                    Object returnedValue = null;

                    if (function instanceof Json2DataMapperFunction) {
                        DataMapper classData = new DataMapper(returnType, values, objectDTO.classMapper,
                                objectDTO.level, getPrettyIndentation());
                        returnedValue = ((Json2DataMapperFunction) function).apply(classData);

                    } else if (function instanceof Json2FieldDataFunction) {
                        Json2FieldDataFunction f = (Json2FieldDataFunction) function;
                        FieldData fieldData = objectDTO.clone();

                        fieldData.valueToProcess = values;

                        returnedValue = f.apply(fieldData);

                    } else if (function instanceof Json2MapFunction) {
                        return ((Json2MapFunction) function).apply(values);

                    } else {
                        returnedValue = function.apply(values);
                    }

                    if (returnedValue instanceof Optional) {
                        returnedValue = ObjectUtil.unwrap(returnedValue);
                    }

                    if (returnedValue == null) {
                        return null;

                    } else if (Map.class.isAssignableFrom(returnedValue.getClass())) {
                        return (Map) returnedValue;

                    } else {
                        // do not know what to do
                    }

                } catch (Exception e) {
                }
            }

            if (values != null && values.size() > 0) {
                if (returnObj == null) {
                    if (defaultValue != null) {
                        returnObj = defaultValue;
                    }

                    if (returnObj == null) {
                        returnObj = newInstance(new HashMap(), returnType);
                    }

                    if (returnObj == null) {
                        returnObj = DefaultValue.map(returnType);
                    }

                    if (returnObj == null) {
                        returnObj = new HashMap();
                    }
                }

                objectDTO.incrLevel();
                Class<E> componentType = guessComponentType(objectDTO); // objectDTO.getComponentType(getJsonClassType());
                boolean isObject = ObjectUtil.isObject(componentType);

                Collection<Object> keys = values.keySet();
                // || getOrderByKeyAndProperties()
                if (objectDTO.classMapper.orderByKeyAndProperties) {
                    keys = new TreeSet(keys);
                }

                boolean isBsonDocument = returnObj.getClass().getCanonicalName()
                        .equals("org.bson.BsonDocument");

                //for (Entry<String, Object> entry: values.entrySet()) {
                // Object obj = entry.getValue(); // obj.getClass()
                for (Object key : keys) {
                    Object obj = values.get(key); // entry.getValue(); // obj.getClass()

                    Object component = null;
                    if (obj != null) {
                        FieldData newFieldData = new FieldData(obj, obj.getClass(), objectDTO.json2Java,
                                objectDTO.level, objectDTO.set);
                        newFieldData.field = objectDTO.field;
                        newFieldData.isMapValue = true;
                        newFieldData.returnType = guessComponentType(newFieldData);
                        if (isObject) {
                            if (ObjectUtil.isBasicDataType(newFieldData.returnType)) {
                                newFieldData.returnType = componentType;
                            }
                        }
                        newFieldData.fieldMapper = objectDTO.fieldMapper;

                        if (isBsonDocument) {
                            newFieldData.defaultValue = json2Object(newFieldData);
                            component = Json2BsonValueFunction.apply(newFieldData);
                            if (component == null) {
                                continue;
                            }
                        } else {
                            component = json2Object(newFieldData);
                        }
                    }

                    if (component == null && getDefaultType() == JSON_INCLUDE.DEFAULT) {
                        component = getDefaultValue(componentType);
                    }

                    //String key = entry.getKey();
                    Object keyObj = null;
                    if (Map.class.isAssignableFrom(key.getClass()) || StringUtil.parenthesized((String) key)) {
                        if (Map.class.isAssignableFrom(key.getClass())) {
                            keyObj = key;
                        } else {
                            keyObj = getListMapObject((String) key);
                        }
                        FieldData newFieldData = new FieldData(keyObj, keyObj.getClass(), objectDTO.json2Java,
                                objectDTO.level, objectDTO.set);
                        newFieldData.field = objectDTO.field;
                        newFieldData.setter = objectDTO.setter;
                        newFieldData.enclosingtype = objectDTO.enclosingtype;
                        newFieldData.returnType = guessMapKeyType(newFieldData);
                        newFieldData.fieldMapper = objectDTO.fieldMapper;
                        keyObj = json2Object(newFieldData);

                    } else {
                        FieldData newFieldData = new FieldData(key, key.getClass(), objectDTO.json2Java,
                                objectDTO.level, objectDTO.set);
                        newFieldData.field = objectDTO.field;
                        newFieldData.setter = objectDTO.setter;
                        newFieldData.enclosingtype = objectDTO.enclosingtype;
                        newFieldData.returnType = guessMapKeyType(newFieldData);
                        newFieldData.fieldMapper = objectDTO.fieldMapper;
                        keyObj = json2Object(newFieldData);
                    }

                    if (keyObj == null) { // failed to get a correct key value, just use its key
                        returnObj.put(key, component);

                    } else {
                        returnObj.put(keyObj, component);
                    }
                }

                return returnObj;
            }
        } else {
            return values;
        }
    }

    return json2MapDefault(objectDTO);
}