Comments (3)
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.
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.
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)
- How to work with elements that have the same name and different attributes
- Is it possible to completely ignore namespaces during deserialization? HOT 2
- Default namespace on root element and unqualified child elements HOT 5
- IndexOutOfBoundsException: There are no children HOT 3
- Support for multiple alternative names during parsing? HOT 2
- Enums as XML attributes HOT 3
- Error when integrated into android (java) application HOT 2
- Add Multiple NameSpaces for a XML Element HOT 4
- XML NameSpace Prefix is incorrect for Non-Null Attribute HOT 2
- XmlUtil.xmlCollapseWhitespace() throws StringIndexOutOfBoundsException on empty attribute HOT 2
- @XmlClassDiscriminator HOT 1
- Boolean decoding should be strict by default, or at least optionally HOT 4
- DefaultXmlSerializationPolicy deprecated constructor HOT 10
- Partial regression on checking root tag name checking HOT 4
- Wrong order of contents and string parsing HOT 5
- Comments in XML will cause native DOM parser to fail HOT 2
- XmlOtherAttributes: deserialization replaces keys by its value HOT 4
- Add version supporting serialization 1.5.1 but keeping kotlin as 1.8.10 HOT 1
- How to simply parse/serialize a Map to/from XML that has keys as tag names wrapping text values. HOT 5
- Using namespace HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xmlutil.