使用 Gson 将 Java 8 LocalDate 序列化为 yyyy-mm-dd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39192945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serialize Java 8 LocalDate as yyyy-mm-dd with Gson
提问by Drux
I am using Java 8 and the latest RELEASE
version (via Maven) of Gson.
If I serialize a LocalDate
I get something like this
我正在使用 Java 8 和Gson的最新RELEASE
版本(通过 Maven)。如果我序列化 a我会得到这样的东西LocalDate
"birthday": {
"year": 1997,
"month": 11,
"day": 25
}
where I would have preferred "birthday": "1997-11-25"
. Does Gson also support the more concise format out-of-the-box, or do I have to implement a custom serializer for LocalDate
s?
我更喜欢的地方"birthday": "1997-11-25"
。Gson 是否也支持开箱即用的更简洁的格式,还是我必须为LocalDate
s实现自定义序列化程序?
(I've tried gsonBuilder.setDateFormat(DateFormat.SHORT)
, but that does not seem to make a difference.)
(我试过了gsonBuilder.setDateFormat(DateFormat.SHORT)
,但这似乎没有什么区别。)
回答by Drux
Until further notice, I have implemented a custom serializer like so:
在进一步通知之前,我已经实现了一个自定义序列化程序,如下所示:
class LocalDateAdapter implements JsonSerializer<LocalDate> {
public JsonElement serialize(LocalDate date, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(date.format(DateTimeFormatter.ISO_LOCAL_DATE)); // "yyyy-mm-dd"
}
}
It can be installed e.g. like so:
它可以像这样安装:
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
.create();
回答by Sam Barnum
I use the following, supports read/write and null values:
我使用以下,支持读/写和空值:
class LocalDateAdapter extends TypeAdapter<LocalDate> {
@Override
public void write(final JsonWriter jsonWriter, final LocalDate localDate) throws IOException {
if (localDate == null) {
jsonWriter.nullValue();
} else {
jsonWriter.value(localDate.toString());
}
}
@Override
public LocalDate read(final JsonReader jsonReader) throws IOException {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
} else {
return LocalDate.parse(jsonReader.nextString());
}
}
}
Registered as @Drux says:
注册为@Drux 说:
return new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
.create();
EDIT 2019-04-04A simpler implementation
编辑 2019-04-04更简单的实现
private static final class LocalDateAdapter extends TypeAdapter<LocalDate> {
@Override
public void write( final JsonWriter jsonWriter, final LocalDate localDate ) throws IOException {
jsonWriter.value(localDate.toString());
}
@Override
public LocalDate read( final JsonReader jsonReader ) throws IOException {
return LocalDate.parse(jsonReader.nextString());
}
}
Which you can add null
support to by registering the nullSafe()
wrapped version:
您可以null
通过注册nullSafe()
包装版本来添加支持:
new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter().nullSafe())
回答by Cristan
Kotlin version which supports serializing and deserializing:
支持序列化和反序列化的 Kotlin 版本:
class LocalDateTypeAdapter : TypeAdapter<LocalDate>() {
override fun write(out: JsonWriter, value: LocalDate) {
out.value(DateTimeFormatter.ISO_LOCAL_DATE.format(value))
}
override fun read(input: JsonReader): LocalDate = LocalDate.parse(input.nextString())
}
Register with your GsonBuilder. Wrap using nullSafe()
for null
support:
注册您的 GsonBuilder。包装nullSafe()
用于null
支持:
GsonBuilder().registerTypeAdapter(LocalDate::class.java, LocalDateTypeAdapter().nullSafe())