1

I need to render objects in JSON and send them to the client but I need to exclude fields like email and password for obvious reasons.

I know play uses GSON (by google?) and you can pass a serializer class when calling the renderJSON() method. However I'm rendering different types of classes at once using a container class:

public class JSONContainer {

    public List<User> userList;

    public List<Toy> toyList;

}

For each class it's possible to make a Serializer class implementing GSON's JsonSerializer<...> method. But if I render a JSONContainer object like this: renderJSON(container) how can I pass the serializer classes to the rendering method?

Or is there maybe an easier/better way to do this?

Tunaki
  • 132,869
  • 46
  • 340
  • 423

1 Answers1

3

Take a look at this post, which gives you a couple of options.

It would appear that the best option is to the @Expose (com.google.gson.annotations.Expose) annotation to mark the fields that you want to be serialised by Gson. You then need to use the GsonBuilder to specifically only include the @Expose fields.

Alternatively, as you have mentioned in your post, you can simply build your serialisations yourself. If you look at this post, it shows how specific class types are registered against the GsonBuilder, so any object of that found as part of the serialisation will use your specific serialiser.

Community
  • 1
  • 1
Codemwnci
  • 54,176
  • 10
  • 96
  • 129