JacksonJsonProvider 中的 GSON disableHtmlEscaping 等价物是什么
What is the GSON disableHtmlEscaping equivalent in JacksonJsonProvider
我正在尝试转换 URL 例如
https://api.test.com/cusomter?customer_id=1&customer_type=A
但是在序列化过程中它被转换成
https://api.test.com/customer?customer_id\u003d1\u0026customer_type\u003dA
我知道在 GSON 中有 disableHtmlEscaping 选项可以转义 html = 和 & 字符的安全转换。
能否告知 JacksonJsonProvider 中的等效选项。
我在 here
上找到了这个示例代码
import org.codehaus.jackson.SerializableString;
import org.codehaus.jackson.io.CharacterEscapes;
// First, definition of what to escape
public class HTMLCharacterEscapes extends CharacterEscapes
{
private final int[] asciiEscapes;
public HTMLCharacterEscapes()
{
// start with set of characters known to require escaping (double-quote, backslash etc)
int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
// and force escaping of a few others:
esc['<'] = CharacterEscapes.ESCAPE_STANDARD;
esc['>'] = CharacterEscapes.ESCAPE_STANDARD;
esc['&'] = CharacterEscapes.ESCAPE_STANDARD;
esc['\''] = CharacterEscapes.ESCAPE_STANDARD;
asciiEscapes = esc;
}
// this method gets called for character codes 0 - 127
@Override public int[] getEscapeCodesForAscii() {
return asciiEscapes;
}
// and this for others; we don't need anything special here
@Override public SerializableString getEscapeSequence(int ch) {
// no further escaping (beyond ASCII chars) needed:
return null;
}
}
// and then an example of how to apply it
public ObjectMapper getEscapingMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.getJsonFactory().setCharacterEscapes(new HTMLCharacterEscapes());
return mapper;
}
// so we could do:
public byte[] serializeWithEscapes(Object ob) throws IOException
{
return getEscapingMapper().writeValueAsBytes(ob);
}
我正在尝试转换 URL 例如 https://api.test.com/cusomter?customer_id=1&customer_type=A
但是在序列化过程中它被转换成 https://api.test.com/customer?customer_id\u003d1\u0026customer_type\u003dA
我知道在 GSON 中有 disableHtmlEscaping 选项可以转义 html = 和 & 字符的安全转换。
能否告知 JacksonJsonProvider 中的等效选项。
我在 here
上找到了这个示例代码import org.codehaus.jackson.SerializableString;
import org.codehaus.jackson.io.CharacterEscapes;
// First, definition of what to escape
public class HTMLCharacterEscapes extends CharacterEscapes
{
private final int[] asciiEscapes;
public HTMLCharacterEscapes()
{
// start with set of characters known to require escaping (double-quote, backslash etc)
int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
// and force escaping of a few others:
esc['<'] = CharacterEscapes.ESCAPE_STANDARD;
esc['>'] = CharacterEscapes.ESCAPE_STANDARD;
esc['&'] = CharacterEscapes.ESCAPE_STANDARD;
esc['\''] = CharacterEscapes.ESCAPE_STANDARD;
asciiEscapes = esc;
}
// this method gets called for character codes 0 - 127
@Override public int[] getEscapeCodesForAscii() {
return asciiEscapes;
}
// and this for others; we don't need anything special here
@Override public SerializableString getEscapeSequence(int ch) {
// no further escaping (beyond ASCII chars) needed:
return null;
}
}
// and then an example of how to apply it
public ObjectMapper getEscapingMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.getJsonFactory().setCharacterEscapes(new HTMLCharacterEscapes());
return mapper;
}
// so we could do:
public byte[] serializeWithEscapes(Object ob) throws IOException
{
return getEscapingMapper().writeValueAsBytes(ob);
}