-1

What serializer is Entity.json(T entity) using to serialize/deserialize objects? Is it somehow possible to use a custom serializer?

In my case the serialization is wrong because my object contains fields with the Guava Optional data type and absent values are returned as {"present":false} instead of null.

birnbaum
  • 4,718
  • 28
  • 37

2 Answers2

2

The JSON serializer isn't specified by JAX-RS, it depends on your configuration. For example, Jersey JAX-RS allows several (https://jersey.java.net/documentation/latest/media.html), including

  • MOXy
  • Java API for JSON Processing (JSON-P)
  • Jackson
  • Jettison

But a better solution is not to use Optional (either Guava or Java 8) for fields. See http://blog.joda.org/2014/11/optional-in-java-se-8.html

My only fear is that Optional will be overused. Please focus on using it as a return type (from methods that perform some useful piece of functionality) Please don't use it as the field of a Java-Bean.

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • Thank you, I configured the Jackson Guava module and it is working fine now. I've read the article you mentioned before but isn't `Optional` reasonable for fields in an API representation class? People that implement my client will have an explicit indication that a certain field can be null (e.g. ErrorCode) this way – birnbaum Oct 01 '15 at 15:56
  • 1
    Also relevant regarding use of Optional (with a response from Brian Goetz, even): http://stackoverflow.com/questions/26327957/should-java-8-getters-return-optional-type – VGR Oct 01 '15 at 17:22
0

Not directly solving your problem. I suggest you use Googles Gson as a parser. It is very flexible and configurable.

Tutorial

It also skips blank fields so the json size is not too large.

Oliver
  • 6,152
  • 2
  • 42
  • 75