Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the diffrence between JsObject and JsValue in Scala?

JsObject and JsValue solves almost the same purpose but in JsObject

val data[JsObject] = Json.obj("id1"-> "some_id1")
val newData[JsObject] = data ++ Json.obj("id"-> "some_id")

we can add a new key-value pair in JsObject but I still haven't figure out how to add a key-value pair in JsValue

like image 279
perfectus Avatar asked Dec 05 '16 11:12

perfectus


People also ask

What is a JSValue?

You use the JSValue class to convert basic values, such as numbers and strings, between JavaScript and Objective-C or Swift representations to pass data between native code and JavaScript code.

What is play JSON?

The Play JSON API provides implicit Writes for most basic types, such as Int , Double , String , and Boolean . It also supports Writes for collections of any type T that a Writes[T] exists. import play. json.


1 Answers

You can do more with JsObject because it is a subclass of JsValue that represents a JSON object ({...}). JsValue can be a string, numeric, object or array. Therefore you can't assume that it has key value pairs. If you have a val x: JsValue that you know is a JsObject you can use x.as[JsObject] to cast it or if you're not sure it's a JsObject you can use x.asOpt[JsObject] to get an Option[JsObject].

like image 116
Simon Avatar answered Sep 22 '22 01:09

Simon