Created
October 7, 2015 16:41
-
-
Save mcalavera81/0efcf90f0ee3caf634c2 to your computer and use it in GitHub Desktop.
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
object JsonParsing extends App { | |
import org.json4s._ | |
import org.json4s.jackson.JsonMethods._ | |
import org.json4s.JsonDSL._ | |
implicit val formats = DefaultFormats | |
println(parse( """ { "numbers" : [1, 2, 3, 4] } """)) | |
println(parse( """{"name":"Toy","price":35.35}""", useBigDecimalForDouble = true)) | |
val json1 = List(1, 2, 3) | |
println(compact(render(json1))) | |
val json2 = ("name" -> "joe") | |
println(compact(render(json2))) | |
val json3 = ("name" -> "joe") ~ ("age" -> 35) | |
println(pretty(render(json3))) | |
val winners = List(Winner(23, List(2, 45, 34, 23, 3, 5)), Winner(54, List(52, 3, 12, 11, 18, 22))) | |
val lotto = Lotto(5, List(2, 45, 34, 23, 7, 5, 3), winners, None) | |
val json = | |
("lotto" -> | |
("lotto-id" -> lotto.id) ~ | |
("winning-numbers" -> lotto.winningNumbers) ~ | |
("draw-date" -> lotto.drawDate.map(_.toString)) ~ | |
("winners" -> | |
lotto.winners.map { w => | |
(("winner-id" -> w.id) ~ | |
("numbers" -> w.numbers)) | |
})) | |
case class Winner(id: Long, numbers: List[Int]) | |
case class Lotto(id: Long, winningNumbers: List[Int], winners: List[Winner], drawDate: Option[java.util.Date]) | |
println(pretty(render(json))) | |
val json4: JObject = | |
("person" -> | |
("name" -> "Joe") ~ | |
("age" -> 35) ~ | |
("spouse" -> | |
("person" -> | |
("name" -> "Marilyn") ~ | |
("age" -> 33) | |
) | |
) | |
) | |
println(json4) | |
println(pretty(render(json4))) | |
println(pretty(render(json4 \\ "spouse"))) | |
val json5 = parse( | |
""" | |
{ "name": "joe", | |
"children": [ | |
{ | |
"name": "Mary", | |
"age": 5 | |
}, | |
{ | |
"name": "Mazy", | |
"age": 3 | |
} | |
] | |
} | |
""") | |
println((json5 \ "children")(1) \ "name") | |
case class Child(name: String, age: Int, birthdate: Option[java.util.Date]) | |
case class Address(street: String, city: String) | |
case class Person(name: String, address: Address, children: List[Child]) | |
val json6 = parse( | |
""" | |
{ "name": "joe", | |
"address": { | |
"street": "Bulevard", | |
"city": "Helsinki" | |
}, | |
"other_field": "hello", | |
"children": [ | |
{ | |
"name": "Mary", | |
"age": 5, | |
"birthdate": "2004-09-04T18:06:22Z" | |
}, | |
{ | |
"name": "Mazy", | |
"age": 3, | |
"bla":45 | |
} | |
] | |
} | |
""") | |
val person: Person=json6.extract[Person] | |
println(person) | |
val children =(json6 \ "children").extract[List[Child]] | |
println(children) | |
val children2 =json6.extract[Map[String,_]] | |
println(children2) | |
import org.json4s.Xml.{toJson, toXml} | |
println(toXml(json6)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment