生成json时,我可以让MOXy将字符串转换为布尔值 [英] Can I get MOXy to convert a string to a boolean when generating json
问题描述
对象模型的元素以String类型结尾
The object model has an element ended of type String
public class LifeSpan {
protected String begin;
protected String end;
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String ended;
....
但它实际上只是一个布尔值,(我不知道XmlJavaTypeAdapter注释的重要性)
but its actually only ever a boolean, (I dont know the significance of the XmlJavaTypeAdapter annotation)
以XML格式输出
<life-span><begin>1999-04</begin><ended>true</ended></life-span>
因此定义为布尔值或字符串并不重要
so doesn't really matter if defined as boolean or string
但是JSON输出是
"life-span" : {
"begin" : "1999-04",
"ended" : "true"
},
当我需要它时
"life-span" : {
"begin" : "1999-04",
"ended" : true
},
我真的无法改变对象模型以为我可以在oxml.xml文件中映射到正确的类型,并尝试
I cannot really change the object model so thought I might be able to map to the correct type in oxml.xml file , and tried
<java-type name="LifeSpan">
<java-attributes>
<xml-element java-attribute="ended" type="boolean"/>
</java-attributes>
</java-type>
但它不是那样的。
推荐答案
以下是 支持此用例的方法EclipseLink JAXB(MOXy) :
BooleanStringAdapter
XmlAdapter
允许您将一种类型的对象编组为另一种类型(参见 http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html )。在此示例中,我们希望将 String
值视为布尔值
一个。
An XmlAdapter
allows you to marshal one type of object as another type (see http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html). In this example we want to treat the String
value as a Boolean
one.
package forum11451880;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class BooleanStringAdapter extends XmlAdapter<Boolean, String> {
@Override
public String unmarshal(Boolean v) throws Exception {
return v.toString();
}
@Override
public Boolean marshal(String v) throws Exception {
return Boolean.valueOf(v);
}
}
oxm.xml
我们可以利用MOXy的外部映射文档来扩充通过注释提供的元数据,以挂钩我们的 XmlAadapter
(参见 http://blog.bdoughan.com/2010/12 /extending-jaxb-representing-annotations.html 。
We can leverage MOXy's external mapping document to augment the metadata supplied via annotations to hook in our XmlAadapter
(see http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html).
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11451880">
<java-types>
<java-type name="LifeSpan">
<java-attributes>
<xml-element java-attribute="end">
<xml-java-type-adapter value="forum11451880.BooleanStringAdapter"/>
</xml-element>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
LifeSpan
以下是您的域模型,其结束
属性类型为字符串
。
Below is your domain model with an end
property of type String
.
package forum11451880;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LifeSpan {
protected String begin;
protected String end;
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String ended;
}
jaxb.properties
要将MOXy指定为JAXB提供程序,您需要包含一个名为 jaxb.properties
的文件,其中包含以下条目(请参阅< a href =http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html =nofollow> http://blog.bdoughan.com/2011/05/指定-eclipselink-moxy-as-your.html ):
To specify MOXy as your JAXB provider you need to include a file called jaxb.properties
with the followin entry (see http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
下面的演示代码演示了如何利用外部映射文档。
The demo code below demonstrates how to leverage the external mapping document.
package forum11451880;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.MarshallerProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11451880/oxm.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {LifeSpan.class}, properties);
LifeSpan lifeSpan = new LifeSpan();
lifeSpan.begin = "1999-04";
lifeSpan.end = "true";
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(lifeSpan, System.out);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.marshal(lifeSpan, System.out);
}
}
输出
以下是运行示例的输出。如您所见, true
值不带引号:
Below is the output from running the example. As you can see the true
value appears without quotes:
<?xml version="1.0" encoding="UTF-8"?>
<lifeSpan>
<begin>1999-04</begin>
<end>true</end>
</lifeSpan>
{
"lifeSpan" : {
"begin" : "1999-04",
"end" : true
}
}
这篇关于生成json时,我可以让MOXy将字符串转换为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!