gpt4 book ai didi

Java - 反序列化包含原始类型的哈希表时出错

转载 作者:行者123 更新时间:2023-12-01 14:18:21 25 4
gpt4 key购买 nike

我已经连载了HashTable<String,Object>使用 ObjectOutputStream 的对象。序列化对象时,没有出现异常,但反序列化时,出现以下异常:

Exception in thread "main" java.io.InvalidClassException: java.lang.Long; local class
incompatible: stream classdesc serialVersionUID = 4290774032661291999, local class
serialVersionUID = 4290774380558885855

当我删除 HashTable 中的所有键时,我不再收到错误消息其值不是 String (我删除的所有键/值对都有一个原始类型作为它们的值)。

什么可能导致此错误?

更新 - 这是代码

public static String serialize(Quiz quiz) throws IOException{
HashMap<String,Object> quizData = new HashMap<String,Object>();
quizData.put("version", 0); //int
quizData.put("name", quiz.getName()); //String
quizData.put("desc", quiz.getDesc()); //String
quizData.put("timelimitType", quiz.getTimelimitType()); //String
quizData.put("timelimit", quiz.getTimelimit()); //long
ArrayList<String> serializedQuestionsData = new ArrayList<String>();
for (Question question : quiz.getQuestions())
serializedQuestionsData.add(Question.serialize(question));
quizData.put("questions", serializedQuestionsData.toArray(new String[0])); //String[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos;
try { oos = new ObjectOutputStream(baos); } catch (IOException error){ throw error; }
try { oos.writeObject(quizData); } catch (IOException error){ throw error; }
return baos.toString();
}
@SuppressWarnings("unchecked")
public static Quiz deserialize(String serializedQuizData) throws IOException, ClassNotFoundException{
ByteArrayInputStream bais = new ByteArrayInputStream(serializedQuizData.getBytes());
ObjectInputStream ois;
try { ois = new ObjectInputStream(bais); } catch (IOException error){ throw error; }
HashMap<String,Object> quizData;
// Exception occurs on the following line!!
try { quizData = (HashMap<String,Object>) ois.readObject(); } catch (ClassNotFoundException error){ throw error; }
Quiz quiz;
if ((int) quizData.get("version") == 0){
quiz = new Quiz((String) quizData.get("name"),
(String) quizData.get("desc"),
(String) quizData.get("timelimitType"),
(long) quizData.get("timelimit"));
for (String serializedQuestionData : (String[]) quizData.get("questions"))
quiz.addQuestion(Question.deserialize(serializedQuestionData));
} else {
throw new UnsupportedOperationException("Unsupported version: \"" + quizData.get("version") + "\"");
}
return quiz;
}

最佳答案

问题在于您正在使用 toString() 将字节数组输出流转换为字符串。 toString() 方法只是使用平台默认编码将字节(根本不表示字符,而是纯粹的二进制数据)转换为字符串。因此,这是一个有损操作,因为您的平台默认编码没有为每个可能的字节提供有效字符。

您不应该使用 String 来保存二进制数据。字符串包含字符。如果您确实需要字符串,请使用十六进制或 Base64 编码器对字节数组进行编码。否则,只需使用字节数组来保存二进制数据:

public static byte[] serialize(Quiz quiz) throws IOException{
...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
...
return baos.toByteArray();
}

@SuppressWarnings("unchecked")
public static Quiz deserialize(byte[] serializedQuizData) throws IOException, ClassNotFoundException{
ByteArrayInputStream bais = new ByteArrayInputStream(serializedQuizData);
...
return quiz;
}

关于Java - 反序列化包含原始类型的哈希表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17905448/

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