JSON: Serialize Guava Optional

可紊 提交于 2019-12-04 10:42:17

There is indeed a Guava module for Jackson on GitHub, but Optional is not supported (yet). Seems like a rather straightforward serializer/deserializer to implement; the behaviour should be fairly similar to @JsonUnwrapped, so for your simple test the result should be:

{"myString":"testString"}

and for an Optional.absent the serialized form should be:

{"myString":null}

Update: Seemed simple enough so I've just implemented it and pushed it to GitHub. You can get it via the official repo and build from source, or wait for the next official release. Enjoy!

There is a library with Guava Jackson serializers/deserializers here. However there is no one for optional. But it is a really basic class, and you can write one yourself easily following the samples there. Then you can pass the code to Tatu so he can add it to the library and that would be appreciated.

V-Q-A NGUYEN

I have the same the problem and i resolved my problem by using a JsonSerializer and a JsonDeserializer as the How to serialize / deserialize a Option<> class (functional java) with JSON?. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!