问题描述
我使用Jackson库生成这样的json字符串:
I use Jackson library to generate json string like this:
ObjectMapper objectMapper = new ObjectMapper();
String str = objectMapper.writeValueAsString(model);
这个剪切代码例如生成这样的东西:
and this snipped code for instance generate somtething like this:
{"x" : "This is x", "y" : "This is y"}
但我希望生成如下内容:
but I want to generate something like this:
{'x' : 'This is x', 'y' : 'This is y'}
我的意思是怎样我可以用单引号字符串更改双引号字符串。我尝试更改这样的代码:
I mean how can I change the double quote string with single quote string.I try to change code like this:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
String str = objectMapper.writeValueAsString(model);
但这个剪切代码生成第一个。
当然我可以用替换方法处理这个问题,但我希望Jackson库能为我做这个。
我该如何处理这个问题?
but this snipped code generate the first one.and of course I can handle this problem with replace method but I want Jackson library do this for me. How can I handle this problem?
推荐答案
objectMapper.configure(JsonParser.Feature。 ALLOW_SINGLE_QUOTES,true);
是关于在反序列化中允许单引号(JSON到Objects),而不是根据需要序列化(Objects to JSON)。
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
is about allowing single quotes in deserializing (JSON to Objects), not serializing (Objects to JSON) as you want.
在序列化中,问题似乎与Jackson 1.X的默认序列化程序有关。以下是Jackson用于编写 String
值的代码。正如您所看到的,双引号是硬编码的,因此无法通过配置进行更改:
In serializing, the issue seems to be with Jackson 1.X's default serializer. Below is the code that Jackson uses to write the String
values. As you can see, the double quotes are hard coded and thus unchangeable through configuration:
@Override
public void writeString(String text)
throws IOException, JsonGenerationException
{
_verifyValueWrite("write text value");
if (text == null) {
_writeNull();
return;
}
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = '"'; // <----------------- opening quote
_writeString(text); // <----------------- string actual value
// And finally, closing quotes
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = '"'; // <----------------- closing quote
}
要实现您的目标,至少有两种选择:
To achieve what you want, there are at least two options:
这是一种安全的方法,因为Jackson提供了双引号()已经转义(
\
)。您所要做的就是转义单引号并在属性名称和值周围切换:
This is a safe approach because Jackson gives the double quotes ("
) already escaped (\"
). All you have to do is escape the single quotes and switch the "
around properties names and values:
ObjectMapper objectMapper = new ObjectMapper();
String str = objectMapper.writeValueAsString(model);
System.out.println("Received.: "+str);
str = str.replaceAll("'", "\\\\'"); // escapes all ' (turns all ' into \')
str = str.replaceAll("(?<!\\\\)\"", "'"); // turns all "bla" into 'bla'
System.out.println("Converted: "+str);
输出:
Received.: {"x":"ab\"c","y":"x\"y'z","z":15,"b":true}
Converted: {'x':'ab\"c','y':'x\"y\'z','z':15,'b':true}
声明自定义序列化程序:
Declare the custom serializer:
public class SingleQuoteStringSerializer extends JsonSerializer<String> {
@Override
public void serialize(String str, JsonGenerator jGen, SerializerProvider sP)
throws IOException, JsonProcessingException {
str = str.replace("'", "\\'"); // turns all ' into \'
jGen.writeRawValue("'" + str + "'"); // write surrounded by single quote
}
}
在你想单引号的字段:
public class MyModel {
@JsonSerialize(using = SingleQuoteStringSerializer.class)
private String x;
...
继续照常( QUOTE_FIELD_NAMES == false
用于取消引用字段名称):
And proceed as usual (QUOTE_FIELD_NAMES == false
is used to unquote the field names):
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
String str = objectMapper.writeValueAsString(model);
System.out.println("Received.: "+str);
输出:
Received.: {x:'ab"c',y:'x"y\'z',z:15,b:true}
这篇关于创建Json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!