你如何让GSON省略空或空对象并清空数组和列表?

时间:2021-08-24 04:39:54

I am using Gson and I am in a situation in which I have to shrink the size of certain Json strings. I would like to do so by getting it to not put null objects, only empty values, and empty lists and arrays into the Json string.

我正在使用Gson,我处于一种必须缩小某些Json字符串大小的情况。我想通过让它不将空对象,空值和空列表和数组放入Json字符串来实现。

Is there a straightforward way to do that?

有一种直截了当的方法吗?

Let me clarify a bit: I want everything that says: emptyProp:{} or emptyArray:[] to be skipped. I want any object that only contains properties that are empty to be skipped.

让我澄清一点:我希望所有内容:emptyProp:{}或emptyArray:[]被跳过。我希望任何只包含空属性的对象被跳过。

2 个解决方案

#1


24  

Null values are excluded by default as long as you don't set serializeNulls() to your GsonBuilder.

默认情况下,只要不将serializeNulls()设置为GsonBuilder,就会排除空值。

A way for empty lists is to create a JsonSerializer

空列表的一种方法是创建一个JsonSerializer

class CollectionAdapter implements JsonSerializer<Collection<?>> {
  @Override
  public JsonElement serialize(Collection<?> src, Type typeOfSrc, JsonSerializationContext context) {
    if (src == null || src.isEmpty()) // exclusion is made here
      return null;

    JsonArray array = new JsonArray();

    for (Object child : src) {
      JsonElement element = context.serialize(child);
      array.add(element);
    }

    return array;
  }
}

Then register it

然后注册

Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Collection.class, new CollectionAdapter()).create();

#2


0  

According to PomPom a HashMap can serialized via:

根据PomPom,HashMap可以通过以下方式序列化:

class MapAdapter implements JsonSerializer<Map<?, ?>> {
        @Override
        public JsonElement serialize(Map<?, ?> src, Type typeOfSrc,JsonSerializationContext context) {
            if (src == null || src.isEmpty())
                return null;
            JsonObject obj = new JsonObject();
            for (Map.Entry<?, ?> entry : src.entrySet()) {
                obj.add(entry.getKey().toString(), context.serialize(entry.getValue()));
            }
        return obj;
        }
    }

#1


24  

Null values are excluded by default as long as you don't set serializeNulls() to your GsonBuilder.

默认情况下,只要不将serializeNulls()设置为GsonBuilder,就会排除空值。

A way for empty lists is to create a JsonSerializer

空列表的一种方法是创建一个JsonSerializer

class CollectionAdapter implements JsonSerializer<Collection<?>> {
  @Override
  public JsonElement serialize(Collection<?> src, Type typeOfSrc, JsonSerializationContext context) {
    if (src == null || src.isEmpty()) // exclusion is made here
      return null;

    JsonArray array = new JsonArray();

    for (Object child : src) {
      JsonElement element = context.serialize(child);
      array.add(element);
    }

    return array;
  }
}

Then register it

然后注册

Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Collection.class, new CollectionAdapter()).create();

#2


0  

According to PomPom a HashMap can serialized via:

根据PomPom,HashMap可以通过以下方式序列化:

class MapAdapter implements JsonSerializer<Map<?, ?>> {
        @Override
        public JsonElement serialize(Map<?, ?> src, Type typeOfSrc,JsonSerializationContext context) {
            if (src == null || src.isEmpty())
                return null;
            JsonObject obj = new JsonObject();
            for (Map.Entry<?, ?> entry : src.entrySet()) {
                obj.add(entry.getKey().toString(), context.serialize(entry.getValue()));
            }
        return obj;
        }
    }