Gson能将空字符串反序列化为映射内的null吗?

huangapple 未分类评论43阅读模式
英文:

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:

{ 
  &quot;something&quot;: 1234,
  &quot;message&quot;: &quot;abcde&quot;,
  &quot;someKey&quot;: null,
  &quot;Results&quot;: [
    {
     &quot;key1&quot;: &quot;&quot;,
     &quot;key2&quot;: &quot;some value here&quot;,
     &quot;key3&quot;: &quot;&quot;
    }
  ]
}

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&lt;T&gt; implements JsonSerializer&lt;T&gt;, JsonDeserializer&lt;T&gt; {
        @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() &amp;&amp; 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:

{ 
  &quot;something&quot;: 1234,
  &quot;message&quot;: &quot;abcde&quot;,
  &quot;Results&quot;: [
    {
     &quot;key2&quot;: &quot;some value here&quot;,
    }
  ]
}

答案1

得分: 0

你需要在这里实现JsonSerializer,在这里你需要将对象序列化为字符串。

英文:

You need to implement JsonSerializer here you are again serializing the object in string.

huangapple
  • 本文由 发表于 2020年4月8日 15:26:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/61095370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定