[英]How to escape slashes in Gson
According to the json specs, escaping " /
" is optional. 根据json规范,转义“
/
”是可选的。
Gson does not do it by default, but I am dealing with a webservice which is expecting escaped " /
". Gson默认情况下不执行此操作,但是我正在处理期望转义为“
/
”的Web服务。 So what I want to send is " somestring\\\\/someotherstring
". 所以我要发送的是“
somestring\\\\/someotherstring
”。 Any ideas on how to achieve this? 关于如何实现这一目标的任何想法?
To make things clearer: if I try to deserialize " \\\\/
" with Gson, it will send " \\\\\\\\/
", which is not what I want! 为了使事情更清楚:如果我尝试使用Gson反序列化“
\\\\/
”,它将发送“ \\\\\\\\/
”,这不是我想要的!
You can write your own Custom Serializer - I have created one which follows the rule that you want /
to be \\\\/
but if the string is already escaped you want it to stay \\\\/
and not be \\\\\\\\/
. 您可以编写自己的自定义序列化程序-我创建了一个遵循您希望
/
设为\\\\/
的规则,但是如果字符串已经转义,则希望它保留\\\\/
而不是\\\\\\\\/
。
package com.dominikangerer.q29396608;
import java.lang.reflect.Type;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class EscapeStringSerializer implements JsonSerializer<String> {
@Override
public JsonElement serialize(String src, Type typeOfSrc,
JsonSerializationContext context) {
src = createEscapedString(src);
return new JsonPrimitive(src);
}
private String createEscapedString(String src) {
// StringBuilder for the new String
StringBuilder builder = new StringBuilder();
// First occurrence
int index = src.indexOf('/');
// lastAdded starting at position 0
int lastAdded = 0;
while (index >= 0) {
// append first part without a /
builder.append(src.substring(lastAdded, index));
// if / doesn't have a \ directly in front - add a \
if (index - 1 >= 0 && !src.substring(index - 1, index).equals("\\")) {
builder.append("\\");
// if we are at index 0 we also add it because - well it's the
// first character
} else if (index == 0) {
builder.append("\\");
}
// change last added to index
lastAdded = index;
// change index to the new occurrence of the /
index = src.indexOf('/', index + 1);
}
// add the rest of the string
builder.append(src.substring(lastAdded, src.length()));
// return the new String
return builder.toString();
}
}
This will create from the following String: 这将从以下字符串创建:
"12 /first /second \\/third\\/fourth\\//fifth"`
the output: 输出:
"12 \\/first \\/second \\/third\\/fourth\\/\\/fifth"
Register your Custom Serializer 注册您的自定义序列化器
Than of course you need to pass this serializer to Gson on Setup like this: 当然,您需要像下面这样在安装程序中将此串行器传递给Gson:
Gson gson = new GsonBuilder().registerTypeAdapter(String.class, new EscapeStringSerializer()).create();
String json = gson.toJson(yourObject);
You can find this answer and the exact example in my github stackoverflow answers repo: 您可以在我的github stackoverflow答案回购中找到此答案和确切的示例:
Gson CustomSerializer to escape a String in a special way by DominikAngerer Gson CustomSerializer通过DominikAngerer以特殊方式转义字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.