Giter VIP home page Giter VIP logo

Comments (3)

pdvrieze avatar pdvrieze commented on May 12, 2024 1

Unfortunately the format of the third party program doesn't quite work according to the way XML was designed/intended to work. I see two options to address this:

  • In the case that the child content cooperates you can do the following: have a serialization-only type that contains all properties (some optional) and then have a custom serializer for fruit that gets the appropriate fruit subtype for the child content (and serializes properly as well). This depends on the types not being wildly incompatible, in particular in terms of child content. The advantage is low complexity and no need to special case XML.
  • Use a filter to transform the xml on input/output. An example of how this works can be seen in https://github.com/pdvrieze/xmlutil/blob/master/examples/DYNAMIC_TAG_NAMES.md . In that case you probably want to enable automatic polymorphism (get rid of the nesting) and then on reading use the attribute to translate <fruit type="apple" ... into <Apple .... And on writing the other way around

from xmlutil.

floscher avatar floscher commented on May 12, 2024

I think I need something like https://github.com/Kotlin/kotlinx.serialization/blob/4bd949b0634fd498f847b1e059826cc7e67adf07/formats/json/commonMain/src/kotlinx/serialization/json/JsonContentPolymorphicSerializer.kt , but for XML.

When extending that serializer you just need to implement

fun selectDeserializer(element: JsonElement): DeserializationStrategy<out T>

which can inspect the content of the JSON element and decide based on that, which deserializer to use.

from xmlutil.

floscher avatar floscher commented on May 12, 2024

Thank you for the answer! I think I'll then go for something like your first solution, that looks easier to me. In my case there are only three possible subtypes and about half of the attributes in each type are common for all subtypes, so the overhead should be manageable, when duplicating the properties to a new class just for serializing.

I've put a simplified version of what I'm going to do in my project at: https://github.com/floscher/xmlutil/tree/polymorphic-fruit-example/examples/src/main/kotlin/net/devrieze/serialization/examples/custompolymorphic
If you'd find that useful to archive as an example, feel free to pull the polymorphic-fruit-example branch.

from xmlutil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.