如何在Gson生成的JSON文本中跳过空值,如果TypeAdapter转换了空值?

huangapple go评论74阅读模式
英文:

How to skip null values in json text produced by gson, if TypeAdapter converts null-value?

问题

我创建了一个用于Instant类型的TypeAdapter,通过写入毫秒数来实现。
如何处理instant字段为null的情况?
我不想写一个特殊的条目,比如-1L,然后在读取时将其转换为null(见下面的示例)。
我希望在生成的JSON文本中跳过这个条目,因为这是默认情况。

示例:

gsonBuilder.registerTypeAdapter(Instant.class, new TypeAdapter<Instant>(){
            @Override
            public void write(final JsonWriter jsonWriter, final Instant instant) throws IOException {
                if (instant != null) {
                    jsonWriter.value(instant.toEpochMilli());
                } else {
                    jsonWriter.value(-1L);
                }
            }

            @Override
            public Instant read(final JsonReader jsonReader) throws IOException {
                long millisEpoch = jsonReader.nextLong();
                if (millisEpoch == -1) {
                    return null;
                } else {
                    return Instant.ofEpochMilli(millisEpoch);
                }
            }
        });
英文:

I created a TypeAdapter for the Type Instant by writing milliseconds.
How can I handle the case of the instant-field to be null.
I do not want to write a kind of special entry like -1L, which I later could convert to null during reading (see example below).
I want to skip the entry in the produced json-text as it is the default case.

Example:

gsonBuilder.registerTypeAdapter(Instant.class, new TypeAdapter&lt;Instant&gt;(){
            @Override
            public void write(final JsonWriter jsonWriter, final Instant instant) throws IOException {
                if (instant != null) {
                    jsonWriter.value(instant.toEpochMilli());
                } else {
                    jsonWriter.value(-1L);
                }
            }

            @Override
            public Instant read(final JsonReader jsonReader) throws IOException {
                long millisEpoch = jsonReader.nextLong();
                if (millisEpoch == -1) {
                    return null;
                } else {
                    return Instant.ofEpochMilli(millisEpoch);
                }
            }
        });

答案1

得分: 2

这是翻译好的内容:

"很简单,我认为你可以修改你的 TypeAdapter 来检查即时值是否为 null,然后不将其写入 JSON 输出中。

让我用一个简单的例子来解释一下,首先在 write 方法中,我检查 Instant 值是否为 null,如果是,就写入一个空值,而不是时代毫秒,这将跳过 JSON 输出中的空值,在 read 方法中,我仍然检查特殊情况下的 -1L,将其转换为 null。

gsonBuilder.registerTypeAdapter(Instant.class, new TypeAdapter&lt;Instant&gt;() {
    @Override
    public void write(final JsonWriter jsonWriter, final Instant instant) throws IOException {
        if (instant != null) {
            jsonWriter.value(instant.toEpochMilli());
        } else {
            jsonWriter.nullValue();
        }
    }

    @Override
    public Instant read(final JsonReader jsonReader) throws IOException {
        long millisEpoch = jsonReader.nextLong();
        if (millisEpoch == -1) {
            return null;
        } else {
            return Instant.ofEpochMilli(millisEpoch);
        }
    }
});

祝你好运!"

英文:

It's easy, I think you can modify your TypeAdapter to check if the instant value is null and not write it to the JSON output

let me explain with a simple example, first, in write method, I check if the Instant value is null and write a null value instead of the epoch milliseconds, that will skip the null value in the JSON output and in the read method, I still check for the special case of -1L to convert it to null

gsonBuilder.registerTypeAdapter(Instant.class, new TypeAdapter&lt;Instant&gt;() {
    @Override
    public void write(final JsonWriter jsonWriter, final Instant instant) throws IOException {
        if (instant != null) {
            jsonWriter.value(instant.toEpochMilli());
        } else {
            jsonWriter.nullValue();
        }
    }

    @Override
    public Instant read(final JsonReader jsonReader) throws IOException {
        long millisEpoch = jsonReader.nextLong();
        if (millisEpoch == -1) {
            return null;
        } else {
            return Instant.ofEpochMilli(millisEpoch);
        }
    }
});

good luck!

huangapple
  • 本文由 发表于 2023年6月19日 04:12:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502368.html
匿名

发表评论

匿名网友

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

确定