Created
December 3, 2012 00:45
-
-
Save fehguy/4191861 to your computer and use it in GitHub Desktop.
Json4s + salat sample
This file contains hidden or 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
import java.util.Date | |
// define some classes | |
case class Child (name: String, birthdate: Date) | |
case class Spouse (name: String, birthdate: Date) | |
case class Address (address1: String, | |
address2: String, | |
city: String, | |
state: String, | |
zip: String) | |
case class UserSettings ( | |
wantsGeneralNotifications: Boolean, | |
wantsUpdates: Boolean) | |
case class User (username: String, | |
email: String, | |
addresses: List[Address], | |
spouses: List[Spouse], | |
children: List[Child], | |
settings: UserSettings) | |
import org.json4s._ | |
import org.json4s.JsonDSL._ | |
import org.json4s.jackson.JsonMethods._ | |
import org.json4s.native.Serialization.{read, write} | |
// deserialize json into object with default formats | |
implicit val formats = DefaultFormats | |
// turn json into an object | |
val jsonString = """ | |
{ | |
"username":"johnny", | |
"email":"[email protected]", | |
"addresses":[ | |
{ | |
"address1":"195 E. 4th Ave", | |
"address2":"2nd floor", | |
"city":"San Mateo", | |
"state":"CA", | |
"zip":"94401" | |
} | |
], | |
"settings":{ | |
"wantsGeneralNotifications":true, | |
"wantsUpdates":true | |
} | |
} | |
""" | |
val json = parse(jsonString) | |
val userObject = json.extract[User] | |
// serialize an object into JSON | |
val user = User("fehguy", | |
"[email protected]", | |
List( | |
Address("195 E. 4th Ave", "2nd floor", "San Mateo", "CA", "94401") | |
), | |
List.empty, | |
List.empty, | |
UserSettings(true, true) | |
) | |
implicit val formats = DefaultFormats | |
// turn an object into json | |
write(user) | |
// a custom serializer to handle missing fields on the input | |
class AddressSerializer extends CustomSerializer[Address](formats => ({ | |
case json => | |
implicit val fmts: Formats = formats | |
Address( | |
address1 = (json \ "address1"), // required! | |
address2 = (json \ "address2").extractOrElse(""), // handle empty values | |
city = (json \ "city").extractOrElse(""), | |
state = (json \ "state").extractOrElse(""), | |
zip = (json \ "zip").extractOrElse("") | |
) | |
}, { | |
case x: Address => | |
implicit val fmts = formats | |
("address1" -> x.address1) ~ | |
("address2" -> x.address2) ~ | |
("city" -> x.city) ~ | |
("state" -> x.state) ~ | |
("zip" -> x.zip) | |
})) | |
implicit val formats = DefaultFormats + new AddressSerializer | |
// note tne missing "address2" field! | |
val jsonString = """ | |
{ | |
"username":"johnny", | |
"email":"[email protected]", | |
"addresses":[ | |
{ | |
"address1":"195 E. 4th Ave", | |
"city":"San Mateo", | |
"state":"CA", | |
"zip":"94401" | |
} | |
], | |
"settings":{ | |
"wantsGeneralNotifications":true, | |
"wantsUpdates":true | |
} | |
} | |
""" | |
val json = parse(jsonString) | |
val userObject = json.extract[User] | |
// write back to json | |
val jsonString = write(user) | |
// saving to mongodb with salat | |
import com.novus.salat._ | |
import com.novus.salat.global._ | |
val dbo = grater[User].asDBObject(user) | |
userCollection.save(dbo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment