[Simple-support] Creating a "short" serialization
Brought to you by:
niallg
|
From: Ben W. <si...@be...> - 2007-12-07 20:33:57
|
We have several places in our code where we could benefit from a shorter serialization. We don't need to serialize the entire object, just the primary keys of the object. I haven't been able to find a way to do this yet. Did I miss something? Is there a way to do it currently? My thoughts on how to implement it: The primary key attributes/elements would need to be annotated. The most general way to do it would be to add a String "tag" option to the "Attribute" and "Element" annotations. The tag would be a string that groups certain attributes together and can be used later. I don't know why it would be necessary right now, but we could have any number of "sets of serializations" with any named tag we want: A "Short" one that would (de)derialize to just the primary keys, a "medium" one that would (de)serialize to only certain attributes, and then the default that would work normally using all annotations (as it does now). 1) It could be done with a custom Strategy, but alas, "Strategy"s are only notified about elements (via setElement(...)). If there were a setAttribute(...) method that was called on strategies, the custom strategy could look up the Object's annoations, find the right ones, and only let certain attributes and elements be written out to the DOM. This has the benefit of the least about of core simpleframework code changes. 2) The Source object could have a "List<String> tags" object that holds the current tags to serialize on. The Source can tell the scanner what tags to look for. Only the matching tags are included as attributes and elements for objects. The Persister/Serializer will need to have a setTags() method that it can pass on to the Source. The downside of this approach is that more code would have to be changed. The benefit is that not all of the attributes/elements would be looped over unnecessarily if there is only a "short" serialization going on. I am happy to code this up and submit a patch. However, I am not sure which you would prefer. There might be a third option that I'm missing. Please point me in the right direction if that is the case. Thanks, Ben |