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;
}
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;
}
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"));
}
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");
}
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"));
}
Aggregations