Skip to content

Instantly share code, notes, and snippets.

@monkeygroover
Last active August 29, 2015 14:22
Show Gist options
  • Save monkeygroover/6285bc400d1428d0eb20 to your computer and use it in GitHub Desktop.
Save monkeygroover/6285bc400d1428d0eb20 to your computer and use it in GitHub Desktop.
import shapeless._
import spray.json._
object Main extends App {
case class Test(optionalBlah: Option[String])
case class Test2(nested: Test)
import DefaultJsonProtocol._
implicit val testFormat = DefaultJsonProtocol.jsonFormat1(Test)
implicit val test2Format = DefaultJsonProtocol.jsonFormat1(Test2)
val none = Test2(Test(None))
val emptySome = Test2(Test(Some("")))
val some = Test2(Test(Some("blah")))
println(none.toJson.prettyPrint)
println(emptySome.toJson.prettyPrint)
println(some.toJson.prettyPrint)
val optionStringLens = lens[Test2] >> 'nested >> 'optionalBlah
//replace some with none
def normalise(in: Test2) : Test2 = optionStringLens.modify(in)(_.filter(!_.isEmpty))
println(normalise(none).toJson.prettyPrint)
println(normalise(emptySome).toJson.prettyPrint)
println(normalise(some).toJson.prettyPrint)
}
@monkeygroover
Copy link
Author

{
"nested": {

}
}
{
"nested": {
"optionalBlah": ""
}
}
{
"nested": {
"optionalBlah": "blah"
}
}
{
"nested": {

}
}
{
"nested": {

}
}
{
"nested": {
"optionalBlah": "blah"
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment