<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1512487146270966215</id><updated>2024-09-17T07:45:40.164+09:00</updated><category term="json"/><category term="playframework"/><category term="scala"/><title type='text'>snippet</title><subtitle type='html'>Tech Memorandum.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tech-snippet.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1512487146270966215/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://tech-snippet.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>syuta</name><uri>http://www.blogger.com/profile/12874802705196066921</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1512487146270966215.post-9135217517271335450</id><published>2011-12-11T09:50:00.001+09:00</published><updated>2011-12-11T10:09:00.981+09:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="json"/><category scheme="http://www.blogger.com/atom/ns#" term="playframework"/><category scheme="http://www.blogger.com/atom/ns#" term="scala"/><title type='text'>play-scala using Gson for Scala class</title><content type='html'>before post,&lt;br /&gt;
&lt;a href=&quot;http://tech-snippet.blogspot.com/2011/11/play-scala-using-gson.html&quot;&gt;http://tech-snippet.blogspot.com/2011/11/play-scala-using-gson.html&lt;/a&gt;&lt;br /&gt;
I wrote play-scala sample return in JSON format.&lt;br /&gt;
If you are serialized, that does not work with Scala class.&lt;br /&gt;
&lt;br /&gt;
so,For example, if anorm&#39;s model  has the Option or Pk ,Serialization is not good.&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;  case class User(
    id: Pk[Int],
    name:String
    email: Option[String])
  object User extends Magic[User]
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Then I will use the Custom Serializer, to return any value.&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;  import com.google.gson._

    /**
     * for Pk[Int]
     */
    class PkSerializer extends JsonSerializer[Pk[Int]] {
      def serialize(src: Pk[Int], typeOfSrc: Type, context: JsonSerializationContext): JsonElement = {
        new JsonPrimitive(src.apply);
      }
    }

    /**
     * for Option
     */
    class OptionSerializer extends JsonSerializer[Option[Any]] {
      def serialize(src: Option[Any], typeOfSrc: Type, context: JsonSerializationContext): JsonElement = {
        new JsonPrimitive(src.getOrElse(&quot;none&quot;).toString());
      }
    }
&lt;/pre&gt;Serializer to convert an Option and Pk.&lt;br /&gt;
Pk as it returns its value, Option value if it Some, if None &quot;none&quot; returns a String.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then  serialized using created Custom Serializer.&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt; import play._
 import play.mvc._
 import play.db.anorm._
 import com.google.gson._

  val builder = new GsonBuilder()
  builder.registerTypeAdapter(classOf[play.db.anorm.Pk[Int]], new PkSerializer)
  builder.registerTypeAdapter(classOf[Option[Any]], new OptionSerializer)
  val gson = builder.create()
  Json(gson.toJson(&lt;Userオブジェクトのインスタンス&gt;))
&lt;/pre&gt;&lt;br /&gt;
Any type can be serialized in this way.</content><link rel='replies' type='application/atom+xml' href='http://tech-snippet.blogspot.com/feeds/9135217517271335450/comments/default' title='コメントの投稿'/><link rel='replies' type='text/html' href='http://tech-snippet.blogspot.com/2011/12/play-scala-using-gson-for-scala-class.html#comment-form' title='0 件のコメント'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1512487146270966215/posts/default/9135217517271335450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1512487146270966215/posts/default/9135217517271335450'/><link rel='alternate' type='text/html' href='http://tech-snippet.blogspot.com/2011/12/play-scala-using-gson-for-scala-class.html' title='play-scala using Gson for Scala class'/><author><name>syuta</name><uri>http://www.blogger.com/profile/12874802705196066921</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1512487146270966215.post-3781230866032303452</id><published>2011-11-17T01:10:00.001+09:00</published><updated>2011-11-17T09:27:06.810+09:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="json"/><category scheme="http://www.blogger.com/atom/ns#" term="playframework"/><category scheme="http://www.blogger.com/atom/ns#" term="scala"/><title type='text'>play-scala using Gson</title><content type='html'>I find out how to return data in JSON format using the play-scala.&lt;br /&gt;
In the play-scala around the handle JSON can present a very bad,&lt;br /&gt;
such as lift-json, prefer to work well with other libraries.&lt;br /&gt;
&lt;br /&gt;
so I try to experiment,googl-served to keep a record so the easiest Gson.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;1.create play application&lt;/h3&gt;&lt;pre class=&quot;prettyprint&quot;&gt;$ play new jsonsample --with scala
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;2.get Gson lib&lt;/h3&gt;use dependency management eatures in play.&lt;br /&gt;
&lt;br /&gt;
conf/dependencies.yml&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;# Application dependencies

require:
    - play
    - play -&amp;gt; scala 0.9
    - com.google.code.gson -&amp;gt; gson 1.7.1
&lt;/pre&gt;&lt;br /&gt;
and then,execute deps command.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;$ cd jsonsample
$ play deps
&lt;/pre&gt;available Gson.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;3.create Controller&lt;/h3&gt;To create a Controller that will be returned in the Json format data from the default index method.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;import com.google.gson.Gson

