- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 Kryo 时遇到了一个奇怪的问题。我可以很好地序列化一个泛型类(例如 List)。我什至可以序列化一个没有通用字段的通用类。但是,如果我用通用字段序列化一个通用类,它就会失败。一个例子应该能说明问题。
这是我要序列化的类:
import java.util.ArrayList;
public class GenericClass<T>
{
private final ArrayList<T> list;
public GenericClass()
{
list = new ArrayList<T>();
}
}
这是测试它的代码。第二次调用 writeClassAndObject() 抛出异常。 ArrayList 的写入只是为了证明它工作得很好。
public void testWhySoDumb()
{
File s = Environment.getExternalStorageDirectory();
String dir = s.getAbsolutePath() + "/pagetest";
String fileLocation = dir + "/filetest";
new File(dir).mkdirs();
Output output = null;
Kryo kryo = new Kryo();
kryo.setAsmEnabled(true);
kryo.setDefaultSerializer(CompatibleFieldSerializer.class);
// Test writing ArrayList (this works)
List<Integer> arrayList = new ArrayList<Integer>();
try
{
output = new Output(new FileOutputStream(fileLocation));
kryo.writeClassAndObject(output, arrayList);
}
catch (Exception e)
{
System.out.println(e.getMessage()); // No problem here
}
finally
{
if (output != null)
{
output.close();
}
}
// Test writing GenericClass (this does NOT work)
GenericClass<Integer> genericClass = new GenericClass<Integer>();
try
{
output = new Output(new FileOutputStream(fileLocation));
kryo.writeClassAndObject(output, genericClass); // throws exception!!! <------
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
finally
{
if (output != null)
{
output.close();
}
}
}
我收到以下异常:
com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: type cannot be null.
Serialization trace:
list (com.myapp.page.history.GenericClass)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:82)
at com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer.write(CompatibleFieldSerializer.java:42)
at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:624)
at com.myapp.page.history.TestGenerics.testWhySoDumb(TestGenerics.java:57)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
Caused by: java.lang.IllegalArgumentException: type cannot be null.
at com.esotericsoftware.kryo.Kryo.isFinal(Kryo.java:1135)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.setGenerics(CollectionSerializer.java:60)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:60)
... 15 more
非常奇怪的是,如果我将“列表”的类型从“ArrayList”更改为“T”,它工作正常。 Kryo 似乎不知道如何处理 ArrayList 字段。我正在使用 Kryo 版本 3.0.0。
谢谢!
最佳答案
不知道你是否还有这个问题。我可能会认为这是版本问题或特定于 Android 的问题,因为这应该适用于当前版本。
Kryo kryo = new Kryo();
Output output = new Output(new FileOutputStream("fileLocation"));
kryo.writeObject(output, new GenericClass<String>());
关于android - 无法在 Kryo 中使用泛型字段序列化泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27909061/
WARN: [kryo] Unable to load class slade.core.Child with kryo's ClassLoader. Retrying with current...
我正在为我的 pojo 类使用 Flink 和自定义 kryo 类。但得到 Caused by: java.lang.NullPointerException at MyTreeSeriali
我需要通过添加两个新参数来修改一个类。这个类是用 Kryo 序列化的。 每次我停止流时,我目前都将与此类相关的信息保存为 RDD。 当我重新启动流时,我会加载我之前保留的信息,并使用它们在我停止和重新
我正在尝试将 kryo 序列化用于: kryo.setRegistrationRequired(true); 我不断收到以下错误消息,说某个类未注册: java.lang.IllegalArgumen
我正在使用 Kryo 进行序列化/反序列化,并且没有事先注册类(我正在研究这一点)。也就是说,在反序列化时,我遇到了异常: 无法使用 kryo 的 ClassLoader 加载类 shell.api.
我发现了两种使用 Kryo 进行读写的通用方法,我在想其中一种是更好还是更差,或者它们是否完全相同。 选项 1 - 使用 kryo 函数 writeClassAndObject (readClassA
我直接使用 Kryo IO 来执行我自己的字符串、长整型和 double 型的低级原始序列化。 我想知道的是,Kryo IO 是否有任何方法可以在读回序列化字节时自动检测它们的原始数据类型? 如果我有
我将 JSON 存储在表中(实际上是 Cassandra 列族),如下所示: ____________________________________________________________
我正在尝试使用 Kryo 序列化某些对象的列表列表(自定义类:List>)。 list2D; // List> which is already produced. Kryo k1 = new Kry
我正在尝试使用 Kryo 库将任何给定对象转换为 byteArray 并存储在数据存储或队列中供以后使用。但是是否可以序列化任何给定的对象,或者只能转换实现可序列化接口(interface)的对象。
实际项目中,我们经常需要使用序列化工具来存储和传输对象。目前用得比较多的序列化工具有:jackson、fastjson、kryo、protostuff、fst 等,本文将简单对比这几款工具序列化和反序
你如何使用 反序列化不可变集合Kryo ?除了我所做的事情之外,我还需要注册一些东西吗? 这是我的示例代码 import com.esotericsoftware.kryo.Kryo import c
在使用带有以下代码的Apache Flink时: DataStream> result = source.window(Time.of(1, TimeUnit.SECONDS)).mapWindow(
我正在尝试从此 link 运行 Chill-Scala 的示例代码 我有两个虚拟案例类 一个是用户 @SerialVersionUID(1) case class User(name :String,
我正在尝试了解以下错误,并且正在客户端 ode 中运行。 org.apache.spark.SparkException: Kryo serialization failed: Buffer ove
我有一段 Spark 代码可以在 Spark 1.3 上运行,但当我将它移至 Spark 1.5.2 时失败(集群升级超出我的控制)。失败如下: Caused by: java.io.NotSeria
通过使用 Kryo 3,每次序列化/反序列化根对象(流)时都会创建一个 Kryo 3 实例。 我想知道我可以使用什么重用概念。 从源代码来看,Kryo 实例可能仅以单线程方式使用,并且由于它使用引用,
我正在尝试使用 kryo 序列化和反序列化为二进制文件。我想我有序列化工作,但我似乎无法反序列化。下面是我正在处理的代码,但最终我想存储一个 byte[] 并稍后再次读取它。该文档仅显示如何使用文件执
我正在寻找 Kryo 自定义序列化和反序列化示例。如何检查Kryo读写函数的正确性。 我写了一些代码来检查,但它返回异常。任何帮助将不胜感激。提前致谢。 import com.esotericsoft
对于个人项目,我必须使用 Kryo-Framework 在 Java 中序列化 Collections.sychronizedMap。我绑定(bind)到该框架,因为还有其他依赖项需要它。我目前的尝试
我是一名优秀的程序员,十分优秀!