Well, what do you want to achieve in the first pla...
# announcements
s
Well, what do you want to achieve in the first place?
🧵 2
🤨 1
r
I want to declare a statically type checked data structure in a way that mirrors as closely as possible the shape of the actual data that I can parse from XML/JSON/YAML/whatever .
âž• 1
j
jackson should do this
feel free to override any (de)serializers too with your data classes
r
There’s context just previously in the channel; I’m not asking about how to serialize/deserialize.
j
statically type checked data structure in a way that mirrors as closely as possible the shape of the actual data
but serializing and deserializing would do this
i'm sorry, but maybe i'm also missing the point. are you just wanting to use different syntax?
r
Yes.
j
gotcha, one sec
r
More accurately I was suggesting a new syntax sugar that doesn’t currently exist - something like anonymous inner classes, or lambdas for SAM.
j
ahh
if you want something like this:
Copy code
<a>
  <b>
    <c>
      <foo>"hello"</foo>
      <bar>45</bar>
    </c>
  </b>
</a>
you'd do something like this:
Copy code
data class A(val b: B)
  data class B(val c: C)
  data class C(val foo: String, val bar: Int)

  @Test
  fun test() {
    val test = XmlMapper().writeValueAsString(A(B(C(foo = "test", bar = 42))))
    println(test)
  }
you get this output from above:
Copy code
<A><b><c><foo>test</foo><bar>42</bar></c></b></A>
as with nested classes, you only get the last class you've initalized. like this:
Copy code
data class A(val b: B) {
    data class B(val c: C) {
      data class C(val foo: String, val bar: Int)
    }
  }

  @Test
  fun test() {
    val test = XmlMapper().writeValueAsString(A.B.C(foo = "test", bar = 42))
    println(test)
  }
gives an output of:
Copy code
<C><foo>test</foo><bar>42</bar></C>
but i do understand what you're saying.
r
Thanks, but I know how to use jackson to serialize to and from kotlin data classes.
j
you could also do this... which is similar to your original post:
Copy code
data class A(
      val b: Any = object {
        val c: Any = object {
          val foo: String = ""
          val bar: Int = 0
        }
      }
  )

  @Test
  fun test() {
    val test = XmlMapper().writeValueAsString(A())
    println(test)
  }
that outputs:
Copy code
<A><b><c><foo></foo><bar>0</bar></c></b></A>
would be nice to declare data classes rather anonymous objects
r
The type of b & c is Any, so there’s no way to reference their properties. Fine for serializing to a data format, but no use for deserializing to kotlin values you want to use.
j
very true.