JodaOrg / joda-time

Joda-Time is the widely used replacement for the Java date and time classes prior to Java SE 8.
http://www.joda.org/joda-time/
Apache License 2.0
4.98k stars 985 forks source link

Not able to serialize ZonedDateTime,PeriodDuration while converting bean to xml #777

Closed solsau closed 7 months ago

solsau commented 8 months ago

I am using joda time earlier now I have migrated from DateTime(joda) to ZonedDateTime(java.time) also converted Period(joda) to PeriodDuration(java). But as ZonedDateTime and PeriodDuration does not have any constructor So I am getting error while serializing bean to xml.

I want to store dateTime value in millisecond(long) and PeriodDuration value in String in xml using.Please suggest solution

Joda sample code (Working with Joda)

public static byte[] beanToXml(final Object parBean) { ClassLoader threadCL = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(parBean.getClass().getClassLoader());

        ByteArrayOutputStream theBytes = new ByteArrayOutputStream();
        XMLEncoder theEncoder = new XMLEncoder(theBytes);
        theEncoder.setExceptionListener(myExceptionListener);

        theEncoder.setPersistenceDelegate(DateTime.class, new PersistenceDelegate()
        {
            @Override
            protected Expression instantiate(final Object parOldInstance, final Encoder parOut)
            {
                DateTime theDateTime = (DateTime) parOldInstance;
                return new Expression(parOldInstance, theDateTime.getClass(), "new", new Object[]
                {
                    theDateTime.getMillis()
                });
            }
        });

         theEncoder.setPersistenceDelegate(Period.class, new PersistenceDelegate()
        {
            @Override
            protected Expression instantiate(final Object parOldInstance, final Encoder parOut)
            {
                Period thePeriod = (Period) parOldInstance;
                return new Expression(parOldInstance, thePeriod.getClass(), "new", new Object[]
                {
                    thePeriod.toString()
                });
            }
        });

        theEncoder.writeObject(parBean);
        theEncoder.close();
        return theBytes.toByteArray();
    }
    finally
    {
        Thread.currentThread().setContextClassLoader(threadCL);
    }
}

Java.Time sample code

public static byte[] beanToXml(final Object parBean) { ClassLoader threadCL = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(parBean.getClass().getClassLoader());

        ByteArrayOutputStream theBytes = new ByteArrayOutputStream();
        XMLEncoder theEncoder = new XMLEncoder(theBytes);
        theEncoder.setExceptionListener(myExceptionListener);

        theEncoder.setPersistenceDelegate(ZonedDateTime.class, new PersistenceDelegate()
        {
            @Override
            protected Expression instantiate(final Object parOldInstance, final Encoder parOut)
            {
                ZonedDateTime theDateTime = (ZonedDateTime) parOldInstance;

                return new Expression(parOldInstance, theDateTime.getClass(), "new", new Object[]
                {
                    theDateTime.toInstant().toEpochMilli()
                });
            }
        });

        theEncoder.setPersistenceDelegate(PeriodDuration.class, new PersistenceDelegate()
        {
            @Override
            protected Expression instantiate(final Object parOldInstance, final Encoder parOut)
            {
                PeriodDuration thePeriod = (PeriodDuration) parOldInstance;
                return new Expression(parOldInstance, thePeriod.getClass(), "new", new Object[]
                {
                    thePeriod.toString()
                });
            }
        });

        theEncoder.writeObject(parBean);
        theEncoder.close();
        return theBytes.toByteArray();
    }
    finally
    {
        Thread.currentThread().setContextClassLoader(threadCL);
    }
}

exception in java.time code

Caused by: java.lang.RuntimeException: failed to evaluate: =Class.new(Long); at java.beans.Encoder.getValue(Encoder.java:109) at java.beans.Encoder.get(Encoder.java:252) at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:112) at java.beans.Encoder.writeObject(Encoder.java:74) at java.beans.XMLEncoder.writeObject(XMLEncoder.java:327) at java.beans.Encoder.writeExpression(Encoder.java:330) at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:454) at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:115) at java.beans.Encoder.writeObject(Encoder.java:74) at java.beans.XMLEncoder.writeObject(XMLEncoder.java:327) at java.beans.Encoder.writeObject1(Encoder.java:258) at java.beans.Encoder.cloneStatement(Encoder.java:271) at java.beans.Encoder.writeExpression(Encoder.java:329) at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:454) at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:115) at java.beans.Encoder.writeObject(Encoder.java:74) at java.beans.XMLEncoder.writeObject(XMLEncoder.java:327) at java.beans.Encoder.writeObject1(Encoder.java:258) at java.beans.Encoder.cloneStatement(Encoder.java:271) at java.beans.Encoder.writeStatement(Encoder.java:301) at java.beans.XMLEncoder.writeStatement(XMLEncoder.java:400) ... 85 common frames omitted

jodastephen commented 7 months ago

https://stackoverflow.com/questions/58809573/how-to-serialize-java-zoneddatetime-to-xml-file