Re: [Simple-support] Creating a "short" serialization
Brought to you by:
niallg
|
From: Ben W. <si...@be...> - 2007-12-14 15:13:43
|
Using @Persist and @Complete won't work for a few reasons. We're using a hibernate backend. Modifications to the object will make it into the database. Adding calls to our 20+ pojos to clear out and refill the object just isn't plausible. The @Replace/@Resolve seems to be the best bet. It doesn't effect the root class -- which is what we want. The only need would be to have it act conditionally. It looks like the @Replace method will take in a "Map sessionData" object. Is there a way I can add things to that map when I create the Persister? Or is the only way to modify that map by doing so in the @Persist/@Complete/@Replace/@Resolve methods? Thanks, Ben Niall Gallagher wrote: > Hi, > > Take a look at the following unit test. > > http://simple.svn.sourceforge.net/viewvc/simple/trunk/download/stream/src/test/java/org/simpleframework/xml/load/SubstituteTest.java > > This shows the @Replace and @Resolve methods working. > The reason you could not get your test to run is > because the very root Class within the schema cannot > be replaced, only fields of a composite class. For > instance if the class schema you defined was a field > of some other class then it would replace fine. > > With respect to you global property suggestion, why > not just use the session map to pass information > about, it will be passed to the @Persist and @Complete > annotated methods. For example: > > @Persist > private void persistMe(Map session) { > // use session here before serialization... > } > > @Complete > private void completeMe(Map session) { > // use the session here after serialization... > } > > This is just one way, there are tonnes of other ways > this can be done. > > Niall > > --- Ben Wolfe <si...@be...> wrote: > >> I can't seem to get the Replace method to work. >> I've attached a test >> method to demonstrate. Perhaps you can show me what >> I'm doing wrong. :-/ >> >> The @Replace/@Resolve methods have promise, but I >> still don't see a way >> for the person serializing to trigger it. The >> closest solution would be >> if "someProperty" in your example below were >> actually a global property >> set/retrieved via a call to a util class. However, >> our code is running >> on a server, and it is quite possible that multiple >> serializations will >> be happening at the same time -- some "primary key >> only" and some full. >> >> >public Object myResolveMethod() { >> > if(someProperty) { >> > return new SomeOtherObject(myProperty1, >> myProperty2); >> > } >> > return this; // Here I am saying don't read >> resolve >> >} >> >> I have coded up the tags possibility. I can submit >> a patch if you're >> interested. :-) >> >> Ben >> >> Niall Gallagher wrote: >>> Hi, >>> >>> Forgot to explain the following code snippit. This >>> demonstrates how an @Replace method could be used >>> instead of a @Resolve. The @Replace is used in >>> writing, it is similar to Java object >> serialization >>> writeReplace. >>> >>> @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 >>> } >>> } >>> >>> You could also use the @Persist and @Complete, the >>> method annotated with @Persist is called just >> before >>> serialization of the object, and @Complete is >> called >>> directly after. So you can modify the contents of >> the >>> object before it is serialized and bring the >> object >>> back to a known state when you have finished >>> serializing it. So you get a chance to modify the >>> object for serialization, there is an example in >> the >>> tutorial. >>> >>> Niall >>> >>> --- Niall Gallagher <gal...@ya...> >> wrote: >>>> 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 > === message truncated ===> package > org.simpleframework.xml.load; >> import java.io.StringWriter; >> >> import org.simpleframework.xml.Attribute; >> import org.simpleframework.xml.Element; >> import org.simpleframework.xml.Root; >> import org.simpleframework.xml.ValidationTestCase; >> >> /** >> * >> */ >> public class ReplaceTest extends ValidationTestCase >> { >> >> private static final String exampleReplaceMethod >> = "<example exampleId=\"123\"/>"; >> >> @Root >> private static class Example { >> >> public Example(Integer exampleId) { >> this.exampleId = exampleId; >> } >> >> @Element(required=false) >> private String exampleMessage; >> >> @Attribute(required=false) >> private Integer exampleId; >> >> @Replace >> private Object exampleReplaceMethod() { >> return new Example(exampleId); >> } >> } >> >> /** >> * This method tests the replace method >> * >> * @throws Exception >> */ >> public void testReplaceExample() throws Exception { >> >> Persister persister = new Persister(); >> Example ex = new Example(123); >> ex.exampleMessage = "test"; >> >> StringWriter buffer = new StringWriter(); >> persister.write(ex, buffer); >> >> assertEquals("The serialization is not >> right.", exampleReplaceMethod, buffer.toString()); >> >> validate(ex, persister); >> >> } >> >> } > ------------------------------------------------------------------------- >> 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 > > > > ____________________________________________________________________________________ > Never miss a thing. Make Yahoo your home page. > http://www.yahoo.com/r/hs |