LocalDate序列化错误

时间:2016-12-29 05:23:09

标签: java serialization

序列化包含LocalDate字段的类会导致以下错误:

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

主类代码:

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();        
    }
}

员工类代码:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

以下代码修改解决了这个问题:

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());
    }
}

序列化数据如下所示:

<?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>

涉及单个LocalDate字段的替代实现:

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());
    }
}

序列化数据如下所示:

<?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>

请注意,可以修改最后一个实现以包含其他字段,例如LocalDateTime序列化中的小时和分钟。