Created
December 3, 2013 14:57
-
-
Save danielkhan/7770495 to your computer and use it in GitHub Desktop.
Json, case-classes and transformations in scala
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
package models | |
import _root_.utils.Sanitizer | |
import reactivemongo.bson._ | |
import play.modules.reactivemongo.json.BSONFormats._ | |
import play.api.libs.json._ | |
import play.api.libs.json.Reads._ | |
import play.api.libs.functional.syntax._ | |
import play.api.libs.json.JsString | |
import scala.Some | |
import models.Comment._ | |
/** | |
* User: dkhan | |
* Date: 13.11.13 | |
* Time: 10:21 | |
*/ | |
case class Idea( | |
_id: Option[BSONObjectID], | |
user_id: BSONObjectID, | |
user_data: Option[JsObject], | |
created_at: Option[Long], | |
history: Option[JsArray], | |
is_draft: JsBoolean, | |
slug: Option[String], | |
name: String, | |
situation: String, | |
solution: String, | |
company: String, | |
tags: Option[JsArray] | |
) { | |
def complete(nid: BSONObjectID, nslug: String): Idea = { | |
assert(this._id == None) | |
val timestamp: Long = System.currentTimeMillis / 1000 | |
this.copy(_id = Some(nid), slug = Some(nslug), created_at = Some(timestamp)) | |
} | |
} | |
object Idea { | |
implicit val ideaWrites = { | |
( | |
(__ \ "_id").writeNullable[BSONObjectID] and | |
(__ \ "user_id").write[BSONObjectID] and | |
(__ \ "user_data").writeNullable[JsObject] and | |
(__ \ "created_at").writeNullable[Long] and | |
(__ \ "history").writeNullable[JsArray] and | |
(__ \ "is_draft").write[JsBoolean] and | |
(__ \ "slug").writeNullable[String] and | |
(__ \ "name").write[String] and | |
(__ \ "situation").write[String] and | |
(__ \ "solution").write[String] and | |
(__ \ "company").write[String] and | |
(__ \ "tags").writeNullable[JsArray] | |
)(unlift(Idea.unapply)) | |
} | |
implicit val ideaReads = { | |
( | |
(__ \ "_id").readNullable[BSONObjectID] and | |
(__ \ "user_id").read[BSONObjectID] and | |
(__ \ "user_data").readNullable[JsObject] and | |
(__ \ "created_at").readNullable[Long] and | |
(__ \ "history").readNullable[JsArray] and | |
(__ \ "is_draft").read[JsBoolean] and | |
(__ \ "slug").readNullable[String] and | |
(__ \ "name").read[String](minLength[String](5)) and | |
(__ \ "situation").read[String](minLength[String](12)) and | |
(__ \ "solution").read[String](minLength[String](12)) and | |
(__ \ "company").read[String] and | |
(__ \ "tags").readNullable[JsArray] | |
)(Idea.apply _) | |
} | |
implicit val transformFormat: Reads[JsObject] = { | |
(__ \ 'situation).json.update(of[JsString].map { | |
case JsString(situation) => JsString(Sanitizer.toMarkDown(situation)) | |
}) andThen | |
(__ \ 'solution).json.update(of[JsString].map { | |
case JsString(solution) => JsString(Sanitizer.toMarkDown(solution)) | |
}) andThen | |
(__ \ 'name).json.update(of[JsString].map { | |
case JsString(raw) => JsString(Sanitizer.toText(raw)) | |
}) andThen | |
(__ \ 'company).json.update(of[JsString].map { | |
case JsString(raw) => JsString(Sanitizer.toText(raw)) | |
}) andThen | |
(__ \ 'tags).json.update( | |
__.read[JsArray].map { | |
tags => JsArray(tags.as[List[String]].map { | |
tag => JsString(Sanitizer.toText(tag)) | |
}) | |
}.orElse(Reads.pure(new JsArray))) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment