JAXB:如何自定义双字段的Xml序列化 [英] JAXB: How to customize Xml serialization of double fields

查看:158
本文介绍了JAXB:如何自定义双字段的Xml序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个遗留类,有很多公共双字段。所有双字段都使用 Double.MAX_VALUE 进行初始化,以指示它们为空。 (遗留序列化编码为忽略该字段,如果字段等于 Double.MAX_VALUE ,则不进行序列化。)

I have a legacy class, with a lot of public double fields. All double fields are initialized with Double.MAX_VALUE to indicate that they are empty. (The legacy serialization is coded to ignore the field and not serialize if field is equals to Double.MAX_VALUE).

我们现在尝试使用JAXB Marshaller将此类序列化为Xml。它工作正常,除了我们想要阻止为等于 Double.MAX_VALUE 的字段生成Xml。

We are now trying to serialize this class to Xml using JAXB Marshaller. It is working fine, except that we want to prevent generating Xml for fields which equal Double.MAX_VALUE.

我们没有使用单独的JAXB模式,只是用各种 javax.xml.bind.annotation标记我们的类注释。如果使用架构,则可以添加< javaType> 元素以指定自定义DataType转换器。有没有办法使用Annotations或以编程方式执行此操作?

We aren't using a separate JAXB schema, just marking up our classes with various javax.xml.bind.annotation Annotations. If a schema is used, you can add a <javaType> element to specify a custom DataType converter. Is there any way to do this using Annotations or programmatically?

尝试下面推荐的方法后,我仍然无法获得 XmlAdapter 拾取:

After trying approach recommended below, I still can't get XmlAdapter picked up:

@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=EmptyDoubleValueHandler.class, type=Double.class), @XmlJavaTypeAdapter(value=EmptyDoubleValueHandler.class, type=double.class)})
package tta.penstock.data.iserver;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

我的顶级课程是:tta.penstock.data.iserver.OrderBlotter,其中包含一个列表tta.penstock.data.iserver.OrderResponseWrappers扩展 com.eztech.OrderResponse 。所有双字段都包含在 com.eztech.OrderResponse 中。

My top level class is: tta.penstock.data.iserver.OrderBlotter, which contains a list of tta.penstock.data.iserver.OrderResponseWrappers which extends com.eztech.OrderResponse. All the double fields are contained in com.eztech.OrderResponse.

我的单元测试代码执行以下操作:

My unit test code does the following:

JAXBContext context = JAXBContext.newInstance(new Class[] { OrderBlotter.class, OrderResponseWrapper.class, OrderResponse.class});

Marshaller marshaller = context.createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(blotter, stringWriter);
System.out.println("result xml=\n" + stringWriter.toString());

XmlAdapter仍无法处理双值。我知道我遗漏了一些基本的东西,但我不确定它是什么。

But the double values still don't get handled by the XmlAdapter. I know I'm missing something basic, but I'm not sure what it is.

推荐答案

你可以使用XmlAdapter:

You could use an XmlAdapter:

  • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html

XmlAdapter

package example;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DoubleAdapter extends XmlAdapter<Double, Double>{

    @Override
    public Double unmarshal(Double v) throws Exception {
        return v;
    }

    @Override
    public Double marshal(Double v) throws Exception {
       if(Double.MAX_VALUE == v) {
           return null;
       } else {
           return v;
       }
    }

}

模型对象

package example;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Root {

    @XmlJavaTypeAdapter(DoubleAdapter.class)
    public Double maxDouble = Double.MAX_VALUE;

    @XmlJavaTypeAdapter(DoubleAdapter.class)
    public Double aDouble = 123d;

}

演示代码

package example;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(new Root(), System.out);
    }

}

UPDATE

StaxMan的建议很好。如果指定以下包级别注释,则可以避免单独注释所有Double属性

StaxMan's suggestion is a good one. If you specify the following package level annotation you can avoid the need of individually annotating all the Double properties

package-info.java

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(type=Double.class, value=DoubleAdapter.class)
})
package example;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

这篇关于JAXB:如何自定义双字段的Xml序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