spray-json

Spray-Json: serialize None as null

夙愿已清 提交于 2020-12-12 10:13:46
问题 I am porting a rest API to scala, using akka-http with spray-json. The old API had the following response: { "result": { ... }, "error": null } Now I want to maintain exact backwards compatibility, so when there's no error I want an error key with a null value. However I can't see any support for this in spray-json. When I serialize the following with a None error: case class Response(result: Result, error: Option[Error]) I end up with { "result": { ... } } And it completely drops the error

NPE in spray-json because of recursive implicits (context bound issue?)

末鹿安然 提交于 2020-01-04 05:22:09
问题 Perhaps I discovered a bug in spray-json. I get Null Pointer Exception when I'm trying to get json of an object that has field of type of itself. Example is: case class TestItem(subitems: Option[List[TestItem]]) object MyJsonProtocol extends DefaultJsonProtocol { implicit val testItemFormat: RootJsonFormat[TestItem] = jsonFormat(TestItem, "subitems") } import MyJsonProtocol._ object TestNPE { def main(args: Array[String]) { val subitems = List(TestItem(None)) val item: TestItem = TestItem

Providing a JsonFormat for a Sequence of Objects

僤鯓⒐⒋嵵緔 提交于 2020-01-03 15:58:00
问题 i`m trying here to find some help to apply an JsonFormat extended of the DefaultJsonProtocol to an class containing a Sequence of Objects. So for the classes: class Person(val name: String, [......], val adresses: Seq[Adress]) class Adress(val streetname: String, val plz: BigDecimal, val city: String) now i would like to apply my JsonFormat: object PersonJsonProtocol extends DefaultJsonProtocol { implicit object PersonJsonFormat extends RootJsonFormat[Person] { def write(pers: Person) =

Providing a JsonFormat for a Sequence of Objects

余生颓废 提交于 2020-01-03 15:57:48
问题 i`m trying here to find some help to apply an JsonFormat extended of the DefaultJsonProtocol to an class containing a Sequence of Objects. So for the classes: class Person(val name: String, [......], val adresses: Seq[Adress]) class Adress(val streetname: String, val plz: BigDecimal, val city: String) now i would like to apply my JsonFormat: object PersonJsonProtocol extends DefaultJsonProtocol { implicit object PersonJsonFormat extends RootJsonFormat[Person] { def write(pers: Person) =

Marshaller not found

隐身守侯 提交于 2020-01-03 03:08:09
问题 Just trying out the spray-json, and it seems to be having problem finding my JsonProtocols I have setup. I have the following dependencies: "io.spray" % "spray-servlet" % "1.2-M8", "io.spray" % "spray-routing" % "1.2-M8", "io.spray" % "spray-testkit" % "1.2-M8", "io.spray" % "spray-json_2.10" % "1.2.5" And the following code: Content.scala import spray.json.DefaultJsonProtocol case class Content(id:String, name: String, contentType: String, duration: Int) object MyJsonProtocol extends

Parsing a simple array with Spray-json

為{幸葍}努か 提交于 2020-01-02 02:59:05
问题 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", "age": "30" }, { "name": "Tom", "age": "25" }] And my code looks like this: package jsontest import spray.json._ import DefaultJsonProtocol._ object JsonFun { case class Person(name: String, age: String) case class FriendList(items: List[Person]) object

JsonFormat for abstract class with generic parameter

谁都会走 提交于 2019-12-24 08:49:04
问题 I am trying to write a JsonFormat for an abstract class with a generic parameter that looks like something like this: abstract class Animal[A] { def data: A def otherStuff: String = "stuff" } case class CatData(catField: String) case class Cat(data: CatData) extends Animal[CatData] So far my attempt at this looks like: object AnimalProtocol extends DefaultJsonProtocol { implicit val catDataFormat = jsonFormat1(CatData) implicit val catFormat = jsonFormat1(Cat) implicit def animalFormat[T <:

Explanation for - No Reflection involved

孤街浪徒 提交于 2019-12-23 07:27:30
问题 I have a very simple question. This is not only true with spray-json but I have read similar claims with argonaut and circe. So please enlighten me. In spray-json, I have come across the statement saying There is no reflection involved . I understand for type class based approach, if the user provides JsonFormat then all is well. But is this claim also true when it comes to using DefaultJsonProtocol ? Because when we you look at this, you can see the usage of clazz.getMethods , clazz

(Un)marshall JSON with named root for Ember Data using Scala case class on Spray

早过忘川 提交于 2019-12-23 05:44:11
问题 I am writing a RESTful interface and I would like to marshall and unmarshall JSON ready for Ember Data. The wrinkle is that Ember Data wants the entity name and the two libraries I've tried, spray-json and json4s, don't appear to do this easily. Desired Ember Data format { "coursePhoto": { "photoId": 1 } } Current default format: {"photoId":15} This should come from a case class: case class CoursePhoto(photoId: Long) I did get it running with the following custom code: object

How to parse json with spray json that uses snake case (underscore notation) instead of camel case

牧云@^-^@ 提交于 2019-12-23 02:39:48
问题 How to parse json with spray json that uses snake case (underscore notation) instead of camel case? E.g. case class Test(subjectDescription: String) "{\"subject_description\":\"Medicine\"}".parseJson.convertTo[Test] should work and not throw exception. 回答1: Like this: case class Test(subjectDescription: String) implicit val testFormat = jsonFormat(Test.apply, "subject_description") "{\"subject_description\":\"Medicine\"}".parseJson.convertTo[Test] The trick here is jsonFormat function takes