Configure JSON serialization on Play

I use renderJSON(Object) to return some objects as JSON values, and it works fine except for one field. Is there an easy way to add this single field without having to manually create the entire json template?

+1
source share
3 answers

Play uses GSON to build a JSON string. If your single field is a specific type of object, you can easily do this by providing custom serialization for that type. See the documentation here

http://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserializ

However, if it is an Integer class, for example, you want to work in one way for one, and in another way for another, then you may have a bit more problems.

Example

 GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(SpecificClass.class, new MySerializer()); private class MySerializer implements JsonSerializer<DateTime> { public JsonElement serialize(SpecificClass src, Type typeOfSrc, JsonSerializationContext context) { String res = "special format of specificClass" return new JsonPrimitive(res); } } 
+6
source

Just follow

 JsonElement elem = new Gson().toJsonTree(yourObject); JsonObject obj = elem.getAsJsonObject(); obj.remove("xxx"); obj.addProperty("xxx", "what you want"); // other stuff ... renderJSON(obj.toString()); 

and etc.

+2
source

After evaluating the playback platform, we will click on the stumbling block and choose a solution for serializing JSON for the external API. Highlighting articles there involves using the Lift framework in the game, which just seems like an unnecessary overhead. After trying some frameworks / modules in the gaming infrastructure, college and I decided to write a block of light weight code that could satisfy our needs,

 case class User ( user_id: Int, user_name: Option[String], password: Option[String], salt: Option[String] ) extends Serializable { def toXml = <user> <user_id>{user_id}</user_id> <user_name>{user_name.getOrElse("")}</user_name> </user> override def toJson = "{" + JSON.key("user_id") + JSON.value(user_id) + ", " + JSON.key("user_name") + JSON.value(user_name) + "}" } class Serializable { def toJson = "" } object JSON { def key(x:String) = value(x) + ": " def value(x:Any):String = { x match { case s:String => "\"" + s + "\"" case y:Some[String] => value(y.getOrElse("")) case i:Int => value(i.toString) case s:Serializable => s.toJson case xs:List[Any] => "[" + xs.map(x => value(x)).reduceLeft(_ + ", " + _) + "]" } } } def searchUserByName(user_name: String) = { (for ( u <- Users if u.user_name.like(("%"+user_name+"%").bind) ) yield u.*) .list .map(User.tupled(_)) } def toXml(users:List[User]) = { <users> { users.map(u => u.toXml) } </users> } def toJson(users:List[User]) = { "[" + users.map(u => u.toJson).reduceLeft(_ + ", " + _) + "]" } 

And from the controller.

 // -- http://localhost:9000/api/users/getUser/xml // -- http://localhost:9000/api/users/getUser/json def getUser(requestType:String) = { db withSession{ val user = Users.byUserName("King.Kong") if(requestType == "xml") { Xml(user.toXml) } else { user.toJson } } } //--- http://localhost:9000/api/users/searchuser/xml //--- http://localhost:9000/api/users/searchuser/json def searchUser(requestType:String) = { db withSession{ val users = Users.searchUserByName("Doctor.Spoc") if(requestType == "xml") { Xml(Users.toXml(users)) } else { val jsonList = Users.toJson(users) Json(jsonList) } } 
0
source

Source: https://habr.com/ru/post/891017/


All Articles