生成json时,我可以让MOXy将字符串转换为布尔值

时间:2021-09-25 21:44:52

The object model has an element ended of type String

对象模型具有以String类型结尾的元素

public class LifeSpan {

protected String begin;
protected String end;
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String ended;
....

but its actually only ever a boolean, (I dont know the significance of the XmlJavaTypeAdapter annotation)

但它实际上只是一个布尔值,(我不知道XmlJavaTypeAdapter注释的意义)

When output as XML gives

以XML格式输出时给出

<life-span><begin>1999-04</begin><ended>true</ended></life-span>

so doesn't really matter if defined as boolean or string

所以如果定义为boolean或string则无关紧要

but JSON output is

但是JSON输出是

"life-span" : {
         "begin" : "1999-04",
         "ended" : "true"
      },

when I need it to be

当我需要的时候

 "life-span" : {
         "begin" : "1999-04",
         "ended" : true
      },

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

我无法真正更改对象模型,以为我可以在oxml.xml文件中映射到正确的类型,并尝试了

<java-type name="LifeSpan">
            <java-attributes>
                <xml-element java-attribute="ended"  type="boolean"/>
            </java-attributes>
        </java-type>

but it didnt like that.

但它不是那样的。

1 个解决方案

#1


3  

Below is how you can support this use case with EclipseLink JAXB (MOXy):

以下是如何使用EclipseLink JAXB(MOXy)支持此用例:

BooleanStringAdapter

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.

XmlAdapter允许您将一种类型的对象编组为另一种类型(请参阅http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html)。在此示例中,我们希望将String值视为布尔值。

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

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).

我们可以利用MOXy的外部映射文档来扩充通过注释提供的元数据来挂钩我们的XmlAadapter(请参阅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.

下面是您的域模型,其end属性为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

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):

要将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

Demo

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);
    }

}

Output

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
   }
}

#1


3  

Below is how you can support this use case with EclipseLink JAXB (MOXy):

以下是如何使用EclipseLink JAXB(MOXy)支持此用例:

BooleanStringAdapter

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.

XmlAdapter允许您将一种类型的对象编组为另一种类型(请参阅http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html)。在此示例中,我们希望将String值视为布尔值。

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

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).

我们可以利用MOXy的外部映射文档来扩充通过注释提供的元数据来挂钩我们的XmlAadapter(请参阅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.

下面是您的域模型,其end属性为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

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):

要将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

Demo

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);
    }

}

Output

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
   }
}