我正在使用Eclipse MOXy 2.6,在JSON编组和解组时我遇到了一个非常棘手的问题。我必须分别生成和使用以下JSON对象:
{
"ssh::server::storeconfigs_enabled": true
}
不幸的是,密钥包含与XML命名空间分隔符碰撞的冒号。
我使用以下带注释的域类SshOptions
:
@XmlRootElement
public class SshOptions {
@XmlElement(name = "ssh::server::storeconfigs_enabled")
private boolean storeConfigs;
public boolean isStoreConfigs() {
return storeConfigs;
}
public void setStoreConfigs(boolean storeConfigs) {
this.storeConfigs = storeConfigs;
}
}
我使用了以下测试代码:
public class Test {
public static void main(String[] args) {
SshOptions options = new SshOptions();
options.setStoreConfigs(true);
try {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.JSON_NAMESPACE_SEPARATOR, '.');
properties.put(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext context = JAXBContextFactory.createContext(new Class[] { SshOptions.class }, properties);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter output = new StringWriter();
marshaller.marshal(options, output);
System.out.println(output.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
输出结果为:
{
"null" : true
}
有没有人知道如何使用Eclipse MOXy 2.6对JSON中的密钥进行编组和解组,这些密钥在其名称中有一个或多个冒号?