Created
October 20, 2023 14:02
-
-
Save satyagraha/cbb818b2edd27ebe38d72bfd0f58ee11 to your computer and use it in GitHub Desktop.
Circe Transform App
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
package example | |
import io.circe._ | |
import io.circe.parser._ | |
import io.circe.optics.JsonOptics._ | |
import monocle.function.Plated | |
object CirceTransformApp { | |
def main(args: Array[String]): Unit = { | |
val json: Json = parse( | |
""" | |
{ | |
"Order": { | |
"Customer": { | |
"Name": "Custy McCustomer", | |
"ContactDetails": { | |
"Address": "1 Fake Street, London, England", | |
"Phone": "0123-456-789" | |
} | |
}, | |
"Items": [{ | |
"Id": 123, | |
"Description": "banana", | |
"Quantity": 1 | |
}, { | |
"Id": 456, | |
"Description": "apple", | |
"Quantity": 2 | |
}], | |
"Total": 123.45 | |
} | |
} | |
""").getOrElse(Json.Null) | |
val result = Plated.transform[Json] { j => | |
// println(j) | |
// println() | |
j.asObject match { | |
case Some(obj) => Json.fromFields(obj.toIterable.map { case (key, value) => | |
val c = key.toCharArray | |
c(0) = Character.toLowerCase(c(0)) | |
new String(c) -> value }) | |
case None => j | |
} | |
}(json) | |
println(result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment