Search in sources :

Example 1 with Dictionary

use of org.apache.pivot.collections.Dictionary in project pivot by apache.

the class JSON method put.

/**
 * Sets the value at the given path.
 *
 * @param <T> The type of value we're dealing with.
 * @param root The root object.
 * @param path The path to the desired location from the root.
 * @param value The new value to set at the given path.
 * @return The value previously associated with the path.
 */
@SuppressWarnings("unchecked")
public static <T> T put(Object root, String path, T value) {
    Utils.checkNull(root, "root");
    Sequence<String> keys = parse(path);
    if (keys.getLength() == 0) {
        throw new IllegalArgumentException("Path is empty.");
    }
    String key = keys.remove(keys.getLength() - 1, 1).get(0);
    Object parent = get(root, keys);
    if (parent == null) {
        throw new IllegalArgumentException("Invalid path.");
    }
    Map<String, T> adapter = (Map<String, T>) (parent instanceof java.util.Map ? new MapAdapter<>((java.util.Map<String, T>) parent) : (parent instanceof Map ? ((Map<String, T>) parent) : new BeanAdapter(parent)));
    Object previousValue;
    if (adapter.containsKey(key)) {
        previousValue = adapter.put(key, value);
    } else if (parent instanceof Sequence<?>) {
        Sequence<Object> sequence = (Sequence<Object>) parent;
        previousValue = sequence.update(Integer.parseInt(key), value);
    } else if (parent instanceof Dictionary<?, ?>) {
        Dictionary<String, Object> dictionary = (Dictionary<String, Object>) parent;
        previousValue = dictionary.put(key, value);
    } else {
        throw new IllegalArgumentException("Property \"" + key + "\" not found.");
    }
    return (T) previousValue;
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) Sequence(org.apache.pivot.collections.Sequence) BeanAdapter(org.apache.pivot.beans.BeanAdapter) Map(org.apache.pivot.collections.Map)

Example 2 with Dictionary

use of org.apache.pivot.collections.Dictionary in project pivot by apache.

the class JSON method containsKey.

/**
 * Tests the existence of a path in a given object.
 *
 * @param <T> The type of value we're dealing with.
 * @param root The root object.
 * @param path The path to test (from the root).
 * @return <tt>true</tt> if the path exists; <tt>false</tt>, otherwise.
 */
@SuppressWarnings("unchecked")
public static <T> boolean containsKey(Object root, String path) {
    Utils.checkNull(root, "root");
    Sequence<String> keys = parse(path);
    if (keys.getLength() == 0) {
        throw new IllegalArgumentException("Path is empty.");
    }
    String key = keys.remove(keys.getLength() - 1, 1).get(0);
    Object parent = get(root, keys);
    boolean containsKey;
    if (parent == null) {
        containsKey = false;
    } else {
        Map<String, T> adapter = (Map<String, T>) (parent instanceof java.util.Map ? new MapAdapter<>((java.util.Map<String, T>) parent) : (parent instanceof Map ? ((Map<String, T>) parent) : new BeanAdapter(parent)));
        containsKey = adapter.containsKey(key);
        if (!containsKey) {
            if (parent instanceof Sequence<?>) {
                Sequence<Object> sequence = (Sequence<Object>) parent;
                containsKey = (sequence.getLength() > Integer.parseInt(key));
            } else if (parent instanceof Dictionary<?, ?>) {
                Dictionary<String, Object> dictionary = (Dictionary<String, Object>) parent;
                containsKey = dictionary.containsKey(key);
            } else {
                throw new IllegalArgumentException("Property \"" + key + "\" not found.");
            }
        }
    }
    return containsKey;
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) Sequence(org.apache.pivot.collections.Sequence) BeanAdapter(org.apache.pivot.beans.BeanAdapter) Map(org.apache.pivot.collections.Map)

Example 3 with Dictionary

use of org.apache.pivot.collections.Dictionary in project pivot by apache.

the class CSVSerializerTest method testQuotedQuoteReadObject.

@Test
public void testQuotedQuoteReadObject() throws IOException, SerializationException {
    StringBuilder buf = new StringBuilder();
    buf.append("a,\"\"\"b\"\"\",c\r\n");
    StringReader reader = new StringReader(buf.toString());
    CSVSerializer serializer = new CSVSerializer();
    serializer.setKeys("A", "B", "C");
    List<?> result = serializer.readObject(reader);
    @SuppressWarnings("unchecked") Dictionary<String, Object> row = (Dictionary<String, Object>) result.get(0);
    assertEquals("a", row.get("A"));
    assertEquals("\"b\"", row.get("B"));
    assertEquals("c", row.get("C"));
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) StringReader(java.io.StringReader) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Example 4 with Dictionary

use of org.apache.pivot.collections.Dictionary in project pivot by apache.

the class CSVSerializerTest method testInlineKeys.

@Test
public void testInlineKeys() throws IOException, SerializationException {
    StringBuilder buf = new StringBuilder();
    buf.append("A \t, B ,C \n");
    buf.append("a1,b1,c1\n");
    StringReader reader = new StringReader(buf.toString());
    CSVSerializer serializer = new CSVSerializer();
    List<?> result = serializer.readObject(reader);
    @SuppressWarnings("unchecked") Dictionary<String, Object> row = (Dictionary<String, Object>) result.get(0);
    assertEquals(row.get("A"), "a1");
    assertEquals(row.get("B"), "b1");
    assertEquals(row.get("C"), "c1");
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) StringReader(java.io.StringReader) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Example 5 with Dictionary

use of org.apache.pivot.collections.Dictionary in project pivot by apache.

the class CSVSerializerTest method testQuotedCommaReadObject.

@Test
public void testQuotedCommaReadObject() throws IOException, SerializationException {
    StringBuilder buf = new StringBuilder();
    buf.append("a,\",b,\",c\r\n");
    StringReader reader = new StringReader(buf.toString());
    CSVSerializer serializer = new CSVSerializer();
    serializer.setKeys("A", "B", "C");
    List<?> result = serializer.readObject(reader);
    @SuppressWarnings("unchecked") Dictionary<String, Object> row = (Dictionary<String, Object>) result.get(0);
    assertEquals("a", row.get("A"));
    assertEquals(",b,", row.get("B"));
    assertEquals("c", row.get("C"));
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) StringReader(java.io.StringReader) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Aggregations

Dictionary (org.apache.pivot.collections.Dictionary)14 BeanAdapter (org.apache.pivot.beans.BeanAdapter)6 Sequence (org.apache.pivot.collections.Sequence)5 Test (org.junit.Test)5 StringReader (java.io.StringReader)4 CSVSerializer (org.apache.pivot.serialization.CSVSerializer)4 Map (org.apache.pivot.collections.Map)3 URL (java.net.URL)2 SerializationException (org.apache.pivot.serialization.SerializationException)2 Color (java.awt.Color)1 GradientPaint (java.awt.GradientPaint)1 LinearGradientPaint (java.awt.LinearGradientPaint)1 Paint (java.awt.Paint)1 RadialGradientPaint (java.awt.RadialGradientPaint)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1