Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
upickle メモ
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
You can’t perform that action at this time.