为什么jackson无法序列化jnativehook NativeKeyEvent?

时间:2013-10-11 05:08:44

标签: java json serialization jackson

我正在尝试使用jnativehook序列化 java json library, jackson捕获的关键事件。

我按照说明Can't get a basic Jackson Mixin to work来序列化第三方课程。

然而,它失败并出现以下异常:

The class of event object is : org.jnativehook.keyboard.NativeKeyEvent
com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.jnativehook.GlobalScreen and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.jnativehook.keyboard.NativeKeyEvent["source"])
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:569)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:597)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
    at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:681)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:567)
    at GlobalKeyListenerExample.nativeKeyReleased(GlobalKeyListenerExample.java:45)
    at org.jnativehook.GlobalScreen.processKeyEvent(Unknown Source)
    at org.jnativehook.GlobalScreen$1.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

此文件gist中提供了jar文件的来源和链接。

我试着看看jackson在调试器中序列化的元素是什么。我看到了jnativehook库中没有看到的“source”属性。这是jackson无法序列化的元素。我认为这个元素是在运行时添加的,但不确定原因和时间。

在jackson中是否有任何方法可以指示它不序列化该字段或只序列化几个选择属性?

1 个答案:

答案 0 :(得分:0)

在异常描述中,您有提示如何解决此错误:

mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

你能尝试一下吗?

如果您只想序列化某些属性,也可以为此字段实现自定义序列化程序。