def index = {
    val gson: Gson = new Gson()
    val array = new Array[String](3)
    array(0) = &quot;apple&quot;
    array(1) = &quot;orange&quot;
    array(2) = &quot;banana&quot;
    Json(gson.toJson(array))  
}
&lt;/pre&gt;the array returned in JSON format.&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;[&quot;apple&quot;,&quot;orange&quot;,&quot;banana&quot;]
&lt;/pre&gt;&lt;br /&gt;
Map case.&lt;br /&gt;
※conversion from Scala Map to java.util.Map&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;import com.google.gson.Gson
import scala.collection.JavaConversions._

 def index = {
    val gson: Gson = new Gson()
   var map = Map(&quot;key1&quot;-&amp;gt;&quot;val1&quot;, &quot;key2&quot;-&amp;gt;&quot;val2&quot;)
   val jMap: java.util.Map[String, String] = map
   Json(gson.toJson(jMap))  
  }
&lt;/pre&gt;&lt;br /&gt;
result is &lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;{&quot;key1&quot;:&quot;val1&quot;,&quot;key2&quot;:&quot;val2&quot;}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Object in List case.&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;package controllers

import com.google.gson.Gson
import java.util.List
import java.util.ArrayList
import scala.collection.JavaConversions._

object Application extends Controller {

  def index = {
    val gson: Gson = new Gson()

    val user1 = Person(&quot;syuta&quot;,30)
    val user2 = Person(&quot;taro&quot;,20)

    val s1 = Skill(&quot;Java&quot;,&quot;Program language&quot;)
    val s2 = Skill(&quot;Scala&quot;,&quot;Program language&quot;)
    val s3 = Skill(&quot;MySQL&quot;,&quot;Database&quot;)
      
    val list1 = new ArrayList[Skill]
    list1.add(s1)
    list1.add(s2)
    list1.add(s3)
    
    user1.skills = list1

    val list2 = new ArrayList[Skill]
    list2.add(s2)
    list2.add(s3)

    user2.skills = list2

    val str1 = gson.toJson(user1)
   println(str1)

   val str2 = gson.toJson(user2)
   println(str2)
   
   val resList:List[Person] = new ArrayList[Person]()
    resList.add(user1)
    resList.add(user2)


    Json(gson.toJson(resList))
  }

}

case class Person(var name: String,var age: Int) {
  var skills:List[Skill] = _
}

case class Skill(var name: String,var category:String)
&lt;/pre&gt;&lt;br /&gt;
result is&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;[{&quot;name&quot;:&quot;syuta&quot;,&quot;age&quot;:30,&quot;skills&quot;:[{&quot;name&quot;:&quot;Java&quot;,&quot;category&quot;:&quot;Program language&quot;},{&quot;name&quot;:&quot;Scala&quot;,&quot;category&quot;:&quot;Program language&quot;},{&quot;name&quot;:&quot;MySQL&quot;,&quot;category&quot;:&quot;Database&quot;}]},{&quot;name&quot;:&quot;taro&quot;,&quot;age&quot;:20,&quot;skills&quot;:[{&quot;name&quot;:&quot;Scala&quot;,&quot;category&quot;:&quot;Program language&quot;},{&quot;name&quot;:&quot;MySQL&quot;,&quot;category&quot;:&quot;Database&quot;}]}]
&lt;/pre&gt;no problem case class.&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://tech-snippet.blogspot.com/feeds/3781230866032303452/comments/default' title='コメントの投稿'/><link rel='replies' type='text/html' href='http://tech-snippet.blogspot.com/2011/11/play-scala-using-gson.html#comment-form' title='0 件のコメント'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1512487146270966215/posts/default/3781230866032303452'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1512487146270966215/posts/default/3781230866032303452'/><link rel='alternate' type='text/html' href='http://tech-snippet.blogspot.com/2011/11/play-scala-using-gson.html' title='play-scala using Gson'/><author><name>syuta</name><uri>http://www.blogger.com/profile/12874802705196066921</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1512487146270966215.post-7160738748702050019</id><published>2011-10-31T13:31:00.002+09:00</published><updated>2011-10-31T13:33:56.763+09:00</updated><title type='text'>introduction</title><content type='html'>This blog is The English version of&amp;nbsp;&lt;a href=&quot;http://d.hatena.ne.jp/sy-2010/&quot;&gt;this site&lt;/a&gt;.&lt;br /&gt;
about&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Play framework&lt;/li&gt;
&lt;li&gt;Clojure&lt;/li&gt;
&lt;li&gt;node.js&lt;/li&gt;
&lt;li&gt;Android&lt;/li&gt;
&lt;/ul&gt;
and other tech info.
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://tech-snippet.blogspot.com/feeds/7160738748702050019/comments/default' title='コメントの投稿'/><link rel='replies' type='text/html' href='http://tech-snippet.blogspot.com/2011/10/introduction.html#comment-form' title='0 件のコメント'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1512487146270966215/posts/default/7160738748702050019'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1512487146270966215/posts/default/7160738748702050019'/><link rel='alternate' type='text/html' href='http://tech-snippet.blogspot.com/2011/10/introduction.html' title='introduction'/><author><name>syuta</name><uri>http://www.blogger.com/profile/12874802705196066921</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>