Gre*_*pff 37
我决定推出自己的开源软件 - 你可以在这里找到它:
这是Maven的详细信息(查看最新版本的中心):
<dependency>
<groupId>com.fatboyindustrial.gson-jodatime-serialisers</groupId>
<artifactId>gson-jodatime-serialisers</artifactId>
<version>1.6.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是一个如何驱动它的快速示例:
Gson gson = Converters.registerDateTime(new GsonBuilder()).create();
SomeContainerObject original = new SomeContainerObject(new DateTime());
String json = gson.toJson(original);
SomeContainerObject reconstituted = gson.fromJson(json, SomeContainerObject.class);
Run Code Online (Sandbox Code Playgroud)
Ily*_*lya 14
我在我的项目中使用next
public final class DateTimeDeserializer implements JsonDeserializer<DateTime>, JsonSerializer<DateTime>
{
static final org.joda.time.format.DateTimeFormatter DATE_TIME_FORMATTER =
ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
@Override
public DateTime deserialize(final JsonElement je, final Type type,
final JsonDeserializationContext jdc) throws JsonParseException
{
return je.getAsString().length() == 0 ? null : DATE_TIME_FORMATTER.parseDateTime(dateAsString);
}
@Override
public JsonElement serialize(final DateTime src, final Type typeOfSrc,
final JsonSerializationContext context)
{
return new JsonPrimitive(src == null ? StringUtils.EMPTY :DATE_TIME_FORMATTER.print(src));
}
}
Run Code Online (Sandbox Code Playgroud)
Jul*_*ien 11
使用GSON注册TypeAdapter以包装使用Joda预配置的格式化程序,请参阅http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
public class JodaDateTimeWithGson {
public static void main(String[] args) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>(){
@Override
public JsonElement serialize(DateTime json, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(ISODateTimeFormat.dateTime().print(json));
}
})
.create()
;
// Outputs ["20160222T15:58:33.218Z",42,"String"]
System.out.println(gson.toJson(new Object[] {DateTime.now(), 42, "String"}));
}
}
Run Code Online (Sandbox Code Playgroud)
Tao*_*yen 11
我使用上面的答案做了一个小助手,它将处理包含DateTime变量的模型对象的序列化和反序列化.
public static Gson gsonDateTime() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
@Override
public JsonElement serialize(DateTime json, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(ISODateTimeFormat.dateTime().print(json));
}
})
.registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
@Override
public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
DateTime dt = ISODateTimeFormat.dateTime().parseDateTime(json.getAsString());
return dt;
}
})
.create();
return gson;
}
Run Code Online (Sandbox Code Playgroud)