以下代码在编组期间生成无效的JSON,但不会抛出异常。 如果将XML用作编组格式,则可以使用。
看起来像是一个错误,还是我错过了什么?
我使用的是EclispseLink MOXy 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));
}
}