我明白,如果我有:
case class Person(name: String)
Run Code Online (Sandbox Code Playgroud)
我可以用
object PersonJsonImplicits extends DefaultJsonProtocol {
implicit val impPerson = jsonFormat1(Person)
}
Run Code Online (Sandbox Code Playgroud)
并因此序列化:
import com.example.PersonJsonImplicits._
import spray.json._
new Person("somename").toJson
Run Code Online (Sandbox Code Playgroud)
但是,如果我有
trait Animal
case class Person(name: String) extends Animal
Run Code Online (Sandbox Code Playgroud)
我的代码中有一个地方
val animal = ???
Run Code Online (Sandbox Code Playgroud)
我需要序列化它,我想使用json喷雾
我应该添加哪个序列化程序我希望有类似的东西:
object AnimalJsonImplicits extends DefaultJsonProtocol {
implicit val impAnimal = jsonFormat???(Animal)
}
Run Code Online (Sandbox Code Playgroud)
也许我需要添加一些匹配器,以检查什么类型的动物,所以如果它是一个人,我会指向它但没有发现任何东西......正在阅读https://github.com/spray/spray-json 并且不明白该怎么做..
那么如何序列化这一组呢?
trait Animal
case class Person(name: String) extends Animal
Run Code Online (Sandbox Code Playgroud)
用json喷雾?
如何使用spray-json序列化Map [String,Any]?我试试
val data = Map("name" -> "John", "age" -> 42)
import spray.json._
import DefaultJsonProtocol._
data.toJson
Run Code Online (Sandbox Code Playgroud)
它说Cannot find JsonWriter or JsonFormat type class for scala.collection.immutable.Map[String,Any]
.
我的请求中有一个可选字段:
case class SearchRequest(url: String, nextAt: Option[Date])
Run Code Online (Sandbox Code Playgroud)
我的协议是:
object SearchRequestJsonProtocol extends DefaultJsonProtocol {
implicit val searchRequestFormat = jsonFormat(SearchRequest, "url", "nextAt")
}
Run Code Online (Sandbox Code Playgroud)
如何将nextAt字段标记为可选,以便正确读取和接受以下JSON对象:
{"url":"..."}
{"url":"...", "nextAt":null}
{"url":"...", "nextAt":"2012-05-30T15:23Z"}
Run Code Online (Sandbox Code Playgroud)
我实际上并不关心null的情况,但如果你有细节,那就太好了.我正在使用spray-json,并且认为如果原始JSON对象上没有该选项,则使用Option会跳过该字段.
我是新来的AJAX
和javascript
.在我的项目中,我必须json
在我的javascript
文件中获取一个对象.我已经使用了spray-json
它,它向我显示了url中的json对象.http://localhost:8081/all-modules
{
"status": "S1000",
"description": "Success",
"results": ["module1", "module2", "module3"]
}
Run Code Online (Sandbox Code Playgroud)
我的Ajax电话
$.ajax({
url: 'http://localhost:8081/all-modules',
dataType: 'application/json',
complete: function(data){
alert(data)
},
success: function(data){
alert(data)
}
Run Code Online (Sandbox Code Playgroud)
它会返回一个警报[object Object]
.这里有什么问题?
我需要公开一个接受JSON有效负载的Spray服务.我在哪里可以找到能够展示这种功能的样本?
我正在使用spray-json将自定义对象列表编组到JSON中.我有以下case类和它的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)
}
Run Code Online (Sandbox Code Playgroud)
当我试图像这样的路线投入时:
get {
complete {
List(new ElementResponse(...), new ElementResponse(...))
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个错误说:
could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[List[pl.ftang.scala.polka.rest.ElementResponse]]
Run Code Online (Sandbox Code Playgroud)
也许你知道这是什么问题?
我正在使用Scala 2.10.1喷雾1.1-M7和喷雾-json 1.2.5
我有这两个错误:
Error:(39, 20) Cannot find an implicit ExecutionContext. You might pass
an (implicit ec: ExecutionContext) parameter to your method
or import scala.concurrent.ExecutionContext.Implicits.global.
val pipeline = sendReceive
^
Error:(39, 20) not enough arguments for method sendReceive: (implicit refFactory: akka.actor.ActorRefFactory, implicit executionContext: scala.concurrent.ExecutionContext, implicit futureTimeout: akka.util.Timeout)spray.client.pipelining.SendReceive.
Unspecified value parameter executionContext.
val pipeline = sendReceive
^
Run Code Online (Sandbox Code Playgroud)
我的代码是:
import scala.util.{Success, Failure}
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.event.Logging
import akka.io.IO
import spray.can.Http
import spray.client.pipelining._
import spray.util._
import argonaut._, Argonaut._
object test {
case …
Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的问题.这不仅适用于spray-json,而且我已经阅读过与argonaut和circe类似的说法.所以请赐教.
在spray-json中,我遇到了声明There is no reflection involved
.我理解基于类的类方法,如果用户提供JsonFormat,那么一切都很好.但是这个说法在使用时也是如此DefaultJsonProtocol
吗?
因为当我们你看看这个,你能看到的使用clazz.getMethods
,clazz.getDeclaredFields
等等.这不是反射的使用情况如何?虽然当然要归功于object#apply
我们不需要担心使用反射在Java世界中设置不同.但至少阅读字段名称,我不明白如何反思过度.
我试图在scala中使用spray-json来识别转换为Json和返回时Ec2Provider和OpenstackProvider之间的选择.我希望能够在"提供者"中做出选择,如果这些选择不符合可用的选择,那么它就不应该验证.
我在这方面的尝试可以在以下代码中看到:
import spray.json._
import DefaultJsonProtocol._
case class Credentials(username: String, password: String)
abstract class Provider
case class Ec2Provider(endpoint: String,credentials: Credentials) extends Provider
case class OpenstackProvider(credentials: Credentials) extends Provider
case class Infrastructure(name: String, provider: Provider, availableInstanceTypes: List[String])
case class InfrastructuresList(infrastructures: List[Infrastructure])
object Infrastructures extends App with DefaultJsonProtocol {
implicit val credFormat = jsonFormat2(Credentials)
implicit val ec2Provider = jsonFormat2(Ec2Provider)
implicit val novaProvider = jsonFormat1(OpenstackProvider)
implicit val infraFormat = jsonFormat3(Infrastructure)
implicit val infrasFormat = jsonFormat1(InfrastructuresList)
println(
InfrastructuresList(
List(
Infrastructure("test", Ec2Provider("nova", Credentials("user","pass")), List("1", "2")) …
Run Code Online (Sandbox Code Playgroud) 我有以下路由设置,但是当我的地图在第一个完整的块中返回时,我收到一个错误:
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 = new LinkUserService
def linkRoute =
pathPrefix("user" / Segment) {
userId =>
path("link") {
parameters('service ! "YT") {
complete {
Map("status"-> "OK", "auth_url" -> "http://mydomain.com/auth")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据这个测试,我应该能够在导入DefaultJsonProtocol._时将Map转换为json,但即使这样也失败了:
val map:Map[String, …
Run Code Online (Sandbox Code Playgroud) spray-json ×10
scala ×9
spray ×4
json ×2
ajax ×1
argonaut ×1
javascript ×1
jquery ×1
scala-2.10 ×1
spray-client ×1