0

I'm struggling to convert a generated from a Primefaces PickList into JSON.

   Gson gson = new Gson();

    System.out.println(gameTaskHotspots.getTarget().toString());

    Type type = new TypeToken<List<GameTaskHotspot>>(){}.getType();

    if(gameTaskHotspots.getTarget().size() > 0){
        List<GameTaskHotspot> list = gameTaskHotspots.getTarget();
        selectedTask.input = gson.toJson(list);
    }


    System.out.println(selectedTask.input);

It's outputing the bean names in the JSON string, i.e.

The first output is

[co.beek.pano.model.beans.GameTaskHotspot@4697b22e, co.beek.pano.model.beans.GameTaskHotspot@2b7ab42b, co.beek.pano.model.beans.GameTaskHotspot@344f3665]

The Second output is

["co.beek.pano.model.beans.GameTaskHotspot@4697b22e","co.beek.pano.model.beans.GameTaskHotspot@2b7ab42b","co.beek.pano.model.beans.GameTaskHotspot@344f3665"]

Like it's not reading the objects properly. I've tried using the type to give Gson the object, and I get this error:

javax.el.ELException: /views/guide/admin.xhtml @246,147 actionListener="#{GuideViewAdminController.saveSelectedTask()}": java.lang.IllegalArgumentException: Can not set java.lang.String field co.beek.pano.model.beans.GameTaskHotspot.id to java.lang.String

Incidently I use this to convert the Json into a List of Objects and it works great

   GameTaskHotspot[] existing = gson.fromJson(type == 0 ? selectedTask.input : selectedTask.output, GameTaskHotspot[].class);
beek
  • 3,522
  • 8
  • 33
  • 86
  • 1
    Needed to use a converter http://stackoverflow.com/questions/3621251/how-to-write-a-custom-converter-when-working-with-primefaces-components-that-co – beek Sep 15 '15 at 09:43

1 Answers1

0

I had to use a converter to convert the object into JSON

How to write a custom converter for <p:pickList>

Adding this to the PickList worked.

Community
  • 1
  • 1
beek
  • 3,522
  • 8
  • 33
  • 86