/ / Wie werden optionale Felder in Spray-Json dargestellt? - Scala, Spray-Json

Wie man optionale Felder in Spray-json darstellt? - Scala, Spray-Json

Ich habe ein optionales Feld für meine Anfragen:

case class SearchRequest(url: String, nextAt: Option[Date])

Mein Protokoll ist:

object SearchRequestJsonProtocol extends DefaultJsonProtocol {
implicit val searchRequestFormat = jsonFormat(SearchRequest, "url", "nextAt")
}

Wie markiere ich das nextAt-Feld als optional, sodass die folgenden JSON-Objekte korrekt gelesen und akzeptiert werden:

{"url":"..."}
{"url":"...", "nextAt":null}
{"url":"...", "nextAt":"2012-05-30T15:23Z"}

Mir ist der Nullfall eigentlich egal,Aber wenn Sie Einzelheiten haben, wäre es schön. Ich benutze Spray-Json und hatte den Eindruck, dass die Verwendung einer Option das Feld überspringt, wenn es auf dem ursprünglichen JSON-Objekt nicht vorhanden wäre.

Antworten:

8 für die Antwort № 1

Möglicherweise müssen Sie ein explizites Format erstellen (Warnung: psuedocodish):

object SearchRequestJsonProtocol extends DefaultJsonProtocol {
implicit object SearchRequestJsonFormat extends JsonFormat[SearchRequest] {
def read(value: JsValue) = value match {
case JsObject(List(
JsField("url", JsString(url)),
JsField("nextAt", JsString(nextAt)))) =>
SearchRequest(url, Some(new Instant(nextAt)))

case JsObject(List(JsField("url", JsString(url)))) =>
SearchRequest(url, None)

case _ =>
throw new DeserializationException("SearchRequest expected")
}

def write(obj: SearchRequest) = obj.nextAt match {
case Some(nextAt) =>
JsObject(JsField("url", JsString(obj.url)),
JsField("nextAt", JsString(nextAt.toString)))
case None => JsObject(JsField("url", JsString(obj.url)))
}
}
}

25 für die Antwort № 2

Funktioniert für mich (spray-json 1.1.1 scala 2.9.1 build)

import cc.spray.json._
import cc.spray.json.DefaultJsonProtocol._

// string instead of date for simplicity
case class SearchRequest(url: String, nextAt: Option[String])

// btw, you could use jsonFormat2 method here
implicit val searchRequestFormat = jsonFormat(SearchRequest, "url", "nextAt")

assert {
List(
"""{"url":"..."}""",
"""{"url":"...", "nextAt":null}""",
"""{"url":"...", "nextAt":"2012-05-30T15:23Z"}""")
.map(_.asJson.convertTo[SearchRequest]) == List(
SearchRequest("...", None),
SearchRequest("...", None),
SearchRequest("...", Some("2012-05-30T15:23Z")))
}

6 für die Antwort № 3

Benutzen NullOptions Eigenschaft, um das Überspringen zu deaktivieren nulls: https://github.com/spray/spray-json#nulloptions Beispiel: https://github.com/spray/spray-json/blob/master/src/test/scala/spray/json/ProductFormatsSpec.scala


0 für die Antwort № 4

Wissen Sie nicht, ob dies Ihnen helfen wird, aber Sie können diesem Feld in der Fallklassendefinition einen Standardwert zuweisen. Wenn das Feld nicht im Json-Feld enthalten ist, weist es dem Standardwert zu.


0 für die Antwort № 5

Einfach.

import cc.spray.json._
trait MyJsonProtocol extends DefaultJsonProtocol {
implicit val searchFormat = new JsonWriter[SearchRequest] {
def write(r: SearchRequest): JsValue = {
JsObject(
"url" -> JsString(r.url),
"next_at" -> r.nextAt.toJson,
)
}
}
}

class JsonTest extends FunSuite with MyJsonProtocol {
test("JSON") {
val search = new SearchRequest("www.site.ru", None)
val marshalled = search.toJson
println(marshalled)
}
}