Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalDate serialization error

Serializing a class that contains a LocalDate field causes the following error:

java.lang.InstantiationException: java.time.LocalDate
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...

Main class code:

package javaapplication15;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;

/**
 *
 * @author hoshantm
 */
public class JavaApplication15 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        JavaApplication15 japp = new JavaApplication15();
        japp.serializaData();
    }

    public void serializaData() throws FileNotFoundException, IOException {
        XMLEncoder e = new XMLEncoder(new BufferedOutputStream(
                        new FileOutputStream("d:\\temp\\Test.xml")));
        Employee emp = new Employee();
        emp.setFirstName("John");
        emp.setLastName("Doe");
        emp.setBirthDate(LocalDate.parse("1965-01-01"));
        e.writeObject(emp);
        e.close();        
    }
}

Employee class code:

package javaapplication15;

import java.io.Serializable;
import java.time.LocalDate;

public class Employee implements Serializable {

    private String firstName;
    private String lastName;
    private LocalDate birthDate;
    public LocalDate employmentDate;

    public LocalDate getEmploymentDate() {
        return employmentDate;
    }

    public void setEmploymentDate(LocalDate employmentDate) {
        this.employmentDate = employmentDate;
    }

    public LocalDate getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(LocalDate date) {
        birthDate = date;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}
like image 519
Tarik Avatar asked Mar 01 '26 14:03

Tarik


1 Answers

The following modification of the code solves the problem:

package javaapplication15;

import java.beans.Encoder;
import java.beans.Expression;
import java.beans.PersistenceDelegate;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;

/**
 *
 * @author hoshantm
 */
public class JavaApplication15 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        JavaApplication15 japp = new JavaApplication15();
        japp.serializaData();
    }

    public void serializaData() throws FileNotFoundException, IOException {
        XMLEncoder e = new XMLEncoder(new BufferedOutputStream(
                new FileOutputStream("d:\\temp\\Test.xml")));
        Employee emp = new Employee();
        emp.setFirstName("John");
        emp.setLastName("Doe");
        emp.setBirthDate(LocalDate.parse("1965-01-01"));

        e.setPersistenceDelegate(LocalDate.class,
                new PersistenceDelegate() {
                    @Override
                    protected Expression instantiate(Object localDate, Encoder encdr) {
                        return new Expression(localDate,
                                LocalDate.class,
                                "parse",
                                new Object[]{localDate.toString()});
                    }
                });

        e.writeObject(emp);
        e.close();

        XMLDecoder d = new XMLDecoder(new BufferedInputStream(
                new FileInputStream("d:\\temp\\Test.xml")));

        Employee e2 = (Employee) d.readObject();
        System.out.println(e2.getFirstName());
        System.out.println(e2.getLastName());
        System.out.println(e2.getBirthDate());
        System.out.println(e2.getEmploymentDate());
    }
}

Serialized data appears as follows:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_92" class="java.beans.XMLDecoder">
 <object class="javaapplication15.Employee">
  <void property="birthDate">
   <object class="java.time.LocalDate" method="parse">
    <string>1965-01-01</string>
   </object>
  </void>
  <void property="firstName">
   <string>John</string>
  </void>
  <void property="lastName">
   <string>Doe</string>
  </void>
 </object>
</java>

Alternate implementation involving individual LocalDate fields:

package javaapplication15;

import java.beans.Encoder;
import java.beans.Expression;
import java.beans.PersistenceDelegate;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;

/**
 *
 * @author hoshantm
 */
public class JavaApplication15 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        JavaApplication15 japp = new JavaApplication15();
        japp.serializaData();
    }

    public void serializaData() throws FileNotFoundException, IOException {
        XMLEncoder e = new XMLEncoder(new BufferedOutputStream(
                new FileOutputStream("d:\\temp\\Test.xml")));
        Employee emp = new Employee();
        emp.setFirstName("John");
        emp.setLastName("Doe");
        emp.setBirthDate(LocalDate.parse("1965-01-01"));

        e.setPersistenceDelegate(LocalDate.class,
                new PersistenceDelegate() {
                    @Override
                    protected Expression instantiate(Object obj, Encoder encdr) {
                        LocalDate localDate = (LocalDate) obj;
                        return new Expression(localDate,
                                LocalDate.class,
                                "of",
                                new Object[] {localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth()});
                    }
                });

        e.writeObject(emp);
        e.close();

        XMLDecoder d = new XMLDecoder(new BufferedInputStream(
                new FileInputStream("d:\\temp\\Test.xml")));

        Employee e2 = (Employee) d.readObject();
        System.out.println(e2.getFirstName());
        System.out.println(e2.getLastName());
        System.out.println(e2.getBirthDate());
        System.out.println(e2.getEmploymentDate());
    }
}

Serialized data appears as follows:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_92" class="java.beans.XMLDecoder">
 <object class="javaapplication15.Employee">
  <void property="birthDate">
   <object class="java.time.LocalDate" method="of">
    <int>1965</int>
    <object class="java.lang.Enum" method="valueOf">
     <class>java.time.Month</class>
     <string>JANUARY</string>
    </object>
    <int>1</int>
   </object>
  </void>
  <void property="firstName">
   <string>John</string>
  </void>
  <void property="lastName">
   <string>Doe</string>
  </void>
 </object>
</java>

Note that the last implementation can be modified to include other fields such as hours and minutes in the serialization of LocalDateTime.

like image 131
Tarik Avatar answered Mar 03 '26 05:03

Tarik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!