gpt4 book ai didi

java - JacksonJsonProvider 中的 GSON disableHtmlEscaping 等价物是什么

转载 作者:行者123 更新时间:2023-11-30 08:11:38 26 4
gpt4 key购买 nike

我正在尝试转换 URL,例如 https://api.test.com/cusomter?customer_id=1&customer_type=A

但是在序列化过程中它被转换成 https://api.test.com/customer?customer_id\u003d1\u0026客户类型\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);
}

关于java - JacksonJsonProvider 中的 GSON disableHtmlEscaping 等价物是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com