json4s

How do you create Json object with values of different types?

♀尐吖头ヾ 提交于 2019-12-05 11:23:51
How do you create Json object with values of different types ? I'm using spray-json Here is the code val images : List[JsObject] = fetchImageUrls(url).map((url: String) => { JsObject(List( "link_path" -> JsString(url), "display_name" -> JsString("image"), "size" -> JsString(""), "modified" -> JsString(""), "thumbnail" -> JsString(url), "filename" -> JsString("image"), "is_dir" -> JsBoolean(x = false), "thumb_exists" -> JsBoolean(x = true)) ) }) val jsonAst: JsObject = JsObject(List( "client" -> JsString("urlimages"), "view" -> JsString("thumbnails"), "contents" -> JsArray(images) )) It works

How can I rename a field during serialization with Json4s?

时光毁灭记忆、已成空白 提交于 2019-12-05 05:19:12
How to easily rename field-names in json4s? From their documentation, i've tried the following snippet but it doesn't seem to rename the serial field to id . case class Person(serial: Int, firstName: String) val rename = FieldSerializer[Person](renameTo("serial", "id")) implicit val format = DefaultFormats + rename write(Person(1, "Guest")) //returns {"serial":1,"firstName":"Guest"} With Jackson library, it's pretty easy by declaring an annotation. But i'm looking for a pure scala library/solution. Is there a better library or way for object-to-json serialization in scala with easy field

How to Manipulate JSON AST in Scala

若如初见. 提交于 2019-12-05 03:23:20
I am experimenting with the json4s library (based on lift-json). One of the things I would like to do is to parse a JSON string into an AST, and then manipulate it. For example, I would like to upsert a field (insert the field into the AST if it does not exist, or update its value if it does). I have not been able to find how to do it in the documentation. Experimenting with the available methods, I have come up with the following, which works, but feels clumsy. import org.json4s._ import org.json4s.JsonDSL._ import org.json4s.native.JsonMethods._ object TestJson { implicit val formats =

@JsonIgnore serialising Scala case class property using Jackon and Json4s

筅森魡賤 提交于 2019-12-04 07:57:50
I'm trying to prevent one of the properties of a Scala case class being serialised. I've tried annotating the property in question with the usual @JsonIgnore and I've also tried attaching the @JsonIgnoreProperties(Array("property_name")) to the case class . Neither of which seem to achieve what I want. Here's a small example: import org.json4s._ import org.json4s.jackson._ import org.json4s.jackson.Serialization import org.json4s.jackson.Serialization.{read, write} import com.fasterxml.jackson.annotation._ object Example extends App { @JsonIgnoreProperties(Array("b")) case class Message(a:

Importing .jar files into Scala environment

混江龙づ霸主 提交于 2019-12-04 03:43:49
问题 Even after reading: Scala, problem with a jar file, I'm still a bit confused. I am trying to import some packages into my Scala file, and the interpreter is not recognizing them even after adding to classpath. One example: I have the import statement: import org.json4s._ I downloaded the .jar from here: http://mvnrepository.com/artifact/org.json4s/json4s-native_2.10/3.2.4 and added to the interpreter classpath using: scala> :cp /Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar Scala

Extract Options from potentially null JSON values using for expression

佐手、 提交于 2019-12-04 03:28:06
问题 I have a JSON document where some values can be null. Using for expressions in json4s, how I can yield None, instead of nothing? The following will fail to yield when the value for either of the fields FormattedID or PlanEstimate is null . val j: json4s.JValue = ... for { JObject(list) <- j JField("FormattedID", JString(id)) <- list JField("PlanEstimate", JDouble(points)) <- list } yield (id, points) For example: import org.json4s._ import org.json4s.jackson.JsonMethods._ scala> parse("""{ |

json4s: Convert type to JValue

走远了吗. 提交于 2019-12-03 17:30:34
问题 I have some source object src and would like to get a JValue from it. All the examples and documentation for json4s seem to revolve around getting a JSON-encoded string, like so: def encodeJson(src: AnyRef): String = { import org.json4s.NoTypeHints import org.json4s.JsonDSL.WithDouble._ import org.json4s.jackson.JsonMethods._ import org.json4s.jackson.Serialization import org.json4s.jackson.Serialization.write implicit val formats = Serialization.formats(NoTypeHints) write(src) } That's great

json4s: Convert type to JValue

孤街浪徒 提交于 2019-12-03 05:45:27
I have some source object src and would like to get a JValue from it. All the examples and documentation for json4s seem to revolve around getting a JSON-encoded string, like so: def encodeJson(src: AnyRef): String = { import org.json4s.NoTypeHints import org.json4s.JsonDSL.WithDouble._ import org.json4s.jackson.JsonMethods._ import org.json4s.jackson.Serialization import org.json4s.jackson.Serialization.write implicit val formats = Serialization.formats(NoTypeHints) write(src) } That's great if I only want the end result, but I'd prefer to write a: def encodeJson(src: AnyRef): JValue It seems

Extract Options from potentially null JSON values using for expression

不打扰是莪最后的温柔 提交于 2019-12-01 17:48:35
I have a JSON document where some values can be null. Using for expressions in json4s, how I can yield None, instead of nothing? The following will fail to yield when the value for either of the fields FormattedID or PlanEstimate is null . val j: json4s.JValue = ... for { JObject(list) <- j JField("FormattedID", JString(id)) <- list JField("PlanEstimate", JDouble(points)) <- list } yield (id, points) For example: import org.json4s._ import org.json4s.jackson.JsonMethods._ scala> parse("""{ | "FormattedID" : "the id", | "PlanEstimate" : null | }""") res1: org.json4s.JValue = JObject(List(

NotSerializableException with json4s on Spark

回眸只為那壹抹淺笑 提交于 2019-12-01 15:55:13
Basically, i have to analyze some complex JSON's on HDFS with Spark. I use "for comprehensions" to (pre)filter the JSON's and "extract" method of json4s to wrap it into a case class This one works fine! def foo(rdd: RDD[String]) = { case class View(C: String,b: Option[Array[List[String]]], t: Time) case class Time($numberLong: String) implicit val formats = DefaultFormats rdd.map { jsonString => val jsonObj = parse(jsonString) val listsOfView = for { JObject(value) <- jsonObj JField(("v"), JObject(views)) <- value normalized <- views.map(x => (x._2)) } yield normalized } So far so good! When i