Uberto Barbini
04/10/2021, 7:12 PMUberto Barbini
04/10/2021, 7:12 PMUberto Barbini
04/10/2021, 7:13 PMUberto Barbini
04/10/2021, 7:13 PMUberto Barbini
04/10/2021, 7:14 PMRazvan
04/10/2021, 7:16 PMUberto Barbini
04/10/2021, 7:17 PMUberto Barbini
04/10/2021, 7:17 PMRazvan
04/10/2021, 7:19 PM"2021-04-10T19:00:51.170026Z"
is not good have to be "2021-04-10T19:00:51.170Z"
Uberto Barbini
04/10/2021, 7:20 PMRazvan
04/10/2021, 7:21 PMUberto Barbini
04/10/2021, 7:22 PMUberto Barbini
04/11/2021, 2:13 PMRazvan
06/21/2021, 6:06 PM{
"internet-proxy": [
{
"label": "internet-proxy",
"provider": null,
"credentials": {
"username": "red-haze",
"password": "xxxxxxxxx",
}
}
]
}
and I just want to map to a list of Credentials
objects with the username and password. Is it possible to avoid having to create a global object that just have the internet-proxy
field as a list of another object that have a credentials
field of type Credentials
Uberto Barbini
07/15/2021, 12:59 PMUberto Barbini
07/15/2021, 1:00 PMUberto Barbini
07/15/2021, 1:02 PMRazvan
10/10/2021, 4:08 PMdata class TitleRequest(
val id: String,
val type: TitleType? = null
)
enum class TitleType(val label: String) {
Movie("movie"), Series("series"), Episoode("episode")
}
but the type
field in the json I want to be the TitleType.label
value instead the Enum’s name.
{
id: "Title string",
type: "movie"
}
ThanksUberto Barbini
10/10/2021, 4:20 PMobject JTitleType : JStringRepresentable<TitleType>() {
override val cons: (String) -> TitleType = ???
override val render: (TitleType) -> String = TitleType::label
}
Uberto Barbini
10/10/2021, 4:30 PMUberto Barbini
10/10/2021, 8:07 PMRazvan
10/10/2021, 10:01 PMprivate val type by obj(JTitleType, TitleRequest::type)
and a few tricks
data class TitleRequest(
val id: String,
val type: TitleType?
)
enum class TitleType(val label: String) {
Movie("movie"), Series("series"), Episoode("episode");
companion object {
fun fromLabel(label: String) = values().first { it.label == label }
}
}
object JTitleType : JStringRepresentable<TitleType>() {
override val cons: (String) -> TitleType = ::fromLabel
override val render: (TitleType) -> String = TitleType::label
object JTitleRequest : JAny<TitleRequest>() {
private val id by str(TitleRequest::id)
private val type by obj(JTitleType, TitleRequest::type)
override fun JsonNodeObject.deserializeOrThrow(): TitleRequest =
TitleRequest(
id = +id,
type = type.getter(this).recover { null }
)
}
class KondorTest {
@Test
fun `type not null`() {
val inValue = TitleRequest("tom", TitleType.Movie)
val json = JTitleRequest.toPrettyJson(inValue).also {
println(it)
}
json shouldBe """
{
"id": "tom",
"type": "movie"
}
""".trimIndent()
}
@Test
fun `type is null`() {
val inValue = TitleRequest("tom", null)
val json = JTitleRequest.toPrettyJson(inValue).also {
println(it)
}
json shouldBe """
{
"id": "tom"
}
""".trimIndent()
}
@Test
fun `type is set to null when type is unknown`() {
val inValue = """
{
"id": "tom",
"type": "unknown"
}
""".trimIndent()
val title = JTitleRequest.fromJson(inValue).orThrow()
title shouldBe TitleRequest("tom", null)
}
}
Uberto Barbini
10/11/2021, 6:45 AMUberto Barbini
10/11/2021, 6:47 AMUberto Barbini
10/11/2021, 8:45 AMUberto Barbini
10/11/2021, 8:46 AMRazvan
11/12/2021, 5:04 PM{ total: "10" }
But I know it is an Int and want to map it in my object as a int
data class Response(val total: Int?)
Can’t figure out how to write the converter to do that ?
I was thinking about something like this but couldn’t make it work.
JResponse : JAny<Response>() {
private val total by str???
override fun JsonNodeObject.deserializeOrThrow(): Response = Response(total = (+total)?.toIntOrNull())
}
dmcg
11/18/2023, 3:06 PM{
"places" : [
{ "lat" : 52.22, "long" : 22.11 },
...
]
}
dmcg
11/18/2023, 3:34 PMdata class Root(val places: List<Place>)
data class Place(val displayName: DisplayName)
data class DisplayName(val text: String)
I can use
object JRoot : JAny<Root>() {
val places by array(JPlace, Root::places)
override fun JsonNodeObject.deserializeOrThrow() = Root(+places)
}
object JPlace : JAny<Place>() {
val displayName by obj(JDisplayName, Place::displayName)
override fun JsonNodeObject.deserializeOrThrow() = Place(displayName = +displayName)
}
object JDisplayName : JAny<DisplayName>() {
val text: JsonProperty<String> by str(DisplayName::text)
override fun JsonNodeObject.deserializeOrThrow() = DisplayName(+text)
}
gypsydave5
02/06/2024, 10:04 AM