fastjson 自身在自身的map集合属性里,序列化后,反序列化回来有问题

wooyq4lh  于 2021-11-27  发布在  Java
关注(0)|答案(4)|浏览(324)
  • 问题描述:当一个序列化对象的一个object属性里存放了一个map,map中存放了序列化对象本身,反序列化的时候会出问题。引用本身的key-value,value变成了当前的map
  • 我们的用例

Map<String,Object> map=new HashMap();
map.put("roleResource类型", roleResource)
map.put("accountDO类型", accountDO);
map.put("account类型",account);

account.setObj(map);

byte[] bytes = JSON.toJSONString(account, SerializerFeature.WriteClassName).getBytes("utf-8");
AccountaccountDeserialize= JSON.parseObject(new String(bytes, "utf-8"), Account.class);

  • account为序列化对象实体类本身。
  • obj为object类型

图片描述问题

wr98u20j

wr98u20j1#

更新到1.2.16 发现反序列化回来 "account类型"的value 成了 jsonObject(size=0)

t1qtbnec

t1qtbnec2#

用最新版本1.2.21试试看

3lxsmp7m

3lxsmp7m3#

1.2.21这个版本没解决,而且又发现一个bug。
图片解释地址

新bug ,自身一个object属性储存自身会丢失,反序列化回来为null。(obj为Object 类型)
图片解释地址

`/**

  • Created by ZD on 2016/7/11.

*/
public class Account extends Base implements Serializable {

private Object obj;`

kdfy810k

kdfy810k4#

写了一个示例

`package com.yuantu.other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**

  • Created by zilue on 2016/9/28.

*/
public class Other {

public static void main(String[] args) throws UnsupportedEncodingException {

Aob aob=new Aob();
 aob.setPint(123);
 aob.setPlon(123L);
 aob.setPstr("string");

 Map<String,Object> map=new HashMap();
 map.put("Aob类型", aob);
 aob.setPobj(map);

 byte[] bytes = JSON.toJSONString(aob, SerializerFeature.WriteClassName).getBytes("utf-8");
 Aob aobDeserialize = JSON.parseObject(new String(bytes, "utf-8"), Aob.class);

 //        请在此处打断点检查数据
 System.out.println(aobDeserialize);

}

}
class Aob{
private String pstr;
private long plon;
private Integer pint;
private Object pobj;

public Aob() {
}

public String getPstr() {
    return pstr;
}

public void setPstr(String pstr) {
    this.pstr = pstr;
}

public long getPlon() {
    return plon;
}

public void setPlon(long plon) {
    this.plon = plon;
}

public Integer getPint() {
    return pint;
}

public void setPint(Integer pint) {
    this.pint = pint;
}

public Object getPobj() {
    return pobj;
}

public void setPobj(Object pobj) {
    this.pobj = pobj;
}

}

class AoC extends Aob{
private String cstr;
private String cobj;

public AoC() {
}

public String getCstr() {
    return cstr;
}

public void setCstr(String cstr) {
    this.cstr = cstr;
}

public String getCobj() {
    return cobj;
}

public void setCobj(String cobj) {
    this.cobj = cobj;
}

}`

相关问题