uJson is uPickle’s JSON library, which can be used to easily manipulate JSON source and data structures without converting them into Scala case-classes. It comes bundled with uPickle json serializer but it can be used standalone as well. You may refer to my previous blog to known more about uPickle.
We can perform the following operations with uJson:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package object ujson{ | |
def transform[T](t: Transformable, v: Visitor[_, T]) = t.transform(v) | |
def read(s: Transformable): Js.Value = transform(s, Js) | |
def copy(t: Js.Value): Js.Value = transform(t, Js) | |
def write(t: Js.Value, indent: Int = -1): String = { | |
transform(t, StringRenderer(indent)).toString | |
} | |
def writeTo(t: Js.Value, out: java.io.Writer, indent: Int = -1): Unit = { | |
transform(t, Renderer(out, indent)) | |
} | |
def validate(s: Transformable): Unit = transform(s, NoOpVisitor) | |
def reformat(s: Transformable, indent: Int = -1): String = { | |
transform(s, StringRenderer(indent)).toString | |
} | |
def reformatTo(s: Transformable, out: java.io.Writer, indent: Int = -1): Unit = { | |
transform(s, Renderer(out, indent)).toString | |
} | |
// End ujson | |
} |
Add the following to the build.sbt
libraryDependencies += "com.lihaoyi" %% "ujson" % "0.6.5"
Now let’s see the syntax.
We will use the following ujson array for all the examples.
import ujson.Js val json = Js.Arr( Js.Obj("brand" -> "Samsung", "model" -> "EGIR34"), Js.Obj("brand" -> "Nokia", "model" -> "JHKL56") ) Output: json: ujson.Js.Arr = [{"brand":"Samsung","model":"EGIR34"},{"brand":"Nokia","model":"JHKL56"}]
Now we can get the value from the json like this:
val brand = json(0)("brand").str //Output: brand: String = Samsung val model = json(0)("model").str //Output: model: String = EGIR34
To remove an element we can use:
val jsValue = json.arr.remove(1) //Output: jsValue: ujson.Js.Value = {"brand":"Nokia","model":"JHKL56"}
Transformations
uJson allows you seamlessly convert between any of the following forms you may find your JSON in:
- Case classes & Scala data-types
ujson.Js
ASTsString
sCharSequence
sArray[Byte]
s- Third-party JSON ASTs (Argonaut, Circe, Json4s, Play-Json)
String
val jsonString = ujson.transform(json, StringRenderer()).toString //Output: jsonString: String = [{"brand":"Samsung","model":"EGIR34"},{"brand":"Nokia","model":"JHKL56"}]
Array[Byte]
val jsonJson = ujson.transform(json, BytesRenderer()).toBytes Output: jsonJson: Array[Byte] = Array(91, 123, 34, 98, 114, 97, 110, 100, 34, 58, 34, 83, 97, 109, 115, 117, 110, 103, 34, 44, 34, 109, 111, 100, 101, 108, 34, 58, 34, 69, 71, 73, 82, 51, 52, 34, 125, 44, 123, 34, 98, 114, 97, 110, 100, 34, 58, 34, 78, 111, 107, 105, 97, 34, 44, 34, 109, 111, 100, 101, 108, 34, 58, 34, 74, 72, 75, 76, 53, 54, 34, 125, 93)
Transformation
We can every transform the json to third-party JSON library.
We will now convert ujson array that be have taken earlier to the third party libraries and convert that to the json string.
Libraries
- Argonaut
To use ujson with Argonaut we need to add the following dependency:
libraryDependencies += "com.lihaoyi" %% "ujson-argonaut" % "0.6.5"
Now, transforming ujson to Argonaut:
import ujson.argonaut.ArgonautJson val argJson: argonaut.Json = ArgonautJson( json ) Output: argJson: argonaut.Json = [{"brand":"Samsung","model":"EGIR34"},{"brand":"Nokia","model":"JHKL56"}] val argonautString = ArgonautJson.transform(argJson, StringRenderer()).toString Output: argonautString: String = [{"brand":"Samsung","model":"EGIR34"},{"brand":"Nokia","model":"JHKL56"}]
2. Circe
To use ujson with Circe we need to add the following dependency:
libraryDependencies += "com.lihaoyi" %% "ujson-circe" % "0.6.5"
Now, transforming ujson to Circe:
import ujson.circe.CirceJson val circeJson: io.circe.Json = CirceJson( json ) Output: circeJson: io.circe.Json = [ { "brand" : "Samsung", "model" : "EGIR34" }, { "brand" : "Nokia", "model" : "JHKL56" } ] val circeString = CirceJson.transform(circeJson, StringRenderer()).toString Output: circeString: String = [{"brand":"Samsung","model":"EGIR34"},{"brand":"Nokia","model":"JHKL56"}]
3. Json4s
To use ujson withJson4s we need to add the following dependency:
libraryDependencies += "com.lihaoyi" %% "ujson-json4s" % "0.6.5"
Now, transforming ujson to Json4s:
import ujson.json4s.Json4sJson import org.json4s.JsonAST val json4sJson: JsonAST.JValue = Json4sJson( json ) Output: json4sJson: org.json4s.JsonAST.JValue = JArray(List(JObject(List((brand,JString(Samsung)), (model,JString(EGIR34)))), JObject(List((brand,JString(Nokia)), (model,JString(JHKL56)))))) val json4sString = Json4sJson.transform(json4sJson, StringRenderer()).toString Output: json4sString: String = [{"brand":"Samsung","model":"EGIR34"},{"brand":"Nokia","model":"JHKL56"}]
Reference: