Re: [Simple-support] Creating a "short" serialization
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2007-12-10 19:43:05
|
Hi,
This will give you both worlds. Why not do the
following:
public Object myResolveMethod() {
if(someProperty) {
return new SomeOtherObject(myProperty1,
myProperty2);
}
return this; // Here I am saying don't read resolve
}
When you return "this" from the method annotated with
@Resolve annotation you are saying not to resolve to
something different. Also, you don't need to return a
different type.
@Root
public class SomeObject {
@Element(required=false)
private String text;
@Attribute(required=false)
private int version;
@Attribute
private String key;
public SomeObject() {
super();
}
public SomeObject(String key) {
this.key = key;
}
@Validate
public void validate() {
// do some validation here...
}
@Replace
private Object writeReplace() {
return new SomeObject(key); // Here I provide
the same type with a subset of the properties
}
}
Hope this helps.
Niall
--- Ben Wolfe <si...@be...> wrote:
>
> I'm not sure this will work for me. I want to have
> both worlds. I want
> to be able to serialize our objects to just their
> primary keys sometimes
> and at others be able to serialize all @Attributes,
> @Elements, etc on
> the object to xml.
>
> Ben
>
> Niall Gallagher wrote:
> > Hi,
> >
> > There is a way you could do this, don't know how
> well
> > it would suit you. But the @Resolve and @Replace
> > annotations are used in situations like this.
> Anything
> > you can do with the readResolve and writeReplace
> for
> > conventional object serialization can be done with
> > these annotations. Here is an example of what you
> > might do.
> >
> >
> > @Root
> > public class CompleteObject {
> >
> > @Attribute
> > private String name;
> >
> > @Element
> > private String id;
> >
> > @Element
> > private Stirng address;
> >
> > public CompleteObject() {
> > super();
> > }
> >
> > public CompleteObject(String name, String id) {
> > this.name = name;
> > this.id = id;
> > }
> >
> > @Replace
> > private Object getPrimaryKey() {
> > return new PrimaryKey(name, id);
> > }
> >
> > @Root
> > private static class PrimaryKey {
> >
> > @Attribute
> > private String name;
> >
> > @Element
> > private String id;
> >
> > public PrimaryKey() {
> > super();
> > }
> >
> > public PrimaryKey(String name, String id) {
> > this.name = name;
> > this.id = id;
> > }
> >
> > @Resolve
> > private Object getCompleteObject() {
> > return new CompleteObject(name, id);
>
> >
> > }
> > }
> > }
> >
> > If you pair this with a custom Strategy
> implementation
> > then you can ensure the class="" attributes can be
> > replaced with other tokens to indicate what
> objects to
> > use. This above example is obviously not very
> useful,
> > but when used properly @Replace and @Resolve can
> be
> > quite powerful.
> >
> > Niall
> >
> > --- Ben Wolfe <si...@be...> wrote:
> >
> >> 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
> >>
> >>
> >
>
-------------------------------------------------------------------------
> >> SF.Net email is sponsored by:
> >> Check out the new SourceForge.net Marketplace.
> >> It's the best place to buy or sell services for
> >> just about anything Open Source.
> >> http://sourceforge.net/services/buy/index.php
> >> _______________________________________________
> >> Simple-support mailing list
> >> Sim...@li...
> >>
> >
>
https://lists.sourceforge.net/lists/listinfo/simple-support
> >
> >
> >
> >
>
____________________________________________________________________________________
> > Looking for last minute shopping deals?
> > Find them fast with Yahoo! Search.
>
http://tools.search.yahoo.com/newsearch/category.php?category=shopping
> >
> >
>
-------------------------------------------------------------------------
>
=== message truncated ===
____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
|