Last active
August 29, 2015 14:22
-
-
Save monkeygroover/6285bc400d1428d0eb20 to your computer and use it in GitHub Desktop.
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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{
"nested": {
}
}
{
"nested": {
"optionalBlah": ""
}
}
{
"nested": {
"optionalBlah": "blah"
}
}
{
"nested": {
}
}
{
"nested": {
}
}
{
"nested": {
"optionalBlah": "blah"
}
}