Created
May 17, 2013 08:25
-
-
Save srenault/5597738 to your computer and use it in GitHub Desktop.
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
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