2017-05-14 8 views
0

私は、受信したjsonの値を編集する必要があるスプレーjsonサポートのakkaを使用しています。私の使用例でsprayJSONで既存のJSONオブジェクトを編集する方法

import akka.http.scaladsl.server.Directives 
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport 
import spray.json._ 


final case class Item(name: String, id: Long) 
final case class Order(items: List[Item],orderTag:String) 


trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol { 
    implicit val itemFormat = jsonFormat2(Item) 
    implicit val orderFormat = jsonFormat2(Order) 
} 

私はヌルとしてorderTag値とJSONを受け取る、私がする必要があるすべては、企業がvalue.Isとして可能/編集書くためにjsonObjectとどのようにorderTag値を編集し、それを使用していますそれをするには?

class MyJsonService extends Directives with JsonSupport { 

    // format: OFF 
    val route = 
    get { 
     pathSingleSlash { 
     complete(Item("thing", 42)) // will render as JSON 
     } 
    } ~ 
    post { 
     entity(as[Order]) { order => // will unmarshal JSON to Order 
     val itemsCount = order.items.size 
     val itemNames = order.items.map(_.name).mkString(", ") 
     complete(s"Ordered $itemsCount items: $itemNames") 
     } 
    } 

} 

答えて

0

あなただけ..

val json = """{"orderTag":null}""" 
val jsVal = json.parseJson 
val updatedJs = if (jsObj.fields.get("orderTag") == Some(JsNull)) { 
JsObject(jsObj.fields + ("orderTag" -> JsString("new tag"))) 
} else { 
    jsObj 
} 
updatedJs.compactPrint 
res26: String = """ 
{"orderTag":"new tag"} 
""" 
のようなJSON形式のASTを編集することができます
関連する問題