杰克逊序列化没有正确处理单引号

时间:2013-02-23 15:07:17

标签: java javascript json spring jackson

我刚刚开始使用Jackson,因为它与Spring Framework集成并且遇到了一个值中单引号的问题。当尝试使用页面上的jQuery解析JSON时,我收到JavaScript错误"SyntaxError: missing ) after argument list"。我习惯使用Gson来序列化我的对象,并且不会遇到这个问题,因为Gson会用Unicode \ u0027替换单引号。

例如;
爪哇

public final class Person {
  private String firstName;
  private String lastName;

  public Person() {}

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getLastName() {
    return lastName;
  }
}


JSON
在杰克逊 [{"person":{"firstName":"James","lastName":"O'tool"}}]
在Gson [{"person":{"firstName":"James","lastName":"O\u0027tool"}}]

的JavaScript ;
// This is where the JavaScript fails with the Jackson serialized object
$.parseJSON('${requestScope.person}');

我找了一个解决方案,但我找不到一个。 有没有人知道是否有可能配置杰克逊以与Gson相同的方式处理单引号?

感谢您的时间。

3 个答案:

答案 0 :(得分:6)

另一个答案显示了一种方法,它应该可以很好地工作。

但还有另一种方法可以做到这一点,这是一点点工作,在“Forcing escaping of HTML characters in JSON using Jackson”解释

答案 1 :(得分:2)

要解决此问题,我最终为Jackson创建了一个自定义String序列化程序。我从Gson那里借了一些资料。

    public class HtmlStringSerializer extends StdSerializer<String> {
      protected HtmlStringSerializer() { super(String.class); }

      private static final String[] HTML_SAFE_REPLACEMENT_CHARS;
      static {
        HTML_SAFE_REPLACEMENT_CHARS = new String[128];
        for (int i = 0; i <= 0x1f; i++) {
          HTML_SAFE_REPLACEMENT_CHARS[i] = String.format("\\u%04x", i);
        }
        HTML_SAFE_REPLACEMENT_CHARS['"'] = "\\\"";
        HTML_SAFE_REPLACEMENT_CHARS['\\'] = "\\\\";
        HTML_SAFE_REPLACEMENT_CHARS['\t'] = "\\t";
        HTML_SAFE_REPLACEMENT_CHARS['\b'] = "\\b";
        HTML_SAFE_REPLACEMENT_CHARS['\n'] = "\\n";
        HTML_SAFE_REPLACEMENT_CHARS['\r'] = "\\r";
        HTML_SAFE_REPLACEMENT_CHARS['\f'] = "\\f";
        HTML_SAFE_REPLACEMENT_CHARS['<'] = "\\u003c";
        HTML_SAFE_REPLACEMENT_CHARS['>'] = "\\u003e";
        HTML_SAFE_REPLACEMENT_CHARS['&'] = "\\u0026";
        HTML_SAFE_REPLACEMENT_CHARS['='] = "\\u003d";
        HTML_SAFE_REPLACEMENT_CHARS['\''] = "\\u0027";
      }

      @Override
      public void serialize(String string, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
        int last = 0;
        int length = string.length();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
          char c = string.charAt(i);
          String replacement;
          if (c < 128) {
            replacement = HTML_SAFE_REPLACEMENT_CHARS[c];
            if (replacement == null) { continue; }
          } else if (c == '\u2028') {
            replacement = "\\u2028";
          } else if (c == '\u2029') {
            replacement = "\\u2029";
          } else {
            continue;
          }
          if (last < i) {
        sb.append(string.substring(last, i));
          }
          sb.append(replacement);
          last = i + 1;
        }
        if (last < length) {
          sb.append(string.substring(last));
        }
        gen.writeString(sb.toString());
      }
    }

答案 2 :(得分:0)

@Bergi试图说:不要使用:

$.parseJSON('${requestScope.person}')

只需使用

${requestScope.person}

直接!