Kotlinx.serialization: [FEATURE] support XML serialazation

Created on 4 Aug 2018  Â·  16Comments  Â·  Source: Kotlin/kotlinx.serialization

Pretty self explained, is it planned in the future?

feature format request

Most helpful comment

It would be great if this would work with Native. The lack of Native support recently meant I could not propose Kotlin MP as a solution as it needed lots of XML related work.

All 16 comments

It is not currently on roadmap – first of all, we want to polish technology of auto-generated serializers so other people can implement and contribute different formats and use-cases, because there can be a lot of them. Later library can be filled with both official and community implementations.

I heard @pdvrieze tried to do this, is your project finished and open-sourced or you've found serious obstacles to do that?

@sandwwraith @AregevDev I have managed to do this (https://github.com/pdvrieze/ProcessManager/tree/master/xmlutil), but it is not an independent library. It is working, but not tested a lot. As it pushes the framework it does require Kotlin 1.2.50 at least due to serialization bugs (mainly in the compiler extension).

If there is interest I can make it an independent project (or 2, an xml wrapper library). A complication here is that xml is not uniformly supported across android, jvm and js. xmlutil/core is basically a forwarding library that has 3 platform sublibraries providing a pull api for android, jvm and javascript. Javascript is implemented but untested.

Serializing xml then is also a multiplatform library that provides serialization for XML. It works more or less (less deep documentation), for my purposes (rather complex xml serialization/deserialization where I must be able to serialize nested XML (and get all the namespaces correct)). It pushes the limits of what the serialization framework can do. In particular for correct functioning it does some nasty structure "sensing" by serializing/deserializing to a "canary" writer/reader.

One feature it has is that it is actually able to handle parent objects with multiple lists of children where these children are directly contained in the parent tag, even possibly mixed:

<parent>
  <childA/>
  <childB/>
  <childA/>
  <childB/>
</parent>

Btw. Based on the library size/complexity it is not suitable at the moment to include (it or something similar) in the standard serialization framework. It does nicely kick the tires of the serialization though.

I have split out the project at: https://github.com/pdvrieze/xmlutil
I will still have to create some bintray releases. The releases are now on bintray (and adding to jcenter has been requested for the serialization plugin - the others are already approved). Note that the serialization library comes in two variants, android and jvm. They only differ in the pom file to have an automatic dependency on the concrete implementation for that platform.

Any update on when this might be put on the roadmap?

My library is now reasonably stable (at least compared to kotlinx.serialization itself). I've just updated it to work with 0.13.0 and am happy to help with fixing issues/limitations that people have.

The library is intended to support existing XML formats and to generate conventional XML rather than "I don't care what it looks like" XML. As a consequence there are quite some knobs and buttons to configure it if you want to, and it similarly uses custom annotations (including for the tag names to use - it will use @SerialName where given, but its semantics don't match XML).

However, you can just use it like the JSON format if you don't want to customize and things should work.

@sandwwraith - Any updates on this or alternate approaches that could be followed until this feature gets ready ? Thanks.

@juechemparathy XML serialization is supported by @pdvrieze plugin. It works fine for me and Paul promptly fixes all bugs. Have you tried it?

@juechemparathy I'm not guaranteeing full free of bugs, but I'll try to fix it. One limitation is that it currently does not support native at all. The library just forwards to native xml libraries rather than trying to implement an xml parser/serializer. Anyone wanting to contribute it is welcome. Contributing gradle rules to generate kdoc would also be welcome.

It would be great if this would work with Native. The lack of Native support recently meant I could not propose Kotlin MP as a solution as it needed lots of XML related work.

when I npm install xmlutil-serialization. It has an error : 'xmlutil-serialization@latest' is not in the npm registry。
I use kotlin multiplant。 Is it published? @pdvrieze

It is only published with gradle/jcenter (for now only the own repository works correctly due to overlapping folder/naming issues). I wouldn't know where to start to support NPM, but you can just depend on it as a gradle dependency.

Any updates on where this is in the roadmap or if it is on the roadmap at all? I was recently looking into building a service with ktor and need to support xml serialization and found that kotlinx.serialization doesn't support XML natively.

@nek4life xmlutil is a powerful and well-maintained library. Why do you need something else?

The only thing that would improve it is native binding. But, I do not think that it is that necessary.

@altavir I can take another look. I thought kotlin.serialization was stable now and it said in the readme that the library was unstable because kotlin.serialization was unstable so wasn't sure what the status was with the library. I will definitely give it a shot.

Well, it is a community library, so no guarantees. But I am using it for both JVM and JS and quite happy with it. @pdvrieze is also fixing all problems in a short time.

Overall I would say that the library is stable. There have been some bugs, but not too many and some related to handling odd approaches to using xml. The main remaining instability is that I want to look at changing the default configuration, as well as fix up the maven coordinates. Both could lead to requiring small code changes. Probably I also need to update the status in the documentation.

Was this page helpful?
0 / 5 - 0 ratings