Search in sources :

Example 1 with MapAdapter

use of org.apache.pivot.collections.adapter.MapAdapter in project pivot by apache.

the class JSONSerializer method writeObject.

/**
 * Writes data to a JSON stream.
 *
 * @param object The object to serialize. Must be one of the following
 * types: <ul> <li>pivot.collections.Map</li>
 * <li>pivot.collections.List</li> <li>java.lang.String</li>
 * <li>java.lang.Number</li> <li>java.lang.Boolean</li>
 * <li><tt>null</tt></li> </ul>
 * @param writer The writer to which data will be written.
 * @throws IOException for any errors during the writing process.
 * @throws SerializationException for any formatting errors in the data.
 */
@SuppressWarnings("unchecked")
public void writeObject(Object object, Writer writer) throws IOException, SerializationException {
    Utils.checkNull(writer, "writer");
    if (object == null) {
        writer.append("null");
    } else if (object instanceof String) {
        String string = (String) object;
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0, n = string.length(); i < n; i++) {
            char ci = string.charAt(i);
            switch(ci) {
                case '\t':
                    {
                        stringBuilder.append("\\t");
                        break;
                    }
                case '\n':
                    {
                        stringBuilder.append("\\n");
                        break;
                    }
                case '\r':
                    {
                        stringBuilder.append("\\r");
                        break;
                    }
                case '\f':
                    {
                        stringBuilder.append("\\f");
                        break;
                    }
                case '\b':
                    {
                        stringBuilder.append("\\b");
                        break;
                    }
                case '\\':
                case '\"':
                case '\'':
                    {
                        stringBuilder.append("\\" + ci);
                        break;
                    }
                default:
                    {
                        // and for other character sets if the value is an ASCII control character.
                        if ((charset.name().startsWith("UTF") && !Character.isISOControl(ci)) || (ci > 0x1F && ci != 0x7F && ci <= 0xFF)) {
                            stringBuilder.append(ci);
                        } else {
                            stringBuilder.append("\\u");
                            stringBuilder.append(String.format("%04x", (short) ci));
                        }
                    }
            }
        }
        writer.append("\"" + stringBuilder.toString() + "\"");
    } else if (object instanceof Number) {
        Number number = (Number) object;
        if (number instanceof Float) {
            Float f = (Float) number;
            if (f.isNaN() || f.isInfinite()) {
                throw new SerializationException(number + " is not a valid value.");
            }
        } else if (number instanceof Double) {
            Double d = (Double) number;
            if (d.isNaN() || d.isInfinite()) {
                throw new SerializationException(number + " is not a valid value.");
            }
        }
        writer.append(number.toString());
    } else if (object instanceof Boolean) {
        writer.append(object.toString());
    } else if (object instanceof List<?>) {
        List<Object> list = (List<Object>) object;
        writer.append("[");
        int i = 0;
        for (Object item : list) {
            if (i > 0) {
                writer.append(", ");
            }
            writeObject(item, writer);
            i++;
        }
        writer.append("]");
    } else {
        Map<String, Object> map;
        if (object instanceof Map<?, ?>) {
            map = (Map<String, Object>) object;
        } else if (object instanceof java.util.Map<?, ?>) {
            map = new MapAdapter<>((java.util.Map<String, Object>) object);
        } else {
            map = new BeanAdapter(object, true);
        }
        writer.append("{");
        int i = 0;
        for (String key : map) {
            Object value = map.get(key);
            boolean identifier = true;
            StringBuilder keyStringBuilder = new StringBuilder();
            for (int j = 0, n = key.length(); j < n; j++) {
                char cj = key.charAt(j);
                identifier &= Character.isJavaIdentifierPart(cj);
                if (cj == '"') {
                    keyStringBuilder.append('\\');
                }
                keyStringBuilder.append(cj);
            }
            key = keyStringBuilder.toString();
            if (i > 0) {
                writer.append(", ");
            }
            // Write the key
            if (!identifier || alwaysDelimitMapKeys) {
                writer.append('"');
            }
            writer.append(key);
            if (!identifier || alwaysDelimitMapKeys) {
                writer.append('"');
            }
            writer.append(": ");
            // Write the value
            writeObject(value, writer);
            i++;
        }
        writer.append("}");
    }
    writer.flush();
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) ListenerList(org.apache.pivot.util.ListenerList) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) BeanAdapter(org.apache.pivot.beans.BeanAdapter) Map(org.apache.pivot.collections.Map) HashMap(org.apache.pivot.collections.HashMap) MapAdapter(org.apache.pivot.collections.adapter.MapAdapter)

Example 2 with MapAdapter

use of org.apache.pivot.collections.adapter.MapAdapter in project pivot by apache.

the class PropertiesSerializer method readObject.

/**
 * Reads data from a properties stream.
 *
 * @param inputStream The input stream from which data will be read.
 * @return An instance of {@link Map} containing the data read from the
 * properties file. Both keys and values are strings.
 */
@Override
public Map<?, ?> readObject(InputStream inputStream) throws IOException, SerializationException {
    Utils.checkNull(inputStream, "inputStream");
    Properties properties = new Properties();
    properties.load(inputStream);
    return new MapAdapter<>(properties);
}
Also used : Properties(java.util.Properties) MapAdapter(org.apache.pivot.collections.adapter.MapAdapter)

Aggregations

MapAdapter (org.apache.pivot.collections.adapter.MapAdapter)2 Properties (java.util.Properties)1 BeanAdapter (org.apache.pivot.beans.BeanAdapter)1 ArrayList (org.apache.pivot.collections.ArrayList)1 HashMap (org.apache.pivot.collections.HashMap)1 List (org.apache.pivot.collections.List)1 Map (org.apache.pivot.collections.Map)1 SerializationException (org.apache.pivot.serialization.SerializationException)1 ListenerList (org.apache.pivot.util.ListenerList)1