https://kotlinlang.org logo
Join SlackCommunities
Powered by
# kondor-json
  • u

    Uberto Barbini

    04/10/2021, 7:12 PM
    that's great, let me know any help
  • u

    Uberto Barbini

    04/10/2021, 7:12 PM
    what I'm developing now is a automatic generator for the converters from the data class
  • u

    Uberto Barbini

    04/10/2021, 7:13 PM
    it may be not 100% correct but it's convenient for the first draft quickly
  • u

    Uberto Barbini

    04/10/2021, 7:13 PM
    also, I'm interested in KN as well
  • u

    Uberto Barbini

    04/10/2021, 7:14 PM
    ah and making it working with InputStream. We will have a 200Mb json to handle here 🙂
  • r

    Razvan

    04/10/2021, 7:16 PM
    By native I was meaning GraalVM build-native, KN wait till some more libs (like IO) are ready to try it more…
  • u

    Uberto Barbini

    04/10/2021, 7:17 PM
    ah, that's working without problems, I've an example
  • u

    Uberto Barbini

    04/10/2021, 7:17 PM
    the hardest part was to discover the magic gradle configuration... 🙂
  • r

    Razvan

    04/10/2021, 7:19 PM
    By stupid corp rules we also have some partners that for dates format requires just 3 numbers to the nanoseconds any way to specify the date output format ? Like:
    "2021-04-10T19:00:51.170026Z"
    is not good have to be
    "2021-04-10T19:00:51.170Z"
  • u

    Uberto Barbini

    04/10/2021, 7:20 PM
    you just need to define your own JInstant or whatever and use yours 🙂
  • r

    Razvan

    04/10/2021, 7:21 PM
    Oh yes is that simple thanks…
  • u

    Uberto Barbini

    04/10/2021, 7:22 PM
    we have a few very specific converters for our types, super easy
  • u

    Uberto Barbini

    04/11/2021, 2:13 PM
    @Razvan I've added explicit null option today to Kondor 🙂
    r
    • 2
    • 4
  • r

    Razvan

    06/21/2021, 6:06 PM
    Hi Uberto, I was wandering if Kondor can du partial mapping. Let me explain, I have this json
    Copy code
    {
    	"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
  • u

    Uberto Barbini

    07/15/2021, 12:59 PM
    sorry @Razvan, for some reason Slack didn't notify me
  • u

    Uberto Barbini

    07/15/2021, 1:00 PM
    actually if I understand your problem correctly there is a very elegant solution in Kondor using its profunctor interface
    r
    • 2
    • 5
  • u

    Uberto Barbini

    07/15/2021, 1:02 PM
    next time put my name or just pm me! :)
    👍 1
  • r

    Razvan

    10/10/2021, 4:08 PM
    Hello @Uberto Barbini I’ve been struggling to write the converter for a specific use case. It’s a case of a nullable Enum field with properties. I tried the generator but wasn’t very helpful and couldn’t find any exemples of that. I want to serialise `TitleRequest`:
    Copy code
    data 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.
    Copy code
    {
       id: "Title string",
       type: "movie"
    }
    Thanks
  • u

    Uberto Barbini

    10/10/2021, 4:20 PM
    If you have a function that return the Enum from the label you can write a JTitleType converter:
    Copy code
    object JTitleType : JStringRepresentable<TitleType>() {
        override val cons: (String) -> TitleType = ???
        override val render: (TitleType) -> String = TitleType::label
    }
    r
    • 2
    • 6
  • u

    Uberto Barbini

    10/10/2021, 4:30 PM
    @Razvan let me know, in case I can add an example
  • u

    Uberto Barbini

    10/10/2021, 8:07 PM
    checking about it, there is no reason why JStringRepresentable shouldn't work with nullable types. I've released v. 1.6.8 with your example for nullable JStringRepresentable (I hope it's ok)
  • r

    Razvan

    10/10/2021, 10:01 PM
    Managed to make it work using
    Copy code
    private val type by obj(JTitleType, TitleRequest::type)
    and a few tricks
    Copy code
    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)
        }
    }
  • u

    Uberto Barbini

    10/11/2021, 6:45 AM
    if you update to new version (1.6.8) it support nullable so it should work fine
  • u

    Uberto Barbini

    10/11/2021, 6:47 AM
    I like this idea: "type.getter(this).recover { null }" I can create a type.orNull()...
    👍 1
  • u

    Uberto Barbini

    10/11/2021, 8:45 AM
    I'm thinking about some changes on nullability but I need to think a little more about it.
  • u

    Uberto Barbini

    10/11/2021, 8:46 AM
    the use case seems legit to me (let some fields to null if parse fails rather than failing the whole operation)
    r
    • 2
    • 1
  • r

    Razvan

    11/12/2021, 5:04 PM
    Hello @Uberto Barbiniback again with another question I can’t figure out. I got a service that reponds a String:
    Copy code
    { total: "10" }
    But I know it is an Int and want to map it in my object as a int
    Copy code
    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.
    Copy code
    JResponse : JAny<Response>() {
      private val total by str???
    
      override fun JsonNodeObject.deserializeOrThrow(): Response = Response(total = (+total)?.toIntOrNull())
    }
    u
    • 2
    • 5
  • d

    dmcg

    11/18/2023, 3:06 PM
    Is there any documentation on Lists / Arrays? - I’m having trouble seeing how to deserialise eg
    Copy code
    {
        "places" : [
           { "lat" : 52.22, "long" : 22.11 },
           ...
        ]
    }
  • d

    dmcg

    11/18/2023, 3:34 PM
    Ah, OK, watched the video. The readme really needs an example of a nested array and object. For posterity, if I have
    Copy code
    data class Root(val places: List<Place>)
        data class Place(val displayName: DisplayName)
        data class DisplayName(val text: String)
    I can use
    Copy code
    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)
        }
  • g

    gypsydave5

    02/06/2024, 10:04 AM
    @Uberto Barbini heeeey, if you're about - latest release just blew up on upgrade:
    u
    • 2
    • 9