spray-json

spray-json and list marshalling

爱⌒轻易说出口 提交于 2019-12-04 18:17:27
问题 I'm using spray-json to marshal lists of custom objects into JSON. I have the following case class and its JsonProtocol. case class ElementResponse(name: String, symbol: String, code: String, pkwiu: String, remarks: String, priceNetto: BigDecimal, priceBrutto: BigDecimal, vat: Int, minInStock:Int, maxInStock: Int) object JollyJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport { implicit val elementFormat = jsonFormat10(ElementResponse) } When I try to put in in a route like this

spray-json cannot marshal Map[String,String]

喜你入骨 提交于 2019-12-04 03:07:22
I have the following route setup, but when my map is returned in the first complete block I get an error: could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[scala.collection.immutable.Map[String,String]] import spray.routing.HttpService import akka.actor.Actor import spray.http.HttpRequest import spray.routing.RequestContext import spray.json.DefaultJsonProtocol._ class UserServiceActor extends Actor with RestUserService { def actorRefFactory = context def receive = runRoute(linkRoute) } trait RestUserService extends HttpService { val userService =

spray-json and list marshalling

不打扰是莪最后的温柔 提交于 2019-12-03 12:17:05
I'm using spray-json to marshal lists of custom objects into JSON. I have the following case class and its JsonProtocol. case class ElementResponse(name: String, symbol: String, code: String, pkwiu: String, remarks: String, priceNetto: BigDecimal, priceBrutto: BigDecimal, vat: Int, minInStock:Int, maxInStock: Int) object JollyJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport { implicit val elementFormat = jsonFormat10(ElementResponse) } When I try to put in in a route like this one: get { complete { List(new ElementResponse(...), new ElementResponse(...)) } } I get an error saying

How to represent optional fields in spray-json?

折月煮酒 提交于 2019-12-03 06:06:10
I have an optional field on my requests: case class SearchRequest(url: String, nextAt: Option[Date]) My protocol is: object SearchRequestJsonProtocol extends DefaultJsonProtocol { implicit val searchRequestFormat = jsonFormat(SearchRequest, "url", "nextAt") } How do I mark the nextAt field optional, such that the following JSON objects will be correctly read and accepted: {"url":"..."} {"url":"...", "nextAt":null} {"url":"...", "nextAt":"2012-05-30T15:23Z"} I actually don't really care about the null case, but if you have details, it would be nice. I'm using spray-json, and was under the

Which among importing companion object or extending trait is better

江枫思渺然 提交于 2019-12-02 02:48:46
问题 I have a JSON protocol written in spray trait MyJsonProtocol { //some logic } object MyJsonProtocol extends MyJsonProtocol { } Now which is better ?? Importing this companion object or extending the trait ? 回答1: If you are creating some JsonFormat instances for spray, then you can just create an object directly and import that. This means that you only have a single instance of your implicit vals and objects. object MyJsonProtocol extends DefaultJsonProtocol { implicit object MyTypeJsonFormat

Which among importing companion object or extending trait is better

筅森魡賤 提交于 2019-12-01 23:43:32
I have a JSON protocol written in spray trait MyJsonProtocol { //some logic } object MyJsonProtocol extends MyJsonProtocol { } Now which is better ?? Importing this companion object or extending the trait ? If you are creating some JsonFormat instances for spray, then you can just create an object directly and import that. This means that you only have a single instance of your implicit vals and objects. object MyJsonProtocol extends DefaultJsonProtocol { implicit object MyTypeJsonFormat extends RootJsonFormat[MyType] { def write(v: MyType): JsValue = ... def read(value: JsValue): MyType = ...

Get JSON object from AJAX call

自古美人都是妖i 提交于 2019-12-01 14:10:47
问题 I'm new to AJAX and javascript . In my project, I have to get a json object in my javascript file. I've used spray-json and it shows me the json object in the url. http://localhost:8081/all-modules { "status": "S1000", "description": "Success", "results": ["module1", "module2", "module3"] } My Ajax call $.ajax({ url: 'http://localhost:8081/all-modules', dataType: 'application/json', complete: function(data){ alert(data) }, success: function(data){ alert(data) } It returns an alert [object

Spray-json deserializing nested object

匆匆过客 提交于 2019-11-30 04:14:06
How to deserialize nested objects correctly in spray-json? import spray.json._ case class Person(name: String) case class Color(n: String, r: Int, g: Int, b: Int, p: Person) object MyJsonProtocol extends DefaultJsonProtocol { implicit object ColorJsonFormat extends RootJsonFormat[Color] { def write(c: Color) = JsObject( "color-name" -> JsString(c.n), "Green" -> JsNumber(c.g), "Red" -> JsNumber(c.r), "Blue" -> JsNumber(c.b), "person-field" -> JsObject("p-name" -> JsString(c.p.name)) ) def read(value: JsValue) = { value.asJsObject.getFields("color-name", "Red", "Green", "Blue", "person-field")

Serialize Map[String, Any] with spray json

女生的网名这么多〃 提交于 2019-11-29 12:05:01
问题 How do I serialize Map[String, Any] with spray-json? I try val data = Map("name" -> "John", "age" -> 42) import spray.json._ import DefaultJsonProtocol._ data.toJson It says Cannot find JsonWriter or JsonFormat type class for scala.collection.immutable.Map[String,Any] . 回答1: Here's an implicit converter I used to do this task: implicit object AnyJsonFormat extends JsonFormat[Any] { def write(x: Any) = x match { case n: Int => JsNumber(n) case s: String => JsString(s) case b: Boolean if b ==