[英]JAXB truncates long values
我正在使用JAXB将bean转换为JSON。 当JAXB转换long类型的bean值时,将被截断。
例:
如果my long的值是:44444444444444444
JAXB将其截断为:44444444444444450
谁能帮我解决这个问题?
谢谢
注意:我是EclipseLink JAXB(MOXy)的负责人,并且是JAXB(JSR-222)专家组的成员。
JAXB(JSR-222)规范不涉及将对象与JSON相互转换。 您遇到的问题来自利用JAXB元数据的JSON绑定实现。 下面,我将演示当将MOXy用作JSON绑定提供程序时,该用例可以完美地工作。
Java模型(Foo)
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
private long bar;
}
jaxb.properties
要将MOXy指定为JAXB提供程序,您需要在与域模型相同的程序包中包含一个名为jaxb.properties
的文件,并带有以下条目(请参阅: http : //blog.bdoughan.com/2011/05/specifying-eclipselink- moxy-as-your.html )。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource json = new StreamSource("src/forum16821525/input.json");
Foo foo = unmarshaller.unmarshal(json, Foo.class).getValue();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
input.xml中/输出
{
"bar" : 44444444444444444
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.