gpt4 book ai didi

java - Flexjson 或 Gson : Protect object references after object serialization

转载 作者:行者123 更新时间:2023-12-01 12:33:03 24 4
gpt4 key购买 nike

我使用 flexjson 序列化列表。序列化后,我打印对象 hashcode() 并检查对象是否相等。

问题,操作后对象引用正在更改。我不想要它。

我的代码;

    String sameOfString="sameOfString";
List<Object> list=new ArrayList<Object>();
list.add(sameOfString);
list.add(sameOfString);
list.add("differentString");

for(Object str:list){
System.out.println(str.hashCode());
}
**System.out.println(list.get(0)==list.get(1));**
System.out.println(list.get(0)==list.get(2));
System.out.println("--");
String json= new JSONSerializer().exclude("*.class").deepSerialize(list);
List<String> listDeserilized = (List<String>) new JSONDeserializer().deserialize(json);
for(Object str:listDeserilized){
System.out.println(str.hashCode());
}
**System.out.println(listDeserilized.get(0)==listDeserilized.get(1));**
System.out.println(listDeserilized.get(0)==listDeserilized.get(2));

并输出..

1018069468

1018069468

199675306

true

false

-

1018069468

1018069468

199675306

false

false

如何解决这个问题?

最佳答案

我用 jackson lib 解决了我的问题

@JsonIdentityInfo 注解保护对象关系(换句话说;保护引用)反序列化后,对象位置在内存中不会改变。

最终代码:

package com.maar.projectbuilder;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Firat on 11.9.2014.
*/
public class Testt {
@JsonDeserialize(as=StringClazz.class)
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
@JsonTypeName(value = "StringClazz")
private static class StringClazz{
private String data="";

public StringClazz() {
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}
public static void main(String[] args) throws IOException {
StringClazz someOfString=new StringClazz();
someOfString.setData("someOfString");
StringClazz differentString=new StringClazz();
differentString.setData("differentString");

List<StringClazz> list=new ArrayList<StringClazz>();
list.add(someOfString);
list.add(someOfString);
list.add(differentString);

for(Object str:list){
System.out.println(str.hashCode());
}
System.out.println(list.get(0)==list.get(1));
System.out.println(list.get(0)==list.get(2));
System.out.println("-------");
String json= null;
try {
json = new ObjectMapper().enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL).writeValueAsString(list);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println(json);
List<StringClazz> listDeserilized =new ObjectMapper().enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL).readValue(json, new TypeReference<List<StringClazz>>() {
});
for(Object str:listDeserilized){
System.out.println(str.hashCode());
}
System.out.println(listDeserilized.get(0)==listDeserilized.get(1));
System.out.println(listDeserilized.get(0)==listDeserilized.get(2));
}
}

关于java - Flexjson 或 Gson : Protect object references after object serialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25789769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com