Я использую методы gson.tojson и gson.fromgson. У меня есть класс, как ниже. < /P>
@XmlType(name = "CBPR_CancellationReasonCode")
@XmlEnum
public enum CBPRCancellationReasonCode {
DUPL("DUPL"),
CUTA("CUTA"),
UPAY("UPAY"),
CUST("CUST"),
CURR("CURR"),
AGNT("AGNT"),
TECH("TECH"),
FRAD("FRAD"),
COVR("COVR"),
@XmlEnumValue("AM09")
AM_09("AM09"),
NARR("NARR");
private final String value;
CBPRCancellationReasonCode(String v) {
value = v;
}
public String value() {
return value;
}
public static CBPRCancellationReasonCode fromValue(String v) {
for (CBPRCancellationReasonCode c: CBPRCancellationReasonCode.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
< /code>
У меня есть проблема преобразования значений из XML в JSON или JSON в XML при использовании < /p>
@XmlEnumValue("AM09")
AM_09("AM09")
< /code>
Я ожидаю увидеть значение «AM09», но это требует AM_09. < /p>
Как я могу решить эту проблему? Gson gson = new GsonBuilder().setFieldNamingStrategy(new XsdAnnotation()).setPrettyPrinting()
.registerTypeHierarchyAdapter(XMLGregorianCalendar.class,
new XMLGregorianCalendarConverter.Serializer())
.registerTypeHierarchyAdapter(XMLGregorianCalendar.class,
new XMLGregorianCalendarConverter.Deserializer())
.create();
gson.toJson(testObject)
< /code>
public class XsdAnnotation implements FieldNamingStrategy {
@Override
public String translateName(Field field) {
XmlElement fieldNamingPolicyElement = field.getAnnotation(XmlElement.class);
XmlAttribute fieldNamingPolicyAttribute = field.getAnnotation(XmlAttribute.class);
return fieldNamingPolicyElement != null
? fieldNamingPolicyElement.name()
: (fieldNamingPolicyAttribute != null ? fieldNamingPolicyAttribute.name() : field.getName());
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... onversions