xmlutil icon indicating copy to clipboard operation
xmlutil copied to clipboard

Support simple dynamic serial names

Open fkrauthan opened this issue 4 years ago • 3 comments
trafficstars

I know that we now have instructions to create dynamic tags based on variables but overall this requires a lot of boilerplate code. I was wondering if there could be an easy feature enhancement. I had two ideas in mind:

1. Add a @DynamicSerialName annotation

This could be applied to ether a variable (of type String) or to a method returning a string. If the serializer encounters a class with an annotation like this (only one annotation per class allowed) it would use the value instead of the static SerialName. The only downside could be deserialization that might be a bit more complex potentially (even though it shouldn't be to hard).

2. Add @XmlAnyGetter and @XmlAnySetter annotations

Similar to Jackson this annotations could allow annotating a method that returns a Map<String, T?> to create multiple dynamic elements. This is probably a lot harder to implement.

Curious to hear your thoughts on this.

fkrauthan avatar Jan 18 '21 20:01 fkrauthan

A @DynamicSerialName feature basically conflicts with the transparent list support, unless it was to be a built-in filtering system similar to what the example code provides. I'll keep it open as a potential feature but it would add quite some complexity. Even in that case you would need to provide some sort of custom xml filters (even if they had a different API). I feel strongly that it is a better design approach to keep filtering out of the format (except perhaps some convenience invocations that would allow you to add a filter to a function that reads/writes some sort of string or character stream)

On @XmlAnyGetter and @XmlAnySetter, the way I would do this (it is mostly supported already) would be to automatically support a List<Node> element (where Node is a dom node). There is also already support for List<Any> but the elements must be registered in the serializersModule.

There is some limited support to handle reading unknown tags (at least not choking on them by implementing a custom handler). I could see if it is possible/worthwhile to change its API.

pdvrieze avatar Feb 01 '21 21:02 pdvrieze

What could also work would be a Filter System that get's the serialized raw Object and then can map each tag string to a different string (for serialization) or just from String to String for deserialization could be used? This wouldn't break the overall concept and gives the user the power to just do the mapping after the fact?

fkrauthan avatar Feb 03 '21 03:02 fkrauthan

@fkrauthan Basically that is already possible. If you pass XmlWriter or XmlReader instances to the format those can be filters rather than directly directed to a reader/writer.

pdvrieze avatar Feb 03 '21 10:02 pdvrieze