我们使用XStream将对象序列化为JSON,反之亦然。
我们像这样启动xStream
XStream xStream = new XStream(new JettisonMappedXmlDriver(new Configuration(), false));
xStream.ignoreUnknownElements();
xStream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
我们有测试类
public static class TestWOWithBI implements Serializable{
private static final long serialVersionUID = -4720678317857471031L;
private transient String customerNickname;
private transient String customerUuid;
private transient BigInteger discussionId;
private transient String message;
public TestWOWithBI(String customerNickname, String customerUuid, BigInteger discussionId, String message){
this.customerNickname = customerNickname;
this.customerUuid = customerUuid;
this.discussionId = discussionId;
this.message = message;
}
private final void writeObject(final ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(customerNickname);
out.writeObject(customerUuid);
out.writeObject(discussionId);
out.writeObject(message);
}
private final void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException{
in.defaultReadObject();
customerNickname = (String) in.readObject();
customerUuid = (String) in.readObject();
discussionId = (BigInteger) in.readObject();
message = (String) in.readObject();
}
}
序列化后,它看起来像这样:
{ “somethere.ObjectToJSONSerializerTest $ TestWOWithBI”:{ “@serialization”:“custom”, “somethere.ObjectToJSONSerializerTest $ TestWOWithBI”:{ “默认”:“”, “string”:[“name”, 的 “uuid”, “信息”], “big-int”:1 } } }
并且反序列化因类强制转换而失败。它是1.3.1和1.4.7版本。看起来像我的错误,但可能是某些设置在哪里?
UPD: 看起来像org.codehaus.jettison.mapped.MappedXMLStreamWriter.JSONPropertyObject #withProperty
if(old != null) {
JSONArray values;
// Convert an existing property to an array
// and append to the array
if (old instanceof JSONArray) {
values = (JSONArray)old;
} else {
values = new JSONArray();
values.put(old);
}
values.put(value);
object.put(property.getKey(), values);
} else if(getSerializedAsArrays().contains(property.getKey())) {
JSONArray values = new JSONArray();
values.put(value);
object.put(property.getKey(), values);
} else {
// Add the property directly.
object.put(property.getKey(), value);
}
它只是对相同类型的元素进行分组。