简体   繁体   English

替换 & 使用 & 使用 Jackson ObjectMapper

[英]Replace & with & using Jackson ObjectMapper

In the application that I'm working we have a requirement to transform a huge json into an even bigger XML.在我正在工作的应用程序中,我们需要将巨大的 json 转换为更大的 XML。 The structure of both elements is very different, therefore we have decided to create an XML file matching the XSD and populate the fields using unified expression language.两个元素的结构非常不同,因此我们决定创建一个与 XSD 匹配的 XML 文件,并使用统一表达式语言填充字段。 For example:例如:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Objects xmlns="..">
  <Object>
    <Field>${json.field}</Field>
  <Object>
<Objects>

The replacement of ${json.field} is done using JUEL ${json.field}的替换是使用JUEL完成的

After running the JUEL process we unmarshall the xml string into the object and we continue with the process.在运行 JUEL 过程后,我们将 xml 字符串解组到对象中,然后继续该过程。 The unmarshal code is something like this:解组代码是这样的:

  private XmlObjects unmarshal(StringReader xmlString) {
    try {
      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
      return (XmlObjects) unmarshaller.unmarshal(xmlString);
    } catch (JAXBException e) {
      throw new RuntimeException(e);
    }
  }

The problem that we are facing is that the json.field can contain characters that are not allowed in the XML, for example an & or <, >.我们面临的问题是json.field可以包含 XML 中不允许的字符,例如 & 或 <、>。

An easy fix is to replace all the & by & en the above method, but that is not going to solve the problem of < or > and I cannot replace that in that point.一个简单的解决方法是将所有 & 替换为 & en 上面的方法,但这并不能解决 < 或 > 的问题,我无法在这一点上替换它。

What I would like to do is to use Jackson to make the replacement when the json is mapped into the POJO, but I cannot find a way to do it.我想做的是在将json映射到POJO时使用Jackson进行替换,但我找不到办法做到这一点。 So far I've tried creating a custom CharacterEscapes class and setting that to the ObjectMapper but didn't work.到目前为止,我已经尝试创建一个自定义CharacterEscapes类并将其设置为ObjectMapper但没有奏效。

So, this is the test that summarizes everything:所以,这是总结一切的测试:

  @Test
  public void test() throws IOException {
    ObjectMapper objectMapper = Jackson.newObjectMapper();
    objectMapper.getFactory().setCharacterEscapes(new XMLCharacterEscapes());

    String json = "{\"variable\":\"a string with &\"}";
    FooJson fooJson = objectMapper.readValue(json, FooJson.class);
    assertEquals("a string with &amp;", fooJson.getVariable());
  }

This is the XMLCharacterEscapes class:这是XMLCharacterEscapes类:

public class XMLCharacterEscapes extends CharacterEscapes {

  private final int[] asciiEscapes;

  public XMLCharacterEscapes() {
    int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
    esc['&'] = CharacterEscapes.ESCAPE_CUSTOM;
    asciiEscapes = esc;
  }

  @Override
  public int[] getEscapeCodesForAscii() {
    return asciiEscapes;
  }

  @Override
  public SerializableString getEscapeSequence(int i) {
    return new SerializedString("&amp;");
  }
}

If I may suggest an alternative, could you instead have your unmarshaller apply JUEL to the element values as it reads the XML?如果我可以提出替代方案,您是否可以让解组器在读取 XML 时将 JUEL 应用于元素值? Or, could you walk the XML object graph after unmarshalling and apply JUEL to the element values?或者,您能否在解组后遍历 XML 对象图并将 JUEL 应用于元素值?

Edit: It seems like the problem is just the order you apply replacements.编辑:问题似乎只是您应用替换的顺序。 Once you have an XML document, you should be able to set any values you like, and it will take care of proper escaping.一旦你有了一个 XML 文档,你应该能够设置你喜欢的任何值,它会处理正确的转义。

将您的字段括在 CDATA 部分中

<Field><![CDATA[${json.field}]]></Field>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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