Skip to content

Instantly share code, notes, and snippets.

@srenault
Created May 17, 2013 08:25
Show Gist options
  • Save srenault/5597738 to your computer and use it in GitHub Desktop.
Save srenault/5597738 to your computer and use it in GitHub Desktop.
def flattenObject(obj: JsObject, prefix: Option[String] = None) :Seq[(String, JsValue)] = {
obj.fields.toList match {
case ("$date",_) :: Nil if prefix.isDefined => Seq(prefix.get -> obj)
case ("$date",_) :: Nil => throw new Exception("Can't save $date at the first level.")
case fs => fs.flatMap { t =>
val name = prefix.map(_ + ".").getOrElse("") + t._1
t._2 match {
case obj: JsObject => flattenObject(obj, Some(name))
case value => Seq(name -> value)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment