开发者

Customizing JSON serialization in Play

开发者 https://www.devze.com 2023-02-01 04:25 出处:网络
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 ma开发者_StackOverflow社

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 ma开发者_StackOverflow社区nually create the whole json template?


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


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.


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



  }
0

精彩评论

暂无评论...
验证码 换一张
取 消