fix json date format

This commit is contained in:
Sola
2015-12-11 18:24:11 +08:00
parent b03c2fabc0
commit 88a564e0fe
4 changed files with 23 additions and 5 deletions

View File

@@ -1,8 +1,11 @@
package love.sola.netsupport.wechat;
import com.google.gson.*;
import love.sola.netsupport.config.Lang;
import org.junit.Test;
import java.util.Date;
/**
* ***********************************************
* Created by Sola on 2015/12/2.
@@ -16,4 +19,14 @@ public class TestMessageFormat {
assert Lang.messages != null;
}
@Test
public void testJsonDate() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()))
.registerTypeAdapter(Date.class, (JsonSerializer<Date>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getTime()))
.create();
Date date = new Date();
assert gson.fromJson(gson.toJson(date), Date.class).compareTo(date) == 0;
}
}