public
Created

  • Download Gist
json.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
scala>
 
scala> import net.liftweb.json._
import net.liftweb.json._
 
scala>
 
scala> implicit val formats = DefaultFormats
formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@33462704
 
scala>
 
scala>
 
scala>
 
scala> case class TypesQueries(queries: Map[String, JValue]);
defined class TypesQueries
 
scala> case class AddressQuery(street: String, city: String, state: String, zip: Int)
defined class AddressQuery
 
scala> case class AddressQueries(addresses: List[AddressQuery]);
defined class AddressQueries
 
scala> case class NameQuery(firstName: String, lastName: String);
defined class NameQuery
 
scala> case class NameQueries(names: List[NameQuery]);
defined class NameQueries
 
scala> case class EmailQuery(emailAddress: String);
defined class EmailQuery
 
scala> case class EmailQueries(emails: List[EmailQuery]);
defined class EmailQueries
 
scala>
 
scala> val jsonData = parse("""{
| "queries" : {
| "addresses" : [
| {
| "street" : "1234 Main St.",
| "city" : "New York",
| "state" : "New York",
| "zip" : 12345
| },
| {
| "street" : "9876 Broadway Blvd.",
| "city" : "Chicago",
| "state" : "IL",
| "zip" : 23456
| }
| ],
| "names": [
| {
| "firstName" : "John",
| "lastName" : "Doe"
| }
| ],
| "emails" : [
| {
| "emailAddress" : "john.doe@gmail.com"
| },
| {
| "emailAddress" : "david.smith@gmail.com"
| }
| ]
| }
| }""");
jsonData: net.liftweb.json.package.JValue = JObject(List(JField(queries,JObject(List(JField(addresses,JArray(List(JObject(List(JField(street,JString(1234 Main St.)), JField(city,JString(New York)), JField(state,JString(New York)), JField(zip,JInt(12345)))), JObject(List(JField(street,JString(9876 Broadway Blvd.)), JField(city,JString(Chicago)), JField(state,JString(IL)), JField(zip,JInt(23456))))))), JField(names,JArray(List(JObject(List(JField(firstName,JString(John)), JField(lastName,JString(Doe))))))), JField(emails,JArray(List(JObject(List(JField(emailAddress,JString(john.doe@gmail.com)))), JObject(List(JField(emailAddress,JString(david.smith@gmail.com))))))))))))
 
scala>
 
scala>
 
scala> val typesQuery = parse(jsonData).extract[TypesQueries];
<console>:24: error: type mismatch;
found : net.liftweb.json.package.JValue
required: String
val typesQuery = parse(jsonData).extract[TypesQueries];
^
 
scala>
 
scala>
 
scala> typesQuery.queries.foreach {
| case(queryType, queryDefinition) => queryType match {
| case "addresses" => val addressQueries = typesQuery.queries.extract[AddressQueries]; println(addressQueries)
| case "names" => val nameQueries = typesQuery.queries.extract[NameQueries]; println(nameQueries)
| case "emails" => val emailQueries = typesQuery.queries.extract[EmailQueries]; println(emailQueries)
| }
| }
AddressQueries(List(AddressQuery(1234 Main St.,New York,New York,12345), AddressQuery(9876 Broadway Blvd.,Chicago,IL,23456)))
NameQueries(List(NameQuery(John,Doe)))
EmailQueries(List(EmailQuery(john.doe@gmail.com), EmailQuery(david.smith@gmail.com)))
 
scala>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.