Package com.addthis.maljson

Examples of com.addthis.maljson.JSONObject


        // json config is "unexpectedly" an array; if the base class has registered a handler, use it
        if (json instanceof JSONArray) {
            Class<?> arrarySugar = classInfo.getArraySugar();
            if (arrarySugar != null) {
                LineNumberInfo infoCopy = ((JSONArray) json).getMyLineNumberInfo();
                JSONObject magicWrapper = new JSONObject();
                magicWrapper.put(classInfo.getPluginMap().aliasDefaults("_array").toConfig().getString("_primary"), json, infoCopy, infoCopy);
                classInfo = Fields.getClassFieldMap(arrarySugar);
                json = magicWrapper;
                type = (Class<T>) arrarySugar;
            }
        }
        if (!(json instanceof JSONObject)) {
            return (T) json;
        }
        JSONObject jsonObj = (JSONObject) json;

        if (info == LineNumberInfo.MissingInfo) {
            info = jsonObj.getLineNumberInfo();
        }

        String classField = classInfo.getClassField();
        String stype = jsonObj.optString(classField, null);
        if ((stype == null) && Modifier.isAbstract(type.getModifiers()) &&
            (jsonObj.length() == 1)) {
            // if otherwise doomed to fail, try supporting "type-value : {...}"  syntax
            stype = jsonObj.keySet().iterator().next();
            jsonObj = jsonObj.getJSONObject(stype);
        }
        try {
            if (stype != null) {
                Class<?> atype = classInfo.getClass(stype);
                classInfo = Fields.getClassFieldMap(atype);
                type = (Class<T>) atype;
                jsonObj.remove(classField);
            }
        } catch (ClassNotFoundException ex) {
            String helpMessage = Plugins.classNameSuggestions(
                    PluginRegistry.defaultRegistry(), classInfo.getPluginMap(), stype);
            throw new CodecExceptionLineNumber(new ClassNotFoundException(helpMessage, ex),
                                               jsonObj.getValLineNumber(classField));
        } catch (Exception ex) {
            throw new CodecExceptionLineNumber(ex, jsonObj.getValLineNumber(classField));
        }
        try {
            return decodeJSONInternal(classInfo, type.newInstance(), jsonObj, warnings);
        } catch (InstantiationException ex) {
            CodecExceptionLineNumber celn = translateInstantiationException(type, info);
View Full Code Here


                    throw new CodecExceptionLineNumber(
                            "Could not access the type or the constructor of " +
                            type.getName(), valInfo);
                }
                try {
                    ((JSONCodable) value).fromJSONObject(new JSONObject(oValue.toString()));
                } catch (Exception ex) {
                    throw new CodecExceptionLineNumber(ex, valInfo);
                }
            } else if (field.isArray()) {
                value = decodeArrayInternal(type, value, valInfo, warnings);
            } else if (field.isNative()) {
                if (value.getClass() != type) {
                    if (value.getClass() == Integer.class || value.getClass() == int.class) {
                        // upconvert integer values to long if the field requires long
                        if (type == Long.class || type == long.class || type == AtomicLong.class) {
                            value = new Long(((Integer) value).longValue());
                        }
                        // upconvert integer values to double if the field requires double
                        if (type == Double.class || type == double.class) {
                            value = new Double(((Integer) value));
                        }
                        // downconvert integer to short if the field requires short
                        if (type == Short.class || type == short.class) {
                            value = new Short(((Integer) value).shortValue());
                        }
                    }
                    // downconvert double to float if the field requires a float
                    if ((value.getClass() == double.class || value.getClass() == Double.class) &&
                        (type == float.class || type == Float.class)) {
                        value = new Float(((Double) value));
                    }
                    // upconvert long values to double if the field requires double
                    if ((value.getClass() == long.class || value.getClass() == Long.class) &&
                        (type == double.class || type == Double.class)) {
                        value = new Double(((Integer) value));
                    }
                    // upconvert float values to double if the field requires double
                    if ((value.getClass() == float.class || value.getClass() == Float.class) &&
                        (type == double.class || type == Double.class)) {
                        value = new Double(((Float) value));
                    }
                    if (value.getClass() == String.class) {
                        try {

                            // convert String values to int if the field requires int
                            if (type == Integer.class || type == int.class ||
                                type == AtomicInteger.class) {
                                value = Integer.parseInt((String) value);
                            }

                            // convert String values to long if the field requires long
                            if (type == long.class || type == Long.class ||
                                type == AtomicLong.class) {
                                value = Long.parseLong((String) value);
                            }

                            // convert String values to double if the field requires double
                            if (type == double.class || type == Double.class) {
                                value = Double.parseDouble((String) value);
                            }

                            // convert String values to boolean if the field requires boolean
                            if (type == boolean.class || type == Boolean.class ||
                                type == AtomicBoolean.class) {
                                value = Boolean.parseBoolean((String) value);
                            }
                        } catch (NumberFormatException ex) {
                            if (type == Integer.class || type == int.class ||
                                type == AtomicInteger.class) {
                                throw new CodecExceptionLineNumber(
                                        "cannot convert the string to an integer", valInfo);
                            } else if (type == long.class || type == Long.class ||
                                       type == AtomicLong.class) {
                                throw new CodecExceptionLineNumber(
                                        "cannot convert the string to a long", valInfo);
                            } else if (type == double.class || type == Double.class) {
                                throw new CodecExceptionLineNumber(
                                        "cannot convert the string to a double", valInfo);
                            } else {
                                throw new IllegalStateException(
                                        "unhandled case in the NumberFormatException");
                            }
                        }
                    }
                    if (type == AtomicInteger.class) {
                        value = new AtomicInteger((Integer) value);
                    } else if (type == AtomicLong.class) {
                        value = new AtomicLong((Long) value);

                    } else if (type == AtomicBoolean.class) {
                        value = new AtomicBoolean((Boolean) value);
                    }
                }
                // this space left intentionally blank
            } else if (field.isMap()) {
                Map map;
                try {
                    map = (Map) type.newInstance();
                } catch (Exception ex) {
                    throw new CodecExceptionLineNumber(ex, keyInfo);
                }
                JSONObject jmap = (JSONObject) value;
                Class vc = (Class) field.getGenericTypes()[1];
                boolean va = field.isMapValueArray();
                for (Iterator<String> iter = jmap.keys(); iter.hasNext(); ) {
                    String key = iter.next();
                    if (field.isInterned()) {
                        key = key.intern();
                    }
                    map.put(key, va ?
                                 decodeArrayInternal(vc, jmap.get(key), jmap.getKeyLineNumber(key),
                                                     warnings)
                                    :
                                 decodeObjectInternal(vc, jmap.get(key), jmap.getKeyLineNumber(key),
                                                      warnings));
                }
                value = map;
            } else if (field.isCollection()) {
                Collection col;
View Full Code Here

            return encodeArray(object);
        }
        if (object instanceof JSONCodable) {
            return ((JSONCodable) object).toJSONObject();
        }
        JSONObject obj = null;
        boolean lock = object instanceof ConcurrentCodable;
        if (lock) {
            ((ConcurrentCodable) object).encodeLock();
        }
        try {
            if (object instanceof SuperCodable) {
                ((SuperCodable) object).preEncode();
            }
            CodableClassInfo classInfo = Fields.getClassFieldMap(object.getClass());
            if (classInfo.size() == 0 && !(object instanceof Codable)) {
                return object;
            }
            obj = new JSONObject();
            String altType = classInfo.getClassName(object);
            if (altType != null) {
                obj.put(classInfo.getClassField(), altType);
            }
            for (Iterator<CodableFieldInfo> fields = classInfo.values().iterator(); fields.hasNext();) {
                CodableFieldInfo field = fields.next();
                Object value = field.get(object);
                if (value == null || value == JSONObject.NULL || field.isReadOnly()) {
                    continue;
                }
                if (JSONCodable.class.isAssignableFrom(field.getTypeOrComponentType())) {
                    value = ((JSONCodable) value).toJSONObject();
                    obj.put(field.getName(), value);
                } else if (field.isArray()) {
                    obj.put(field.getName(), encodeArray(value));
                } else if (field.isMap()) {
                    Map<?, ?> map = (Map<?, ?>) value;
                    JSONObject jmap = new JSONObject();
                    for (Entry<?, ?> entry : map.entrySet()) {
                        Object mval = entry.getValue();
                        // TODO fails with null keys
                        jmap.put(entry.getKey().toString(), encodeObject(mval));
                    }
                    obj.put(field.getName(), jmap);
                } else if (field.isCollection()) {
                    JSONArray jarr = new JSONArray();
                    for (Iterator<?> iter = ((Collection<?>) value).iterator(); iter.hasNext();) {
View Full Code Here

    }

    public static <T> T decodeString(T object, String json)
            throws CodecExceptionLineNumber, JSONException {

        return decodeJSON(object, new JSONObject(json));
    }
View Full Code Here

    }

    public static <T> T decodeString(T object, String json, List<CodecExceptionLineNumber> warnings)
            throws CodecExceptionLineNumber, JSONException {

        JSONObject jsonObj = new JSONObject(json);
        return decodeJSONInternal(Fields.getClassFieldMap(object.getClass()), object, jsonObj, warnings);
    }
View Full Code Here

        // json config is "unexpectedly" an array; if the base class has registered a handler, use it
        if (json instanceof JSONArray) {
            Class<?> arrarySugar = classInfo.getArraySugar();
            if (arrarySugar != null) {
                LineNumberInfo infoCopy = ((JSONArray) json).getMyLineNumberInfo();
                JSONObject magicWrapper = new JSONObject();
                magicWrapper.put(classInfo.getPluginMap().aliasDefaults("_array").toConfig().getString("_primary"), json, infoCopy, infoCopy);
                classInfo = Fields.getClassFieldMap(arrarySugar);
                json = magicWrapper;
                type = (Class<T>) arrarySugar;
            }
        }
        if (!(json instanceof JSONObject)) {
            return (T) json;
        }
        JSONObject jsonObj = (JSONObject) json;

        if (info == LineNumberInfo.MissingInfo) {
            info = jsonObj.getLineNumberInfo();
        }

        String classField = classInfo.getClassField();
        String stype = jsonObj.optString(classField, null);
        if ((stype == null) && Modifier.isAbstract(type.getModifiers()) &&
            (jsonObj.length() == 1)) {
            // if otherwise doomed to fail, try supporting "type-value : {...}"  syntax
            stype = jsonObj.keySet().iterator().next();
            jsonObj = jsonObj.getJSONObject(stype);
        }
        try {
            if (stype != null) {
                Class<?> atype = classInfo.getClass(stype);
                classInfo = Fields.getClassFieldMap(atype);
                type = (Class<T>) atype;
                jsonObj.remove(classField);
            }
        } catch (ClassNotFoundException ex) {
            String helpMessage = Plugins.classNameSuggestions(
                    PluginRegistry.defaultRegistry(), classInfo.getPluginMap(), stype);
            throw new CodecExceptionLineNumber(new ClassNotFoundException(helpMessage, ex),
                                               jsonObj.getValLineNumber(classField));
        } catch (Exception ex) {
            throw new CodecExceptionLineNumber(ex, jsonObj.getValLineNumber(classField));
        }
        try {
            return decodeJSONInternal(classInfo, type.newInstance(), jsonObj, warnings);
        } catch (InstantiationException ex) {
            CodecExceptionLineNumber celn = translateInstantiationException(type, info);
View Full Code Here

                    throw new CodecExceptionLineNumber(
                            "Could not access the type or the constructor of " +
                            type.getName(), valInfo);
                }
                try {
                    ((JSONCodable) value).fromJSONObject(new JSONObject(oValue.toString()));
                } catch (Exception ex) {
                    throw new CodecExceptionLineNumber(ex, valInfo);
                }
            } else if (field.isArray()) {
                value = decodeArrayInternal(type, value, valInfo, warnings);
            } else if (field.isNative()) {
                if (value.getClass() != type) {
                    if (value.getClass() == Integer.class || value.getClass() == int.class) {
                        // upconvert integer values to long if the field requires long
                        if (type == Long.class || type == long.class || type == AtomicLong.class) {
                            value = new Long(((Integer) value).longValue());
                        }
                        // upconvert integer values to double if the field requires double
                        if (type == Double.class || type == double.class) {
                            value = new Double(((Integer) value));
                        }
                        // downconvert integer to short if the field requires short
                        if (type == Short.class || type == short.class) {
                            value = new Short(((Integer) value).shortValue());
                        }
                    }
                    // downconvert double to float if the field requires a float
                    if ((value.getClass() == double.class || value.getClass() == Double.class) &&
                        (type == float.class || type == Float.class)) {
                        value = new Float(((Double) value));
                    }
                    // upconvert long values to double if the field requires double
                    if ((value.getClass() == long.class || value.getClass() == Long.class) &&
                        (type == double.class || type == Double.class)) {
                        value = new Double(((Integer) value));
                    }
                    // upconvert float values to double if the field requires double
                    if ((value.getClass() == float.class || value.getClass() == Float.class) &&
                        (type == double.class || type == Double.class)) {
                        value = new Double(((Float) value));
                    }
                    if (value.getClass() == String.class) {
                        try {

                            // convert String values to int if the field requires int
                            if (type == Integer.class || type == int.class ||
                                type == AtomicInteger.class) {
                                value = Integer.parseInt((String) value);
                            }

                            // convert String values to long if the field requires long
                            if (type == long.class || type == Long.class ||
                                type == AtomicLong.class) {
                                value = Long.parseLong((String) value);
                            }

                            // convert String values to double if the field requires double
                            if (type == double.class || type == Double.class) {
                                value = Double.parseDouble((String) value);
                            }

                            // convert String values to boolean if the field requires boolean
                            if (type == boolean.class || type == Boolean.class ||
                                type == AtomicBoolean.class) {
                                value = Boolean.parseBoolean((String) value);
                            }
                        } catch (NumberFormatException ex) {
                            if (type == Integer.class || type == int.class ||
                                type == AtomicInteger.class) {
                                throw new CodecExceptionLineNumber(
                                        "cannot convert the string to an integer", valInfo);
                            } else if (type == long.class || type == Long.class ||
                                       type == AtomicLong.class) {
                                throw new CodecExceptionLineNumber(
                                        "cannot convert the string to a long", valInfo);
                            } else if (type == double.class || type == Double.class) {
                                throw new CodecExceptionLineNumber(
                                        "cannot convert the string to a double", valInfo);
                            } else {
                                throw new IllegalStateException(
                                        "unhandled case in the NumberFormatException");
                            }
                        }
                    }
                    if (type == AtomicInteger.class) {
                        value = new AtomicInteger((Integer) value);
                    } else if (type == AtomicLong.class) {
                        value = new AtomicLong((Long) value);

                    } else if (type == AtomicBoolean.class) {
                        value = new AtomicBoolean((Boolean) value);
                    }
                }
                // this space left intentionally blank
            } else if (field.isMap()) {
                Map map;
                try {
                    map = (Map) type.newInstance();
                } catch (Exception ex) {
                    throw new CodecExceptionLineNumber(ex, keyInfo);
                }
                JSONObject jmap = (JSONObject) value;
                Class vc = (Class) field.getGenericTypes()[1];
                boolean va = field.isMapValueArray();
                for (Iterator<String> iter = jmap.keys(); iter.hasNext(); ) {
                    String key = iter.next();
                    if (field.isInterned()) {
                        key = key.intern();
                    }
                    map.put(key, va ?
                                 decodeArrayInternal(vc, jmap.get(key), jmap.getKeyLineNumber(key),
                                                     warnings)
                                    :
                                 decodeObjectInternal(vc, jmap.get(key), jmap.getKeyLineNumber(key),
                                                      warnings));
                }
                value = map;
            } else if (field.isCollection()) {
                Collection col;
View Full Code Here

    }

    /** @deprecated Use {@link #encodeJsonNode(Object)} or {@link CodecJackson} */
    @Deprecated
    public static JSONObject encodeJSON(Object object) throws Exception {
        return new JSONObject(CodecJackson.getDefault().getObjectMapper().writeValueAsString(object));
    }
View Full Code Here

            return encodeArray(object);
        }
        if (object instanceof JSONCodable) {
            return ((JSONCodable) object).toJSONObject();
        }
        JSONObject obj = null;
        boolean lock = object instanceof ConcurrentCodable;
        if (lock) {
            ((ConcurrentCodable) object).encodeLock();
        }
        try {
            if (object instanceof SuperCodable) {
                ((SuperCodable) object).preEncode();
            }
            CodableClassInfo classInfo = Fields.getClassFieldMap(object.getClass());
            if (classInfo.size() == 0 && !(object instanceof Codable)) {
                return object;
            }
            obj = new JSONObject();
            String altType = classInfo.getClassName(object);
            if (altType != null) {
                obj.put(classInfo.getClassField(), altType);
            }
            for (Iterator<CodableFieldInfo> fields = classInfo.values().iterator(); fields.hasNext();) {
                CodableFieldInfo field = fields.next();
                Object value = field.get(object);
                if (value == null || value == JSONObject.NULL || field.isReadOnly()) {
                    continue;
                }
                if (JSONCodable.class.isAssignableFrom(field.getType())) {
                    value = ((JSONCodable) value).toJSONObject();
                    obj.put(field.getName(), value);
                } else if (field.isArray()) {
                    obj.put(field.getName(), encodeArray(value));
                } else if (field.isMap()) {
                    Map<?, ?> map = (Map<?, ?>) value;
                    JSONObject jmap = new JSONObject();
                    for (Entry<?, ?> entry : map.entrySet()) {
                        Object mval = entry.getValue();
                        // TODO fails with null keys
                        jmap.put(entry.getKey().toString(), encodeObject(mval));
                    }
                    obj.put(field.getName(), jmap);
                } else if (field.isCollection()) {
                    JSONArray jarr = new JSONArray();
                    for (Iterator<?> iter = ((Collection<?>) value).iterator(); iter.hasNext();) {
View Full Code Here

    }

    public static <T> T decodeString(T object, String json)
            throws CodecExceptionLineNumber, JSONException {

        return decodeJSON(object, new JSONObject(json));
    }
View Full Code Here

TOP

Related Classes of com.addthis.maljson.JSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.