How do I convert a POJO to JSON in PlayN?
Asked Answered
T

0

2

I want to send a string with json content to a REST service. How can I convert a simple POJO to a json string? Normaly I would use Gson to do this:

My class that I want to convert:

public class UserMdl {
   String name;
   String pwHash;
   //Constructur, getter and setter....
}

And my code to do this:

UserMdl userMd = new UserMdl("name", "pwHash");
Gson gson = new Gson();
String json = gson.toJson(userMdl);

However I cannot get gson to work with PlayN in the HTML build. It only works in the Java build. And I cannot figure out how to do this with the PlayN.json() stuff.

Trophoblast answered 28/1, 2012 at 13:7 Comment(3)
did you ever figure this out? i'm running into the same problem trying to deserialize Box2D shapes from JSONVernation
No, i think PlayN currently lacks many important features, due to the missing reflection capabilities of JavaScript. I started to implement a toJson method for every DTO on my client. At least on the server you can use something better. Look at this answer: https://mcmap.net/q/1328905/-collaboration-from-playn-client-with-serverTrophoblast
For communication between client and server I use the same PlayN.json() classes since I already had to write client side code to be able to read the json, especially giving how easy of a task it is.Reimpression

© 2022 - 2024 — McMap. All rights reserved.