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
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
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());
Java.Time sample code
public static byte[] beanToXml(final Object parBean) { ClassLoader threadCL = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(parBean.getClass().getClassLoader());
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