Customizing JSON serialization in Play

☆樱花仙子☆ 提交于 2019-12-18 05:23:50

问题


I'm using renderJSON(Object) to return some objects as JSON values, and it's working fine except for one field. Is there an easy way to add in that one field without having to manually create the whole json template?


回答1:


Play uses GSON to build the JSON string. If your one field is a specific object type, then you can easily do this by providing a customised serialisation 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, that you want to work in one way for one, and another way for another, then you may have a little more difficulty.

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);
  }
}



回答2:


Simply do a

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

etc.




回答3:


After evaluating the play framework we hit a stumbling block and decision choice on serializing JSON for an external API. Allot of articles out there suggest using the Lift framework within play which just seem like extra overhead.After trying some of the frameworks / modules with in the play framework a college and myself decided to write a light weight code block that could cater for 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)
    }



  }


来源:https://stackoverflow.com/questions/4522169/customizing-json-serialization-in-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!