json4s

How can I rename a field during serialization with Json4s?

十年热恋 提交于 2019-12-22 05:00:28
问题 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

@JsonIgnore serialising Scala case class property using Jackon and Json4s

可紊 提交于 2019-12-21 17:36:09
问题 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

JSON4s can't find constructor w/spark

99封情书 提交于 2019-12-20 03:05:16
问题 I've run into an issue with attempting to parse json in my spark job. I'm using spark 1.1.0, json4s, and the Cassandra Spark Connector, with DSE 4.6. The exception thrown is: org.json4s.package$MappingException: Can't find constructor for BrowserData org.json4s.reflect.ScalaSigReader$.readConstructor(ScalaSigReader.scala:27) org.json4s.reflect.Reflector$ClassDescriptorBuilder.ctorParamType(Reflector.scala:108) org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$6.apply(Reflector

Generating Json string using Json4S from a list containing Some and None values

夙愿已清 提交于 2019-12-19 09:43:39
问题 I am using Scalatra, which in turn uses Json4S to generate Json string. I receive ["A","B"] for List(Some("A"),None,Some("B")) I would like to receive ["A",undefined,"B"] How can this be fixed ? 回答1: undefined is not a valid json value, even though it is valid in javascript. From rfc4627 (application/json): A JSON value MUST be an object, array, number, or string, or one of the following three literal names: false null true (no mention of undefined) However this is fairly straight-forward to

is JSON4S compatible with spark 2.4.0 and EMR 5.26.0

♀尐吖头ヾ 提交于 2019-12-13 17:49:26
问题 Spark json4s[java.lang.NoSuchMethodError: org.json4s.jackson.JsonMethods$.parse(Lorg/json4s/Js] Getting above error while parsing complex json when running spark scala structured streaming application on aws emr. 回答1: It looks like a binary compatibility error... Could you please check the dependency tree for incompatible versions of json4s artifacts? If you will not able to upgrade them to use the same version then possible you can solve the problem by shading some of them with sbt-assembly

Converting (Very) Complicated JSON objects to Scala Objects With Case Classes In Json4s

吃可爱长大的小学妹 提交于 2019-12-13 05:45:50
问题 I have a very complicated JSON file that looks like this: { "Animals": [ [ 100, "Mammals", [ 1, "Cat", 50, 45, 57, -1 ], [ 2, "Dog", 31, 44, 18, -1 ] ], [ 159, "Reptiles", [ 1, "Lizard", 11, 12, 9, -1 ] ] ] } I am attempting to parse this structure and somehow get scala objects out of it. Here was my attempt: case class Facts(number: Int, subTypeOfAnimal: String, data: List[Int]) case class Animaltype(value: Int, typeOfAnimal: String, characteristics: List[Facts]) case class Animal(rows: List

json4s serialization of string to avro specific record class

本小妞迷上赌 提交于 2019-12-13 05:12:22
问题 I have json string which I am trying to serialize as an Avro Specific Record(scala case class extends org.apache.avro.specific.SpecificRecordBase ). Json4s throws exception rightfully for malformed json in case of normal scala case class but doesn't throw an exception for the case class which extends specific record . Instead it tries to contain with nulls(not sure if it's json4s doing this or having the specific record). Say I have this json: { "name":"Tom", "id":1, "address":{ "houseNum":

Spark + Json4s serialization problems

依然范特西╮ 提交于 2019-12-12 15:31:25
问题 I am using Json4s classes inside of a Spark 2.2.0 closure. The "workaround" for a failure to serialize DefaultFormats is to include their definition inside every closure executed by Spark that needs them. I believe I have done more than I needed to below but still get the serialization failure. Using Spark 2.2.0, Scala 2.11, Json4s 3.2.x (whatever is in Spark) and also tried using Json4s 3.5.3 by pulling it into my job using sbt. In all cases I used the workaround shown below. Does anyone

Scala Play Framework JSON JsNull using json4s

强颜欢笑 提交于 2019-12-11 22:07:19
问题 I'm new to Scala. How do I handle the JsNull value in my code? I'm using json4s to convert the JSON to a map. Should I somehow be converting JsNull to an Option ? Example: Play JSON : creating json val jsonA: JsValue = Json.obj( "name" -> "Bob", "location" -> "Irvine", "resident" -> "No", "nick-name" -> "Bigwig", "age" -> "6", "role" -> JsNull, "car" -> "BMW", "multiple-residents" -> JsArray(Seq( JsObject(Seq( "name" -> JsString("Fiver"), "age" -> JsNumber(4), "role" -> JsObject(Seq(

How do I serialize one field “preservingEmptyValues” to null using json4s

蓝咒 提交于 2019-12-11 16:52:44
问题 I am using json4s. I am aware that you can "preserve None values" of all Option members in a type using DefaultFormats.preservingEmptyValues . This has an output like so: scala case class MyType(a: Option[String], b: Option[Int], c: Int) instance MyType(None, None, 1) output { "a": null , "b": null , "c": 1 } Without DefaultFormats.preservingEmptyValues the output is like so: scala case class MyType(a: Option[String], b: Option[Int], c: Int) instance MyType(None, None, 1) output { "c": 1 }