com.foxinmy.weixin4j.xml.ListsuffixResultSerializer Maven / Gradle / Ivy
package com.foxinmy.weixin4j.xml;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.NameFilter;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 对 后缀为_$n 的 xml节点序列化
*
* @className ListsuffixResultSerializer
* @author jinyu([email protected])
* @date 2015年3月24日
* @since JDK 1.6
* @see
*/
public class ListsuffixResultSerializer {
/**
* 序列化为json
*
* @param object
* @return json
*/
public static JSONObject serializeToJSON(Object object) {
JSONObject result = (JSONObject) JSON.toJSON(object);
Map listsuffixFields = ListsuffixResultDeserializer
.getListsuffixFields(object.getClass());
if (!listsuffixFields.isEmpty()) {
JSONField jsonField = null;
Object value = null;
for (Field field : listsuffixFields.keySet()) {
jsonField = field.getAnnotation(JSONField.class);
if (jsonField != null
&& StringUtil.isNotBlank(jsonField.name())) {
result.remove(jsonField.name());
} else {
result.remove(field.getName());
}
try {
field.setAccessible(true);
value = field.get(object);
} catch (Exception e) {
;//
}
if (value != null && value instanceof List) {
result.putAll(listsuffixConvertMap((List>) value));
}
}
}
return result;
}
/**
* list对象转换为map的$n形式
*
* @param listsuffix
* @return
*/
public static Map listsuffixConvertMap(List> listsuffix) {
Map listMap = new HashMap();
if (listsuffix != null && !listsuffix.isEmpty()) {
for (int i = 0; i < listsuffix.size(); i++) {
listMap.putAll(JSON.parseObject(JSON.toJSONString(
listsuffix.get(i), new ListsuffixEndNameFilter(i)),
new TypeReference
© 2015 - 2024 Weber Informatics LLC | Privacy Policy