英文:
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<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);
}
}
});
答案1
得分: 2
这是翻译好的内容:
"很简单,我认为你可以修改你的 TypeAdapter
来检查即时值是否为 null,然后不将其写入 JSON
输出中。
让我用一个简单的例子来解释一下,首先在 write
方法中,我检查 Instant
值是否为 null,如果是,就写入一个空值,而不是时代毫秒,这将跳过 JSON 输出中的空值,在 read
方法中,我仍然检查特殊情况下的 -1L
,将其转换为 null。
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.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<Instant>() {
@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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论