简体   繁体   English

为什么杰克逊无法序列化jnativehook NativeKeyEvent?

[英]Why jackson fails to serialize jnativehook NativeKeyEvent?

I am trying to serialize the keyevent captured by jnativehook using java json library, jackson . 我正在尝试使用java json库jackson序列化jnativehook捕获的关键事件

I followed this instructions Can't get a basic Jackson Mixin to work to serialize 3rd party class. 我遵循了此说明, 无法获得基本的Jackson Mixin来序列化3rd party类。

However it fails with following exception: 但是,它失败并带有以下异常:

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)

The source and links of jar files are available in this gist . jar文件的源代码和链接在本要点中可用。

I tried to see what are the elements jackson is serializing in the debugger. 我试图看看杰克逊在调试器中要序列化的元素是什么。 I see "source" attribute which I don't see in the jnativehook library. 我看到了jnativehook库中没有的“源”属性。 This is the element where jackson fails to serialize. 这是杰克逊无法序列化的元素。 I think this element is added at runtime, but not sure why and when. 我认为此元素是在运行时添加的,但不确定为什么以及何时添加。

Is there any way in jackson so that I can instruct it to not serialize this field OR only serialize few select attributes? 杰克逊有什么办法让我指示它不要序列化该字段,或者只序列化一些选择属性吗?

In exception description you have tip how to solve this bug: 在异常描述中,您技巧提示如何解决此错误:

mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

Could you try with this? 你可以尝试一下吗?

You can also implement custom serializer for this field, if you want to serialize only some properties. 如果只想序列化某些属性,则还可以为该字段实现自定义序列化程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM