public Object fillData(JSONObject objJson) throws AppErrorException { if (type == null) return null; Object obj = ObjectUtil.newInstance(type); for (JsonAttribute attr : map.values()) { String key = attr.getKey(); if (!objJson.has(key)) { continue; } try { Object value = objJson.get(key); Class<?> typeChild = attr.getFieldType(); // 复杂结构 if (value instanceof JSONObject) { JsonObjectStruct structChild = JsonObjectStructManager.instance().getJsonObjectStruct(typeChild); Object objValue = structChild.fillData((JSONObject) value); ObjectUtil.setValue(attr.getField(), obj, objValue); // 多项内容 } else if (value instanceof JSONArray) { JSONArray array = (JSONArray) value; ArrayList<Object> list = new ArrayList<Object>(); JsonObjectStruct structChild = JsonObjectStructManager.instance().getJsonObjectStruct(typeChild); for (int i = 0; i < array.length(); i++) { JSONObject objValue = array.getJSONObject(i); if (!objValue.keys().hasNext()) { continue; } list.add(structChild.fillData(objValue)); } ObjectUtil.setValue(attr.getField(), obj, list); // 简单数据类型 } else { ObjectUtil.setValue(attr.getField(), obj, value); } } catch (JSONException e) { throw new AppErrorException(this.getClass(), LogConst.STRING_009, e); } } return obj; }
@SuppressWarnings("unchecked") public JSONObject createJsonObject(Object obj) throws AppErrorException { JSONObject objJson = new JSONObject(); if (obj == null) { return objJson; } for (JsonAttribute attr : map.values()) { String key = attr.getKey(); Object value = ObjectUtil.getValue(attr.getField(), obj); Class<?> typeChild = attr.getFieldType(); try { // 单项基本类型 if (attr.isSimpleType() && !attr.isMutiple()) { objJson.put(key, value); continue; // 多项基本类型 } else if (attr.isSimpleType() && attr.isMutiple()) { ArrayList<Object> list = (ArrayList<Object>) value; JSONArray array = new JSONArray(); for (Object eleList : list) { array.put(eleList); } for (int i = 0; i < list.size(); i++) { Object eleJson = list.get(i); array.put(i, eleJson); } objJson.put(key, array); // 多项复杂类型 } else if (attr.isMutiple()) { ArrayList<Object> list = (ArrayList<Object>) value; JSONArray array = new JSONArray(); for (Object eleList : list) { array.put(eleList); } JsonObjectStruct childStruct = JsonObjectStructManager.instance().getJsonObjectStruct(typeChild); for (int i = 0; i < list.size(); i++) { JSONObject eleJson = childStruct.createJsonObject(list.get(i)); array.put(i, eleJson); } objJson.put(key, array); // 单项复杂类型 } else { JsonObjectStruct childStruct = JsonObjectStructManager.instance().getJsonObjectStruct(typeChild); JSONObject jsonChild = childStruct.createJsonObject(value); objJson.put(key, jsonChild); } } catch (JSONException e) { throw new AppErrorException(this.getClass(), LogConst.STRING_010, e); } } return objJson; }
public static JsonObjectStruct parse(Class<?> type) { JsonObjectStruct struct = new JsonObjectStruct(); if (type == null) { return struct; } struct.type = type; List<Field> fields = ObjectUtil.getFields(type); for (Field field : fields) { JsonAttribute attr = JsonAttribute.create(field); String key = attr.getKey(); if (!struct.map.containsKey(key)) { struct.map.put(key, attr); } } return struct; }