英文:
Can gson deserialize empty strings as nulls inside a map?
问题
以下是翻译好的部分:
{
"something": 1234,
"message": "abcde",
"Results": [
{
"key2": "some value here"
}
]
}
public static class EmptyStringAsNullTypeAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
if(StringUtils.isBlank(src.toString())) return null;
return new JsonPrimitive(src.toString());
}
@Override
public T deserialize(final JsonElement jsonElement, Type type, final JsonDeserializationContext context) throws JsonParseException {
if ( jsonElement.isJsonPrimitive() ) {
final JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();
if ( jsonPrimitive.isString() && jsonPrimitive.getAsString().isEmpty() ) {
return null;
}
}
return context.deserialize(jsonElement, type);
}
}
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(JsonElement.class, new EmptyStringAsNullTypeAdapter());
Gson gson = gsonBuilder.create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(jsonString);
String prettyJson = gson.toJson(je);
希望这些内容能对您有所帮助!如果有任何疑问,请随时问我。
英文:
I've got a remote API responding with json structured like this:
{
"something": 1234,
"message": "abcde",
"someKey": null,
"Results": [
{
"key1": "",
"key2": "some value here",
"key3": ""
}
]
}
The problem is that there are hundreds of these empty string values being returned making it hard to sort through the results. I've tried registering and implementing my own JsonDeserializer like this:
public static class EmptyStringAsNullTypeAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
if(StringUtils.isBlank(src.toString())) return null;
return new JsonPrimitive(src.toString());
}
@Override
public T deserialize(final JsonElement jsonElement, Type type, final JsonDeserializationContext context) throws JsonParseException {
if ( jsonElement.isJsonPrimitive() ) {
final JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();
if ( jsonPrimitive.isString() && jsonPrimitive.getAsString().isEmpty() ) {
return null;
}
}
return context.deserialize(jsonElement, type);
}
}
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(JsonElement.class, new EmptyStringAsNullTypeAdapter());
// gsonBuilder.registerTypeAdapter(String.class, new EmptyStringAsNullTypeAdapter()); //tried this first
Gson gson = gsonBuilder.create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(jsonString);
String prettyJson = gson.toJson(je);
Doesn't seem to matter what I do here, the custom Deserializer never gets called and my "prettyJson" always contains the empty strings I'm trying to avoid. I've also tried adding a custom serializer but it also never gets called. Can gson do what I'd like it to do here and if so, how?
Here is what I want:
{
"something": 1234,
"message": "abcde",
"Results": [
{
"key2": "some value here",
}
]
}
答案1
得分: 0
你需要在这里实现JsonSerializer
,在这里你需要将对象序列化为字符串。
英文:
You need to implement JsonSerializer
here you are again serializing the object in string.
专注分享java语言的经验与见解,让所有开发者获益!
评论