Created
May 7, 2011 09:11
-
-
Save toulouse/960349 to your computer and use it in GitHub Desktop.
How to parse JSON into a case class in Scala
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
package models | |
import controllers.FacebookApp | |
import dispatch.json._ | |
import helpers.LogHelper | |
import org.joda.time._ | |
import org.joda.time.format._ | |
import play.db.anorm._ | |
import play.db.anorm.defaults._ | |
import play.libs.WS | |
case class User( | |
id: Pk[Long], | |
name: String, | |
firstName: String, | |
lastName: String, | |
link: String, | |
hometown: (Long, String), | |
location: (Long, String), | |
gender: String, | |
timezone: Int, | |
locale: String, | |
languages: List[(Long, String)], | |
verified: Boolean, | |
updatedTime: DateTime, | |
accessToken: String, | |
expires: Int) | |
object User extends Magic[User] with LogHelper { | |
// def handleParsedUser(parsedUser: User, oldUser: Option[User]) = { | |
// oldUser match { | |
// case None => { | |
// User.create(parsedUser) | |
// logger.info("created user") | |
// } | |
// case Some(user) => { | |
// User.update(parsedUser) | |
// logger.info("updated user") | |
// } | |
// } | |
// | |
// } | |
} | |
object UserUtils extends LogHelper { | |
import dispatch.json.Js._ | |
val idEx = ('id ! str) andThen (_ toLong) | |
val nameEx = 'name ! str | |
val firstNameEx = 'first_name ! str | |
val lastNameEx = 'last_name ! str | |
val linkEx = 'link ! str | |
val hometownEx = 'hometown ! obj | |
val locationEx = 'location ! obj | |
val genderEx = 'gender ! str | |
val timezoneEx = 'timezone ! num | |
val localeEx = 'locale ! str | |
val languagesEx = 'languages ! list | |
val verifiedEx = 'verified ! bool | |
val updatedTimeEx = 'updated_time ! str | |
def query(accessToken: String, expires: Int) = { | |
parse(WS.url(FacebookApp.graphUrl("me", "", accessToken)).get().getString(), accessToken, expires) | |
} | |
def parse(rawData: String, accessToken: String, expires: Int) = { | |
val data = Js(rawData) | |
val userId = idEx(data) | |
val userOption = null //User.find("id = {id}").on("id" -> userId).singleOption() | |
val parsedUser = User( | |
Id(userId), | |
nameEx(data), | |
firstNameEx(data), | |
lastNameEx(data), | |
linkEx(data), | |
((hometownEx andThen idEx)(data), | |
(hometownEx andThen nameEx)(data)), | |
((locationEx andThen idEx)(data), | |
(locationEx andThen nameEx)(data)), | |
genderEx(data), | |
(timezoneEx andThen (_ toInt))(data), | |
localeEx(data), | |
(for (langItem <- languagesEx(data)) yield (idEx(langItem), nameEx(langItem))), | |
verifiedEx(data), | |
ISODateTimeFormat.dateTimeNoMillis().parseDateTime(updatedTimeEx(data)), | |
accessToken, | |
expires) | |
logger.info(data) | |
logger.info(parsedUser) | |
// User.handleParsedUser(parsedUser, userOption) | |
} | |
} | |
// Example data | |
/* | |
{ | |
"id": "12345678", | |
"name": "Foolio Bartez", | |
"first_name": "Foolio", | |
"last_name": "Bartez", | |
"link": "http://www.facebook.com/foobar", | |
"username": "foobar", | |
"hometown": { | |
"id": "123456789012345", | |
"name": "Qux, Quux" | |
}, | |
"location": { | |
"id": "543210987654321", | |
"name": "Corge, Grault" | |
}, | |
"gender": "male", | |
"timezone": -7, | |
"locale": "en_US", | |
"languages": [{ | |
"id": "113301478683221", | |
"name": "American English" | |
}], | |
"verified": true, | |
"updated_time": "2011-05-03T22:18:42+0000", | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment