6 Replies Latest reply: Sep 21, 2011 6:50 PM by 843790 RSS

    XMLEncoder and BigDecimal

    843790
      Anybody know how to successfully use XMLEncoder with a BigDecimal?

      I imagine it's creating some sort of PersistenceDelegate but any insight would be sincerely appreciated.

      btw, seems to me there should be a default delegate for this class. I'm using 1.4.2
        • 1. Re: XMLEncoder and BigDecimal
          843790
          Hi,
          here a snippet to use to persist a BigDecimal:
          xmlEncoder.setPersistenceDelegate(BigDecimal.class,
          new DefaultPersistenceDelegate() {
          protected Expression instantiate(Object oldInstance, Encoder out) {
          BigDecimal bd = (BigDecimal) oldInstance;
          return new Expression(oldInstance, oldInstance.getClass(), new",
          new Object[]{bd.toString()}
          );
          }
          });

          cheers!
          • 2. Re: XMLEncoder and BigDecimal
            843790
            This works for BigDecimal properties that aren't initialized, i.e. null. But if you initialize the property, then this won't work unless you override mutatesTo in addition to instantiate mentioned above:

            protected boolean mutatesTo(Object oldInstance, Object newInstance) {
            return oldInstance.equals(newInstance);
            }
            • 3. Re: XMLEncoder and BigDecimal
              843790
              I think you left out a piece of code in the "return new Expression" bit.
              What string are you trying to pass as
              new",
              ?
              • 4. Re: XMLEncoder and BigDecimal
                843790
                Since this post seems to be only place on the entire Internet about this important topic, I am reposting the complete solution directly from tested code. While this is a complete example of converting an arbitrary bean into an XML string representation, this particular topic is concerned with the statement "xmlEncoder.setPersistenceDelegate..."

                import java.math.BigDecimal;
                import java.io.ByteArrayOutputStream;
                import java.io.BufferedOutputStream;
                import java.beans.Encoder;
                import java.beans.Expression;
                import java.beans.DefaultPersistenceDelegate;
                import java.beans.PersistenceDelegate;
                import java.beans.XMLEncoder;

                ABeanContainingBigDecimalFields anyBean;
                ByteArrayOutputStream encodedOutput = new ByteArrayOutputStream();
                XMLEncoder xmlEncoder = new XMLEncoder(new BufferedOutputStream(encodedOutput));
                xmlEncoder.setPersistenceDelegate(BigDecimal.class, new DefaultPersistenceDelegate() {
                protected Expression instantiate(Object oldInstance, Encoder out) {
                BigDecimal bd = (BigDecimal) oldInstance;
                return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[]{bd.toString()} );
                }

                protected boolean mutatesTo(Object oldInstance, Object newInstance) {
                return oldInstance.equals(newInstance);
                }
                }
                );
                xmlEncoder.writeObject(anyBean);
                xmlEncoder.close();
                String xmlEncodedBean = encodedOutput.toString();
                • 5. Re: XMLEncoder and BigDecimal
                  843790
                  I just want to say thanks for providing this solution. I was tearing my hair out over why it wouldn't serialize a Serializable... a BigDecimal!
                  • 6. Re: XMLEncoder and BigDecimal
                    843790
                    I cannot get this code to work with BigDecimal at all. I have a been:
                    /**
                     * Created by IntelliJ IDEA.
                     * User: jmaher
                     * Date: Jul 2, 2008
                     * Time: 10:07:09 AM
                     * Copyright (c) 2006, Coast Consulting, Inc.
                     */
                    and I apply your exact code above and the AMT does not come out in the XML:

                    I get :
                    <?xml version="1.0" encoding="UTF-8"?>
                    <java version="1.6.0_02" class="java.beans.XMLDecoder">
                    <object class="Fee">
                    <void property="rat">
                    <object class="Fee$Rate" method="valueOf">
                    <string>PerTransaction</string>
                    </object>
                    </void>
                    </object>
                    </java>


                    TWO QUESTIONS:
                    1) WHY does Sun require that we do something special to handle BigDecimal, particularly if the seem to handle these 1.5+ enums just fine and
                    2) Can you get the bean above to work?

                    using JDK 1.6?

                    Edited by: EJP on 22/09/2011 09:49: Moderated: deleted copyrighted code. Posting copyright code here is a violation of the Terms of Use.