Questions tagged [spray-json]

Relates to the spray-json Scala library.

spray-json is a lightweight, clean and simple implementation in .

339 questions
8
votes
2 answers

Spray-Json: How to parse a Json Array?

I'm new to the Spray-Json API and I'm trying to parse a Json response from the Docker REST API. There is a clean example of the usage of Spray-Json to parse this Google Map Json response : { "results" : [ { "elevation" :…
abronan
  • 3,309
  • 4
  • 29
  • 38
7
votes
2 answers

spray-json failing for Seq of Eithers

Not sure this is a bug, but the following demo fails on the final cases: import spray.json._ import DefaultJsonProtocol._ object SprayTest { 1.toJson "".toJson (Left(1): Either[Int, String]).toJson (Right(""): Either[Int, String]).toJson …
acjay
  • 34,571
  • 6
  • 57
  • 100
7
votes
3 answers

How to unmarshall `text/plain` as JSON in Akka HTTP

I'm working with a legacy HTTP API (that I can't change) that responds with JSON in the body, but gives a Content-Type: text/plain; charset=utf-8 header. I am attempting to unmarshall that HTTP body as JSON, but I get the following exception:…
David van Geest
  • 1,957
  • 1
  • 20
  • 19
7
votes
2 answers

spray json implicit UUID conversion

I have a User model case class User(name: String, email: String, password: Option[String] = None, key: Option[UUID] = None) With a spray-json marshaller object UserJsonSupport extends DefaultJsonProtocol with SprayJsonSupport { implicit val…
GoldenFish
  • 217
  • 2
  • 9
7
votes
2 answers

Parsing a simple array with Spray-json

I'm trying (and failing) to get my head around how spray-json converts json feeds into objects. If I have a simple key -> value json feed then it seems to work ok but the data I want to read comes in a list like this: [{ "name": "John", …
pogo
  • 2,287
  • 2
  • 25
  • 36
7
votes
1 answer

How to unmarshal POST params and JSON body in a single route?

I have this route: val routes = pathPrefix("api") { path("ElevationService" / DoubleNumber / DoubleNumber) { (long, lat) => post { requestContext => println(long, lat) } } } This works…
Stefan Konno
  • 1,337
  • 2
  • 16
  • 28
7
votes
3 answers

Spray won't convert my case class to json and expect a spray.httpx.marshalling.ToResponseMarshallable

I'm trying to reprocude this or this, but I keep getting an error I am not able to fix... First of all, here are my dependencies: compile 'io.spray:spray-can_2.11:1.3.1' compile 'io.spray:spray-routing_2.11:1.3.1', compile…
ydemartino
  • 242
  • 5
  • 10
6
votes
2 answers

spray-json can't find JsonReader for type List[T]

I'm creating custom json readers for case classes but it can't find implicit JsonReader type class for List[T] which is used in other case class. When I checked DefaultJsonProtocol, it has implicit format for collections already; implicit def…
Fatih Donmez
  • 4,319
  • 3
  • 33
  • 45
6
votes
3 answers

spray Collection ToResponseMarshallable

I am trying to return a List from my complete directive in spray-routing. complete { List("hello") } However, I am getting an error - Expression of type List[String] doesn't conform to expected type ToResponseMarshallable I am getting the same…
adefor
  • 299
  • 2
  • 9
6
votes
3 answers

Implicit jsonFormat for case class with varargs

I have a case class containing varargs, with an implicit jsonFormat as follows: import spray.json._ case class Colors(name: String*) object MyJsonProtocol extends DefaultJsonProtocol { implicit val colorFormat = jsonFormat1(Colors) } import…
mirelon
  • 4,896
  • 6
  • 40
  • 70
6
votes
1 answer

Cannot find JsonWriter or JsonFormat type class for a case class

Following the tutorial from http://www.smartjava.org/content/first-steps-rest-spray-and-scala, there are some unexpected error messages. What is going on? Have I defined implicit JsonWriter by the implicit val personFormat = jsonFormat3(Person)…
mirelon
  • 4,896
  • 6
  • 40
  • 70
6
votes
2 answers

How setup spray-json to set null when json element is not present?

Here is spray-json example. Here is NullOptions trait. The problem is when I declare a case class say object MyJsonProtocol extends DefaultJsonProtocol { implicit val some: RootJsonFormat[Some] = jsonFormat2(Some) } case class Some ( …
Cherry
  • 31,309
  • 66
  • 224
  • 364
6
votes
2 answers

spray-json for normal classes (non case) on a List

I'm finding myself in a situation in which I need to serialize into JSON a non case class. Having a class as: class MyClass(val name: String) { def SaySomething() : String = { return "Saying something... " } } I've created a JsonProtocol…
leonfs
  • 710
  • 1
  • 5
  • 14
6
votes
1 answer

How do I turn all rejections into custom json in spray?

When spray (spray.io) produces a rejection, it responds with a string body. Since all my API clients will assume that my API only returns json, I'd like globally make every rejection a valid json object that conforms to our error object format. How…
Adrian Rodriguez
  • 3,232
  • 2
  • 24
  • 34
6
votes
3 answers

What is a good way to handle default values with spray-json

In some cases default values make more sense than optionals in case classes: case class Car(numberOfWheels:Int = 4, color:String) case class Car(numbeOfWheels:Option[Int], color:String) //silly In the first case I'd expect to be able to easily…
iwein
  • 25,788
  • 10
  • 70
  • 111
1
2
3
22 23