case class -> json

瀏覽次數:169 次
跳到第一則未讀訊息

Hugo Gävert

未讀,
2013年9月19日 凌晨4:02:092013/9/19
收件者:bije...@googlegroups.com
Hi!

Anybody here? Trying to look into bijection. I'm still trying to understand things. There is bijection from simple types to json. Can this therefore do bijection from case class to json?
Yes, I'm trying to find a way to serialize case classes to json for publishing in scala 2.10. Jerkson was great and simple, but....

-- 
Hugo.

Sam Ritchie

未讀,
2013年9月19日 上午11:26:582013/9/19
收件者:Hugo Gävert、bije...@googlegroups.com
Welcome, Hugo! Not much activity yet on the list, but we're absolutely around.

If you can define a Bijection[YourCaseClass, Tuple] then you'll be able to compose it with JsonInjection.toString and be good to go. The required JsonNodeInjection is built up from the primitive ones, as you noticed.

Here's an example:

https://gist.github.com/sritchie/6625178

import com.twitter.bijection._
import com.twitter.bijection.json.JsonInjection
import com.twitter.bijection.json.JsonNodeInjection._ // implicits required for building up JsonInjections from primitives
 
// Here's some custom case class that we'd like to be able to serialize into Json.
case class Wrapper(x: Int, y: Long)
 
// Now, we define a Bijection from our case class to a tuple.
val wrapperToTuple = Bijection.build[Wrapper, (Int, Long)](Wrapper.unapply(_).get)(Wrapper.tupled(_))
 
// Next we'll compose this with the library-supplied injection from Product -> String provided by JsonInjection:
val wrapperToString: Injection[Wrapper,Stri ng] = wrapperToTuple.andThen(JsonInjection.toString[(Int, Long)])
 
// Forward gets us to string, always:
val json: String = wrapperToString(Wrapper(10, 11L)) // String = [10,11]
 
// Invert returns a try:
wrapperToString.invert(Wrapper(10, 11L)) // scala.util.Try[Wrapper] = Success(Wrapper(10,11))
 


September 19, 2013 1:02 AM
--
You received this message because you are subscribed to the Google Groups "bijection" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bijection+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
Sam Ritchie, Twitter Inc
703.662.1337
@sritchie

Hugo Gävert

未讀,
2013年9月23日 上午10:32:582013/9/23
收件者:bije...@googlegroups.com、Hugo Gävert
Thanks for the answer!

I was looking at the code and to be honest, I kind of got the feeling that this is what it would do. Except I was looking for the more traditional JSON serialization which would serialize the Wrapper(10, 11L) as {x: 10, y: 11}. Which obviously looses the type information in this case (and is not bijection), but it still is what I'm looking for. Yeah, I wasn't looking for bijection :-)
(I'm currently trying out play-json for writing and in future reading JSONs).

-- 
Hugo.

Oscar Boykin

未讀,
2013年9月23日 下午1:46:322013/9/23
收件者:Hugo Gävert、bije...@googlegroups.com
You could do that too with an Injection from Wrapped -> Map[String, JsonNode] -> String

Just a different path than Sam took.

This will be automatic with scala 2.10 as the required base (we can use macros), but for now, you still need to wire it up.



scala> inj2(Wrapped(1, 3L))
res1: String = {"x":1,"y":3}
 
scala> inj2.invert(res1)
res2: scala.util.Try[Wrapped] = Success(Wrapped(1,3))

Oscar Boykin :: @posco :: http://twitter.com/posco
回覆所有人
回覆作者
轉寄
0 則新訊息