Package com.addthis.maljson

Examples of com.addthis.maljson.JSONArray


            throw new RuntimeException(ex);
        }
    }

    private static Object encodeArray(Object value) throws Exception {
        JSONArray arr = new JSONArray();
        int len = Array.getLength(value);
        for (int i = 0; i < len; i++) {
            arr.put(encodeObject(Array.get(value, i)));
        }
        return arr;
    }
View Full Code Here


                        // 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();) {
                        jarr.put(encodeObject(iter.next()));
                    }
                    obj.put(field.getName(), jarr);
                } else if (field.isCodable()) {
                    obj.put(field.getName(), encodeObject(value));
                } else if (field.isEnum()) {
View Full Code Here

            return null;
        }
        if (object.getClass() != JSONArray.class) {
            throw new CodecExceptionLineNumber(object.toString() + " not an instance of JSONArray for class " + type, info);
        }
        JSONArray array = (JSONArray) object;
        T value = (T) Array.newInstance(type, array.length());
        if (type == byte.class || type == Byte.class) {
            for (int i = 0; i < array.length(); i++) {
                Array.set(value, i, (byte) array.getInt(i));
            }
        } else {
            for (int i = 0; i < array.length(); i++) {
                Object element = decodeObjectInternal(type, array.opt(i), array.getLineNumber(i), warnings);
                try {
                    Array.set(value, i, element);
                } catch (IllegalArgumentException ex) {
                    throw new CodecExceptionLineNumber("Element " + i + " with value " +
                        array.opt(i).toString() + " cannot be converted to " + type.toString(),
                        array.getLineNumber(i));
                }
            }
        }
        return value;
    }
View Full Code Here

                                                      warnings));
                }
                value = map;
            } else if (field.isCollection()) {
                Collection col;
                JSONArray jarr;
                try {
                    col = (Collection) type.newInstance();
                } catch (Exception ex) {
                    throw new CodecExceptionLineNumber(ex, keyInfo);
                }
                try {
                    jarr = (JSONArray) value;
                } catch (Exception ex) {
                    throw new CodecExceptionLineNumber(ex, valInfo);
                }
                Class vc = field.getCollectionClass();
                boolean ar = field.isCollectionArray();
                for (int i = 0; i < jarr.length(); i++) {
                    col.add(ar ?
                            decodeArrayInternal(vc, jarr.get(i), jarr.getLineNumber(i), warnings) :
                            decodeObjectInternal(vc, jarr.get(i), jarr.getLineNumber(i), warnings));
                }
                value = col;
            } else if (field.isEnum()) {
                try {
                    String valString = value.toString();
View Full Code Here

            throw new RuntimeException(ex);
        }
    }

    private static Object encodeArray(Object value) throws Exception {
        JSONArray arr = new JSONArray();
        int len = Array.getLength(value);
        for (int i = 0; i < len; i++) {
            arr.put(encodeObject(Array.get(value, i)));
        }
        return arr;
    }
View Full Code Here

                        // 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();) {
                        jarr.put(encodeObject(iter.next()));
                    }
                    obj.put(field.getName(), jarr);
                } else if (field.isCodable()) {
                    obj.put(field.getName(), encodeObject(value));
                } else if (field.isEnum()) {
View Full Code Here

            return null;
        }
        if (object.getClass() != JSONArray.class) {
            throw new CodecExceptionLineNumber(object.toString() + " not an instance of JSONArray for class " + type, info);
        }
        JSONArray array = (JSONArray) object;
        T value = (T) Array.newInstance(type, array.length());
        if (type == byte.class || type == Byte.class) {
            for (int i = 0; i < array.length(); i++) {
                Array.set(value, i, (byte) array.getInt(i));
            }
        } else {
            for (int i = 0; i < array.length(); i++) {
                Object element = decodeObjectInternal(type, array.opt(i), array.getLineNumber(i), warnings);
                try {
                    Array.set(value, i, element);
                } catch (IllegalArgumentException ex) {
                    throw new CodecExceptionLineNumber("Element " + i + " with value " +
                        array.opt(i).toString() + " cannot be converted to " + type.toString(),
                        array.getLineNumber(i));
                }
            }
        }
        return value;
    }
View Full Code Here

                                    : decodeObjectInternal(vc, jmap.get(key), jmap.getKeyLineNumber(key), warnings));
                }
                value = map;
            } else if (field.isCollection()) {
                Collection col;
                JSONArray jarr;
                try {
                    col = (Collection) type.newInstance();
                } catch (Exception ex) {
                    throw new CodecExceptionLineNumber(ex, keyInfo);
                }
                try {
                    jarr = (JSONArray) value;
                } catch (Exception ex) {
                    throw new CodecExceptionLineNumber(ex, valInfo);
                }
                Class vc = field.getCollectionClass();
                boolean ar = field.isCollectionArray();
                for (int i = 0; i < jarr.length(); i++) {
                    col.add(ar ? decodeArrayInternal(vc, jarr.get(i), jarr.getLineNumber(i), warnings) :
                            decodeObjectInternal(vc, jarr.get(i), jarr.getLineNumber(i), warnings));
                }
                value = col;
            } else if (field.isEnum()) {
                try {
                    String valString = value.toString();
View Full Code Here

TOP

Related Classes of com.addthis.maljson.JSONArray

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.