2014-01-20 49 views
1

以下代碼在編組期間會生成無效的JSON,但不會引發異常。 如果使用XML作爲編組格式,它將起作用。MOXy在編組期間生成的JSON無效

看起來像一個錯誤或我錯過了什麼?

我使用EclispseLink莫西2.5.1

import java.io.StringReader; 
import java.io.StringWriter; 
import java.util.ArrayList; 
import java.util.List; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import org.eclipse.persistence.jaxb.JAXBContextFactory; 
import org.eclipse.persistence.jaxb.MarshallerProperties; 
import org.eclipse.persistence.oxm.MediaType; 
import org.eclipse.persistence.oxm.annotations.XmlInverseReference; 

public final class InvalidJsonApp { 

    @XmlRootElement(name = "person") 
    static class PersonEntity { 

     private String firstname; 

     @XmlElement(name = "phone") 
     @XmlInverseReference(mappedBy = "person") 
     private List<PhoneNumberEntity> phoneNumbers; 

     public void setFirstname(String firstname) { 
      this.firstname = firstname; 
     } 

     public String getFirstname() { 
      return firstname; 
     } 

     public void setPhoneNumbers(List<PhoneNumberEntity> phoneNumbers) { 
      this.phoneNumbers = new ArrayList<>(phoneNumbers); 
     } 

     public List<PhoneNumberEntity> getPhoneNumbers() { 
      return phoneNumbers; 
     } 

     public void addPhoneNumber(PhoneNumberEntity phoneNumber) { 
      if (phoneNumbers == null) { 
       phoneNumbers = new ArrayList<>(); 
      } 
      this.phoneNumbers.add(phoneNumber); 
     } 

    } 

    @XmlRootElement(name = "phonenumber") 
    static class PhoneNumberEntity { 

     private String number; 

     @XmlElement 
     @XmlInverseReference(mappedBy = "phoneNumbers") 
     private PersonEntity person; 

     public void setNumber(String number) { 
      this.number = number; 
     } 

     public String getNumber() { 
      return number; 
     } 

     void setPerson(PersonEntity person) { 
      this.person = person; 
     } 

     PersonEntity getPerson() { 
      return person; 
     } 

    } 

    public static void main(String[] args) throws JAXBException { 
     testPhoneNumber(); 
    } 

    static void testPhoneNumber() throws JAXBException { 
     System.out.println("phoneNumber:"); 
     PersonEntity personEntity = createPersonEntity(); 
     PhoneNumberEntity phoneNumberEntity = personEntity.getPhoneNumbers().get(0); 
     String entityJson = marshal(phoneNumberEntity); 
     System.out.println(entityJson); 
     unmarshal(PhoneNumberEntity.class, entityJson); 
    } 

    static PersonEntity createPersonEntity() { 
     PersonEntity person = new PersonEntity(); 
     person.setFirstname("yvonne"); 

     PhoneNumberEntity firstPhoneNumber = new PhoneNumberEntity(); 
     firstPhoneNumber.setNumber("12345"); 
     firstPhoneNumber.setPerson(person); 

     PhoneNumberEntity secondPhoneNumber = new PhoneNumberEntity(); 
     secondPhoneNumber.setNumber("54321"); 
     secondPhoneNumber.setPerson(person); 

     person.addPhoneNumber(firstPhoneNumber); 
     person.addPhoneNumber(secondPhoneNumber); 
     return person; 
    } 

    static String marshal(Object toMarshal) throws JAXBException { 
     JAXBContext jc = JAXBContextFactory.createContext(new Class[] {toMarshal.getClass()}, null); 
     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); 
//  marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML); 
     StringWriter sw = new StringWriter(); 
     marshaller.marshal(toMarshal, sw); 
     return sw.toString(); 
    } 

    static <T> T unmarshal(Class<T> entityClass, String str) throws JAXBException { 
     JAXBContext jc = JAXBContextFactory.createContext(new Class[] {entityClass}, null); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); 
//  unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML); 
     return (T) unmarshaller.unmarshal(new StringReader(str)); 
    } 

} 

回答