json4s

Deserialize nested polymorphic json field with json4s

十年热恋 提交于 2019-12-11 10:33:44
问题 I have a common problem but still couldn't wrap my head around what I was reading around. In a scalatra app, I am receiving the following json: { _type: "hello", timestamp: 123, data: [ {table: "stuffJ",_id: 24}, {table: "preferences",_id: 34,word: "john"} ]} with an unknown number of elements in field 'data'. The field table will always be there to differentiate between class types. I am trying to have it parsed to class RestAPIMessage . This is what I have so far: implicit val jsonFormats =

Convert java enum to scala Enumeration for json4s serialization

独自空忆成欢 提交于 2019-12-11 01:36:12
问题 I'm using the json4s library to convert scala case classes into json messages. My case classes are dependent on third party java enum types: //third party java code public enum Fruit { Banana (1), Cherry (2); } My scala classes then use this enum as a parameter: case class Order(fruit : Fruit, quantity : Int) I'm trying to use EnumNameSerializer provided by the `org.json4s.ext' library: import org.json4s._ import org.json4s.native.Serialization import org.json4s.native.Serialization.{write,

Spark streaming + json4s-jackson dependency problems

我怕爱的太早我们不能终老 提交于 2019-12-10 18:56:05
问题 I am unable to use json4s-Jackson 3.2.11 within my spark 1.4.1 Streaming application. Thinking that it was the existing dependency within the spark-core project that is causing the problem as explained here -> Is it possible to use json4s 3.2.11 with Spark 1.3.0? I have built Spark from source with an adjusted core/pom.xml. I have changed the reference from json4s-jackson_2.10:3.2.10 to 3.2.11, as the 2.10 version does not support extracting to implicit types. I have replaced the source jars

How to serialize sealed abstract class with Json4s in Scala?

左心房为你撑大大i 提交于 2019-12-08 10:19:39
问题 How do i serialize a sealed abstract class with Json4s in Scala? The following classes are defined: sealed abstract class Person extends Product with Serializable case class Spouse(name: String, age: Int) extends Person case class Customer(name: String, age: Int, spouse: Spouse) extends Person I create an object of type Customer: val customer: Customer = Customer("Joe", 35, Spouse("Marilyn", 33)) Then I serialize to JSON: implicit val formats = DefaultFormats val serialized = write(customer)

[json4s]:Extracting Array of different objects

扶醉桌前 提交于 2019-12-07 16:48:58
问题 I am using the facebook graph API and the responses look similar to this: { "data": [ { "id": "311620272349920_311718615673419", "from": { "id": "1456046457993048", "name": "Richard Ettinson" }, "to": { "data": [ { "id": "311620272349920", "name": "Barbara Fallerman" } ] }, "with_tags": { "data": [ { "id": "311620272349920", "name": "Barbara Fallerman" } ] }, "message": "I was gong out with her", "actions": [ { "name": "Comment", "link": "https://www.facebook.com/311620272349920/posts

Serialize and Deserialize scala enumerations or case objects using json4s

≯℡__Kan透↙ 提交于 2019-12-07 01:38:54
问题 Suppose I have an enumeration or sealed group of case objects as follows: sealed abstract class Status case object Complete extends Status case object Failed extends Status case object Pending extends Status case object Unknown extends Status or object Status extends Enumeration { val Complete, Failed, Pending, Unknown = Value } What is the easiest way to create json formats for these so that I can very easily (programmatically) generate json formats for use in a custom JsonFormat factory

How to Manipulate JSON AST in Scala

霸气de小男生 提交于 2019-12-06 21:52:22
问题 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

Purpose of `render` in json4s

微笑、不失礼 提交于 2019-12-06 20:14:40
问题 In json4s examples and documentation I often see the idioms compact(render(jval)) and pretty(render(jval)) I do not think I have actually seen an example with compact or pretty applied directly to a code generated JValue , but it is not clear to me what render is doing here. Render has type JValue => JValue and I do not see any obvious difference it makes and running json.take(100000).filter(x => compact(render(x)) != compact(x)) on some of my data returns an empty an empty collection. What

How can I convert between play.api.libs.json.JsValue and or.json4s.JValue in Scala

痴心易碎 提交于 2019-12-06 03:55:48
I'm using Play Framework to build an API, but need to perform some validation and transformation operations with a library that only speaks Json4s. So far, the only thing I've been able to get to work is converting to a string and parsing with the target library: import org.json4s._ import org.json4s.jackson.JsonMethods._ import play.api.libs.json._ val playJson: JsValue = Json.parse(compact(render(json4sJson))) val json4sJson: JValue = parse(Json.stringify(playJson)) But that's obviously inefficient and makes for ugly code. Is there a better way? Pattern match and convert Write implicit

[json4s]:Extracting Array of different objects

白昼怎懂夜的黑 提交于 2019-12-05 19:05:11
I am using the facebook graph API and the responses look similar to this: { "data": [ { "id": "311620272349920_311718615673419", "from": { "id": "1456046457993048", "name": "Richard Ettinson" }, "to": { "data": [ { "id": "311620272349920", "name": "Barbara Fallerman" } ] }, "with_tags": { "data": [ { "id": "311620272349920", "name": "Barbara Fallerman" } ] }, "message": "I was gong out with her", "actions": [ { "name": "Comment", "link": "https://www.facebook.com/311620272349920/posts/311718615673419" }, { "name": "Like", "link": "https://www.facebook.com/311620272349920/posts/311718615673419"