这是我将 JSON 和类似 JSON 的文档“本地”存储到 GAE 数据存储区的想法:
protected void createEntity(Key parent, Map obj){
try {
Entity e = new Entity(
parent == null ? createKey(_kind, (String) obj.get(OBJECT_ID)) : parent);
Iterator it = obj.keySet().iterator();
while (it.hasNext()){
String key = (String) it.next();
if (obj.get(key) == null){
e.setProperty(key, null);
} else if (obj.get(key) instanceof String) {
setProperty(e, key, obj.get(key));
} else if(obj.get(key) instanceof Number) {
setProperty(e, key, obj.get(key));
} else if(obj.get(key) instanceof Boolean) {
setProperty(e, key, obj.get(key));
} else if(obj.get(key) instanceof List) {
// Problem area, right way to store a list?
// List may contain JSONObject too!
} else if(obj.get(key) instanceof Map){
// Update: Ooops, this cause StackOverFlow error!
Key pKey = createKey(e.getKey(), _kind, (String) obj.get(key));
e.setProperty(key, pKey.toString()); // not sure?
createEntity(pKey, obj);
}
}
_ds.put(e);
} catch (ConcurrentModificationException e){
} catch (Exception e) {
// TODO: handle exception
}
}
该方法是递归放置的,其中 GAE 支持的非集合属性直接存储到实体的属性中。然后Map
使用具有当前实体键的父键创建新实体,依此类推。
我的 JSON 接口支持的类型的基础是:http ://code.google.com/p/json-simple/
我现在遇到的问题是我不确定如何处理java.util.List
,以及如何以 Map 之类的方式存储它。
关于如何实现这一目标的任何建议?