<commit_before>package com.cinglevue.challenge.model

/**
 * Created with IntelliJ IDEA.
 * User: mabdullah
 * Date: 10/27/13
 * Time: 9:11 PM
 * To change this template use File | Settings | File Templates.
 */
case class School (_id: Option[Long], name: String, subjects:List[Subject])
case class Subject(_id:Option[Long], name:String, results:List[Result])
case class Result(_id:Option[Long], year:Int, score:Long)<commit_msg>Serialization & De-serialization support for model objects<commit_after>package com.cinglevue.challenge.model

import reactivemongo.bson._

/**
 * Created with IntelliJ IDEA.
 * User: mabdullah
 * Date: 10/27/13
 * Time: 9:11 PM
 * To change this template use File | Settings | File Templates.
 */
case class School (_id: Option[BSONObjectID], name: String, subjects:List[Subject]){
  def toBSON = {
    BSONDocument("name"->name, "subjects"->BSONArray(subjects.map(_.toBSON)))
  }
}
case class Subject(_id:Option[BSONObjectID], name:String, results:List[Result]){
  def toBSON = {
    BSONDocument("name"->name,"results"->BSONArray(results.map(_.toBSON)))
  }
}
case class Result(_id:Option[BSONObjectID], year:Int, score:Long){
  def toBSON = {
    BSONDocument("year"->year, "score"->score)
  }
}

object Codecs{
  implicit object SchoolBSONCodec extends BSONDocumentReader[School] with BSONDocumentWriter[School] {
    def write(t: School):BSONDocument = t.toBSON

    def read(bson: BSONDocument):School = {
      val subjects = bson.getAs[BSONArray]("subjects").get.values.collect {
        case x:BSONDocument => SubjectBSONCodec.read(x)
      }
      new School(bson.getAs[BSONObjectID]("_id"), bson.getAs[String]("name").get, subjects.toList)
    }
  }

  implicit object SubjectBSONCodec extends BSONDocumentReader[Subject] with BSONDocumentWriter[Subject] {
    def write(t: Subject) = t.toBSON // BSONDocument("name"->t.name, "results"->t.results)

    def read(bson: BSONDocument) = {
      val results = bson.getAs[BSONArray]("results").get.values.toList.collect{
        case x:BSONDocument => ResultBSONCodec.read(x)
      }
      new Subject(bson.getAs[BSONObjectID]("_id"), bson.getAs[String]("name").get, results)
    }
  }
  implicit object ResultBSONCodec extends BSONDocumentReader[Result] with BSONDocumentWriter[Result] {
    def write(t: Result) = t.toBSON

    def read(bson: BSONDocument) = {
      new Result(bson.getAs[BSONObjectID]("_id"), bson.getAs[Int]("year").get, bson.getAs[Long]("score").get)
    }
  }
}
{'fraction_non_alphanumeric': 0.14291497975708503, 'fraction_numerical': 0.0072874493927125505, 'mean_word_length': 4.641552511415525, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6611585', 'n_tokens_mistral': 865, 'n_tokens_neox': 841, 'n_words': 167}