如何设置JAXB的浮点格式

如何设置JAXB的浮点格式

本文介绍了如何设置JAXB的浮点格式(使用Eclipse Moxy的json)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的pojo课:

I have a simple pojo class:

@XmlRootElement
public static class MyClass {
  private double f; //[0-1]

  @XmlAttribute
  //@XmlJavaTypeAdapter(FixedFloatingPointXmlAdapter.class) <-- has issues
  public double getF() {
    return f;
  }
}

字段f保持[0,1]范围内的值,有时可能很小.我想避免看到像1.234E-7(科学记法)之类的东西,而是想设置用于打印字段f的格式.

field f holds values in the range [0,1], which can sometimes be small. I'd like to avoid seeing things like 1.234E-7 (scientific notation) and would like to set the format used to print field f.

我看过其他建议使用XmlAdapter的答案,例如:

I've seen other answers that suggest using an XmlAdapter for this, like so:

public class FixedFloatingPointXmlAdapter extends XmlAdapter<String, Double> {
  private static final DecimalFormat FORMAT = new DecimalFormat("0.00000000");

  @Override
  public Double unmarshal(String v) throws Exception {
    return v == null ? null : Double.parseDouble(v);
  }

  @Override
  public String marshal(Double v) throws Exception {
    return v == null ? null : FORMAT.format(v);
  }
}

但是问题是该值将以json STRING 的形式打印出来(因此,"0.25000000" 而不是 0.25000000 )-因为XmlAdapter返回一个字符串(不是因为FORMAT放置了引号.它没有)

but the problem with this is that the value would be printed out as a json STRING (so "0.25000000" instead of 0.25000000) - because the XmlAdapter returns a String (not because FORMAT places quotes. it doesnt)

是否有任何JAXB/moxy注释可以使我控制格式而无需将字段转换为json字符串?

is there any JAXB/moxy annotation that would allow me to control the formatting without turning the field into a json string?

推荐答案

此解决方案正在使用 EclipseLink MOXy 的"nofollow noreferrer>转换注释.

This solution is using transformation annotations of EclipseLink MOXy.

import org.eclipse.persistence.oxm.annotations.XmlTransformation;
import org.eclipse.persistence.oxm.annotations.XmlWriteTransformer;

@XmlRootElement
public static class MyClass {
  @XmlAttribute
  @XmlReadTransformer(transformerClass=FixedFloatingTransformer.class)
  @XmlWriteTransformer(xmlPath="@f", transformerClass=FixedFloatingTransformer.class)
  private double f; //[0-1]

  public double getF() {
    return f;
  }
}

这是 transformerClass 的实现.

import java.text.DecimalFormat;

import org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping;
import org.eclipse.persistence.mappings.transformers.AttributeTransformer;
import org.eclipse.persistence.mappings.transformers.FieldTransformer;
import org.eclipse.persistence.sessions.Record;
import org.eclipse.persistence.sessions.Session;

public class FixedFloatingTransformer implements FieldTransformer, AttributeTransformer {
  private AbstractTransformationMapping mapping;
  private static DecimalFormat FORMAT = new DecimalFormat("0.00000000");

  public Object buildAttributeValue(Record r, Object obj, Session arg2) {
    return r.get(mapping.getFields().get(0));
  }

  public Object buildFieldValue(Object instance, String xpath, Session session) {
    Object o2 = mapping.getAttributeValueFromObject(instance);
    if (o2 instanceof Number) {
      return ((DecimalFormat)FORMAT.clone()).format((Number)o2);
    }
    return null;
  }

  public void initialize(AbstractTransformationMapping mapping) {
    this.mapping = mapping;
  }
}

2016年12月13日更新

moxy 将对象解组为JSON字符串时,将引用 @XmlSchemaType .如果 name boolean number type ,则 moxy 将输出不带引号char的值字符串.

Update in 2016/12/13

The @XmlSchemaType are referenced when moxy unmarshal the object to JSON string.If the name is boolean or number type, moxy will outputed the value string without quoted char.

public static class MyClass {
  private String f;

  @XmlAttribute
  @XmlSchemaType(name="double")
  public String getF() {
    return f;
  }
}
public static class MyClass {
  private double f;

  @XmlTransient //preventing the mapping
  public double getF() {
    return f;
  }

  @XmlAttribute(name="f")
  @XmlSchemaType(name="double")
  public String getFAsString() {
    return new DecimalFormat("0.00000000").format(f);
  }
}

这篇关于如何设置JAXB的浮点格式(使用Eclipse Moxy的json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 19:54