--
Von: Johannes Rudolph <johannes...@googlemail.com>
An: spray...@googlegroups.com
Gesendet: 10:39 Donnerstag, 15.November 2012
Betreff: Re: [spray-user] Spray-Json: unmarshing of only a part of a Json document?
--
Hi,
>________________________________
> Von: Johannes Rudolph <johannes...@googlemail.com>
>An: spray...@googlegroups.com
>Gesendet: 14:28 Donnerstag, 15.November 2012
>Betreff: Re: [spray-user] Spray-Json: unmarshing of only a part of a Json document?
>
>Hi Markus,
>
>On Thu, Nov 15, 2012 at 2:15 PM, Markus Jais <marku...@yahoo.de> wrote:
>> that looks great. It works. I hope this get's integrated in spray-json.
>
>It will be integrated.
cool!
>
>> I also have to convert the whole JSON document.
>> Is there a way to convert the whole document with all the nested objects? I
>> tried several different things but just can't get it running.
>
>Ok, that's reasonable and possible. Btw. it would help if you would
>post exact error messages and/or compiling code.
Sorry, forgot that:
Here is my attempt. Don't know how to implement the read/write methods.
object bird extends App {
case class Bird(name: String, color: String)
case class Birds(german: String, birds: List[Bird])
case class Ecosystem(ecoSystem: String, birds: Birds)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit object EcosystemJsonFormat extends RootJsonFormat[Ecosystem] {
implicit val birdFormat = jsonFormat2(Bird)
implicit val birdsFormat = jsonFormat2(Birds)
implicit val ecosystemFormat = jsonFormat2(Ecosystem)
def write(es: Ecosystem) = {
//JsArray(JsString(es.ecoSystem), JsObject(es.birds))
// ??? how to work with the nested objects like Birds and Bird?
}
def read(value: JsValue) = {
// ????
}
}
}
}
import MyJsonProtocol._
val b1 = Bird("amsel", "braun")
val b2 = Bird("Bussard", "gelb")
val birds = Birds("vogel", List(b1, b2))
val es = Ecosystem("garden", birds)
val json = es.toJson
println(json)
}
>
>> But I can't find the DefaultJsonFormat trait anywhere with the latest
>> spray-json.
>
>Strange. It's hasn't moved.
Can't find it in the latest code on github. Yes, this is strange :-)
>
>I updated the example [1] to extract the complete structure. You can
>clone the complete gist and run it with sbt as a starting point.
Looks great but I cannot compile it in my project. I use Scala 2.10 RC2. Is this a problem?
sbt cannot download the json-lenses jar. Do I need an additional repository to add to my resolvers in the sbt file?
Markus
>
>HTH
>
>--
>Johannes
>
>[1] https://gist.github.com/4077644
>
>-----------------------------------------------
>Johannes Rudolph
>http://virtual-void.net
>
>--
>
>
>
>
>
thanks for the really quick answer.
I got it working now. I mixed up something while copying the gist.
All I had to change was
val ecosystem = json.as[Ecosystem]
to
val ecosystem = json.convertTo[Ecosystem]
I think "as" is part of the json-lenses project (?).
And you are of course right about the DefaultJsonProtocol.scala.
I confused it with DefaultJsonFormat (not "Protocol").
I just read the whole json-lenses docs. Really an awesome small library. I am sure I will use it in all my
projects where I need json stuff once it works with 2.10 RC2 or newer (I can't use an older Scala version at the moment).
Cheers,
Markus
----- Ursprüngliche Message -----
> Von: Johannes Rudolph <johannes...@googlemail.com>
> An: spray...@googlegroups.com
> CC:
> Gesendet: 14:59 Donnerstag, 15.November 2012
> Betreff: Re: [spray-user] Spray-Json: unmarshing of only a part of a Json document?
>
> --
>