Den 23.06.2011 00:25, skrev Greg Brown:
Do you think adding support for java.util.Map to BeanAdapter has little value?
By the way, the patch is trivial, and it works great :)
BeanAdapter is the wrong place to put this code. BeanAdapter makes a bean
instance look like a Pivot Map. It is not meant to make a java.util.Map look
like a Pivot map. That is the purpose of MapAdapter.
The JSON class would be more appropriate for this kind of check
After looking at it closer (and sleeping a bit :), you are ofcourse
absolutely right as always :) Would you concider applying the attached
patch to add Java Map support to JSON? Local variable name "beanAdapter"
might be a misnomer with my change though.
-- Edvin
Index: core/src/org/apache/pivot/json/JSON.java
===================================================================
--- core/src/org/apache/pivot/json/JSON.java (revision 999698)
+++ core/src/org/apache/pivot/json/JSON.java (revision )
@@ -19,7 +19,9 @@
import org.apache.pivot.beans.BeanAdapter;
import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.Dictionary;
+import org.apache.pivot.collections.Map;
import org.apache.pivot.collections.Sequence;
+import org.apache.pivot.collections.adapter.MapAdapter;
/**
* Contains utility methods for working with JSON or JSON-like data structures.
@@ -71,7 +73,7 @@
String key = keys.get(i);
- BeanAdapter beanAdapter = new BeanAdapter(value);
+ Map beanAdapter = value instanceof java.util.Map ? new
MapAdapter((java.util.Map) value) : new BeanAdapter(value);
if (beanAdapter.containsKey(key)) {
value = beanAdapter.get(key);
} else if (value instanceof Sequence<?>) {
@@ -145,8 +147,9 @@
throw new IllegalArgumentException("Invalid path.");
}
- BeanAdapter beanAdapter = new BeanAdapter(parent);
+ Map beanAdapter = parent instanceof java.util.Map ? new
MapAdapter((java.util.Map) parent) : new BeanAdapter(parent);
+
Object previousValue;
if (beanAdapter.containsKey(key)) {
previousValue = beanAdapter.put(key, value);
@@ -230,7 +233,7 @@
if (parent == null) {
containsKey = false;
} else {
- BeanAdapter beanAdapter = new BeanAdapter(parent);
+ Map beanAdapter = parent instanceof java.util.Map ? new
MapAdapter((java.util.Map) parent) : new BeanAdapter(parent);
containsKey = beanAdapter.containsKey(key);
if (!containsKey) {