gpt4 book ai didi

java - MOXy 编码期间生成无效 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 07:06:49 24 4
gpt4 key购买 nike

以下代码在编码期间生成无效的 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));
}

}

最佳答案

我已经能够重现您所看到的错误。您可以使用以下错误来跟踪我们在此问题上的进展:

关于java - MOXy 编码期间生成无效 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21235716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com