简体   繁体   中英

Prevent JAXB from converting String to Number (JSON)

I am using JAXB annotations for creating my JSON object. In addition I am using Spring 3.2.4. The issue that I am encountering is that I have an attribute "String anId;" that is specified as an @XmlElement. If this attribute contains a value similar to "123.0", the resulting JSON that is returned to me is:

    {"anId":123}

as opposed to what I want of

    {"anId":"123.0"}

The .0 is meaningful and I can't have it rounded/truncated off. I've attempted to resolve this by doing the following:

    @XmlJavaTypeAdapter annotation with an XmlAdapter of <String,String>
    adding @XmlSchemaType(name="string")
    @XmlElement(name="anId",type=String.class)

However, none of these have resolved the problem. NOTE: the reason the ID is specified as a String is because "1234" is valid, "1234.0" is valid, and "abc123" is also a potential valid value for the ID.

With this in mind, does anyone have any idea how I can resolve my issue? Or at the very least can explain why it keeps rounding the number?

EDIT: In addition, if I update the value to be 8 digits long (12345678.0) it returns as a string "12345678.0". In addition, if I update the value to end in .00 it returns the full number as a string "123.00". I don't know if this helps any but it makes the problem more baffling to me as the rounding is not consistent.

EDIT 2: It appears the issue was due to me using Jettison as the JSON converter. Switching to Jackson resolved the issue.

The JAXB (JSR-222) specification itself does not cover JSON-binding. There are:

  1. Implementations that offer it as an extension (ie EclipseLink MOXy, example: http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html )
  2. JSON-binding libraries that support JAXB annotations (ie Jackson, Genson, etc)
  3. Libraries (ie Jettinson & StAXON) that turn JSON to/from StAX events so that XML-binding libraries can be used to produce consume JSON (example: http://bdoughan.blogspot.com/2011/04/jaxb-and-json-via-jettison.html ).

I'm assuming you fall into the 3rd scenario. The behaviour you are seeing is because these libraries just receive the characters event. That means they just get the String 123.0 and need to choose how to render it in JSON. Since they have no other option they render it using what they consider the best JSON format they can.

The ultimate solution requires confirming which of the above scenarios reflects your situation. The first 2 options will be able to solve your use case.

Have you tried using @XmlAttribute instead of @XmlElement?

Also, have you annotated the class with the type of accessor, ie @XmlAccessorType(XmlAccessType.FIELD)?

I tend to find using the field type with the xmlattribute I have no issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM