Created
May 9, 2016 17:00
-
-
Save seraphr/6ad4e85d9f00d9428f9fec5fd397219a to your computer and use it in GitHub Desktop.
upickle メモ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import upickle.{Js, Api} | |
trait UseUpickle { | |
val mJsonApi: upickle.Api | |
def write[T: mJsonApi.Writer](a: T): String = mJsonApi.write(a) | |
def writeHoge(aHoge: Hoge): String = { | |
import mJsonApi._ | |
mJsonApi.write(aHoge) | |
} | |
} | |
case class Hoge(v: Long, s: String) | |
/** | |
*/ | |
class UseDefaultUpickle extends UseUpickle { | |
override val mJsonApi: Api = upickle.default | |
} | |
class UseMyUpickle extends UseUpickle { | |
object MyApi extends upickle.AttributeTagged { | |
override implicit val LongRW: ReadWriter[Long] = ReadWriter( | |
l => Js.Num(l), | |
{ | |
case Js.Num(d) => d.toLong | |
} | |
) | |
} | |
override val mJsonApi: Api = MyApi | |
} | |
object UpickleApp extends App { | |
def check(aUseUpickle: UseUpickle): Unit = { | |
import aUseUpickle.mJsonApi._ | |
println(aUseUpickle.write(Hoge(10, "string"))) | |
println(aUseUpickle.writeHoge(Hoge(10, "string"))) | |
} | |
val tUseDefault = new UseDefaultUpickle | |
val tUserMyUpickle = new UseMyUpickle | |
println("default") | |
check(tUseDefault) | |
println("my") | |
check(tUserMyUpickle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment