I'm trying to have a custom date format in Gson output, but .setDateFormat(DateFormat.FULL) doesn't seem to work and it the same with .registerTypeAdapter(Date.class, new DateSerializer()).
It's like Gson doesn't care about the object "Date" and print it in its way.
How can I change that?
Thanks
EDIT:
#Entity
public class AdviceSheet {
public Date lastModif;
[...]
}
public void method {
Gson gson = new GsonBuilder().setDateFormat(DateFormat.LONG).create();
System.out.println(gson.toJson(adviceSheet);
}
I always use java.util.Date; setDateFormat() doesn't work :(
It seems that you need to define formats for both date and time part or use String-based formatting. For example:
Gson gson = new GsonBuilder()
.setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();
or using java.text.DateFormat
Gson gson = new GsonBuilder()
.setDateFormat(DateFormat.FULL, DateFormat.FULL).create();
or do it with serializers:
I believe that formatters cannot produce timestamps, but this serializer/deserializer-pair seems to work
JsonSerializer<Date> ser = new JsonSerializer<Date>() {
#Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext
context) {
return src == null ? null : new JsonPrimitive(src.getTime());
}
};
JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {
#Override
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return json == null ? null : new Date(json.getAsLong());
}
};
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, ser)
.registerTypeAdapter(Date.class, deser).create();
If using Java 8 or above you should use the above serializers/deserializers like so:
JsonSerializer<Date> ser = (src, typeOfSrc, context) -> src == null ? null
: new JsonPrimitive(src.getTime());
JsonDeserializer<Date> deser = (jSon, typeOfT, context) -> jSon == null ? null : new Date(jSon.getAsLong());
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
Above format seems better to me as it has precision up to millis.
As M.L. pointed out, JsonSerializer works here. However, if you are formatting database entities, use java.sql.Date to register you serializer. Deserializer is not needed.
Gson gson = new GsonBuilder()
.registerTypeAdapter(java.sql.Date.class, ser).create();
This bug report might be related: http://code.google.com/p/google-gson/issues/detail?id=230. I use version 1.7.2 though.
In case if you hate Inner classes, by taking the advantage of functional interface you can write less code in Java 8 with a lambda expression.
JsonDeserializer<Date> dateJsonDeserializer =
(json, typeOfT, context) -> json == null ? null : new Date(json.getAsLong());
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class,dateJsonDeserializer).create();
You can specify you format Gson gson = builder.setDateFormat("yyyy-MM-dd").create(); in this method instead of yyyy-MM-dd you can use anyother formats
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.setDateFormat("yyyy-MM-dd").create();
This is a bug. Currently you either have to set a timeStyle as well or use one of the alternatives described in the other answers.
I'm on Gson 2.8.6 and discovered this bug today.
My approach allows all our existing clients (mobile/web/etc) to continue functioning as they were, but adds some handling for those using 24h formats and allows millis too, for good measure.
Gson rawGson = new Gson();
SimpleDateFormat fmt = new SimpleDateFormat("MMM d, yyyy HH:mm:ss")
private class DateDeserializer implements JsonDeserializer<Date> {
#Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
try {
return new rawGson.fromJson(json, Date.class);
} catch (JsonSyntaxException e) {}
String timeString = json.getAsString();
log.warning("Standard date deserialization didn't work:" + timeString);
try {
return fmt.parse(timeString);
} catch (ParseException e) {}
log.warning("Parsing as json 24 didn't work:" + timeString);
return new Date(json.getAsLong());
}
}
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateDeserializer())
.create();
I kept serialization the same as all clients understand the standard json date format.
Ordinarily, I don't think it's good practice to use try/catch blocks to govern flow control, but this should be a fairly rare case.
This won't really work at all. There is no date type in JSON. I would recommend to serialize to ISO 8601 back and forth (for format agnostics and JS compat). Consider that you have to know which fields contain dates.
Related
I'm trying to have a custom date format in Gson output, but .setDateFormat(DateFormat.FULL) doesn't seem to work and it the same with .registerTypeAdapter(Date.class, new DateSerializer()).
It's like Gson doesn't care about the object "Date" and print it in its way.
How can I change that?
Thanks
EDIT:
#Entity
public class AdviceSheet {
public Date lastModif;
[...]
}
public void method {
Gson gson = new GsonBuilder().setDateFormat(DateFormat.LONG).create();
System.out.println(gson.toJson(adviceSheet);
}
I always use java.util.Date; setDateFormat() doesn't work :(
It seems that you need to define formats for both date and time part or use String-based formatting. For example:
Gson gson = new GsonBuilder()
.setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();
or using java.text.DateFormat
Gson gson = new GsonBuilder()
.setDateFormat(DateFormat.FULL, DateFormat.FULL).create();
or do it with serializers:
I believe that formatters cannot produce timestamps, but this serializer/deserializer-pair seems to work
JsonSerializer<Date> ser = new JsonSerializer<Date>() {
#Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext
context) {
return src == null ? null : new JsonPrimitive(src.getTime());
}
};
JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {
#Override
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return json == null ? null : new Date(json.getAsLong());
}
};
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, ser)
.registerTypeAdapter(Date.class, deser).create();
If using Java 8 or above you should use the above serializers/deserializers like so:
JsonSerializer<Date> ser = (src, typeOfSrc, context) -> src == null ? null
: new JsonPrimitive(src.getTime());
JsonDeserializer<Date> deser = (jSon, typeOfT, context) -> jSon == null ? null : new Date(jSon.getAsLong());
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
Above format seems better to me as it has precision up to millis.
As M.L. pointed out, JsonSerializer works here. However, if you are formatting database entities, use java.sql.Date to register you serializer. Deserializer is not needed.
Gson gson = new GsonBuilder()
.registerTypeAdapter(java.sql.Date.class, ser).create();
This bug report might be related: http://code.google.com/p/google-gson/issues/detail?id=230. I use version 1.7.2 though.
In case if you hate Inner classes, by taking the advantage of functional interface you can write less code in Java 8 with a lambda expression.
JsonDeserializer<Date> dateJsonDeserializer =
(json, typeOfT, context) -> json == null ? null : new Date(json.getAsLong());
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class,dateJsonDeserializer).create();
You can specify you format Gson gson = builder.setDateFormat("yyyy-MM-dd").create(); in this method instead of yyyy-MM-dd you can use anyother formats
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.setDateFormat("yyyy-MM-dd").create();
This is a bug. Currently you either have to set a timeStyle as well or use one of the alternatives described in the other answers.
I'm on Gson 2.8.6 and discovered this bug today.
My approach allows all our existing clients (mobile/web/etc) to continue functioning as they were, but adds some handling for those using 24h formats and allows millis too, for good measure.
Gson rawGson = new Gson();
SimpleDateFormat fmt = new SimpleDateFormat("MMM d, yyyy HH:mm:ss")
private class DateDeserializer implements JsonDeserializer<Date> {
#Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
try {
return new rawGson.fromJson(json, Date.class);
} catch (JsonSyntaxException e) {}
String timeString = json.getAsString();
log.warning("Standard date deserialization didn't work:" + timeString);
try {
return fmt.parse(timeString);
} catch (ParseException e) {}
log.warning("Parsing as json 24 didn't work:" + timeString);
return new Date(json.getAsLong());
}
}
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateDeserializer())
.create();
I kept serialization the same as all clients understand the standard json date format.
Ordinarily, I don't think it's good practice to use try/catch blocks to govern flow control, but this should be a fairly rare case.
This won't really work at all. There is no date type in JSON. I would recommend to serialize to ISO 8601 back and forth (for format agnostics and JS compat). Consider that you have to know which fields contain dates.
I need to get data from a badly designed web API which returns the list of objects in the form of JSON object:
{
"29593": { ..object to parse },
"29594": { ..object to parse },
"29600": { ..object to parse }
}
I need to create POJO for this response, but the issue is that these integers are changing, they are like object IDs. I don't know how to extract these integers from the JSON keys and then use the inner JSON objects further in another POJO class (I know basic Gson mapping when the key has a fixed value).
Is it even possible?
The solution is to use a custom JsonDeserializer from gson library, here is a example:
public class MyAwesomeDeserializer implements JsonDeserializer<MyModel> {
public MyModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject eJson = json.getAsJsonObject();
Set<String> keys = eJson.keySet();
MyModel myModel = new MyModel();
for (String key: keys) {
JsonObject asJsonObject = eJson.get(key).getAsJsonObject();
ItemOfMyModel itemOfMyModel = context.deserialize(asJsonObject, ItemOfMyModel.class);
myModel.addItemOfMyModel(itemOfMyModel);
}
return myModel;
}
}
and dont forget to add your custom deserializer as a type adapter to gson builder:
Gson gson = new GsonBuilder()
.registerTypeAdapter(MyModel.class, new MyAwesomeDeserializer())
.create()
I consume a rest service and i get a json object , and i map json object to my java object with Gson library.
But Date Json with following format not deserialized :
"/Date(1466606168687+0430)/"
I also checked following gson object ,but json date is not deserialized yet:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssz").create();
Update:
My problem is time zon for deserializing json date with timezon.
finally i use this code for date deserialization :
public class JsonDateDeserializer implements com.google.gson.JsonDeserializer<Date>{
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String s = json.getAsJsonPrimitive().getAsString();
String s1 = s.substring(6, s.length() - 2);
String[] saa = s1.split("\\+");
long l = Long.parseLong(saa[0]);
Date d = new Date(l);
return d;
}
}
I'm using GSON to serialise some object graphs to JSON. These objects graphs use Joda Time entities (DateTime, LocalTime etc).
The top Google hit for "gson joda" is this page:
https://sites.google.com/site/gson/gson-type-adapters-for-common-classes
It provides source for a type adapter for org.joda.time.DateTime. This link is also what is referenced in the GSON User Guide.
I expected to find a pre-rolled library that included joda-time serialisers that I could reference as a Maven dependency - but I can't find one.
Is there one? Or am I forced to replicate that snippet in my own project?
I've decided to roll my own open source one - you can find it here:
https://github.com/gkopff/gson-jodatime-serialisers
Here's the Maven details (check central for the latest version):
<dependency>
<groupId>com.fatboyindustrial.gson-jodatime-serialisers</groupId>
<artifactId>gson-jodatime-serialisers</artifactId>
<version>1.6.0</version>
</dependency>
And here's a quick example of how you drive it:
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);
I used the answers above to do a little helper that will handle both serialization and deserialization for model objects containing DateTime variables.
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;
}
I am using next in my project
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));
}
}
register a TypeAdapter with GSON to wrap the use of a Joda preconfigured Formatters, see 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"}));
}
}
It seems to me quite normal that you don't have this kind of library available.
It might look simple at first glance, but the risk is that you end up with a lot of dependencies (some at compile time, some at runtime), and it would not be easy to make sure you don't create unwanted dependencies.
For your case, this should be ok, as I think this is only a runtime dependency (Then a project using the serializerLib should not need JODA lib if JODA is not used). But for some other case, this could become ugly.
replicate that snippet, a lot of people use different date string formatting thus confusing any library creation.
I am trying to use Gson to deserialize a json array, but am currently getting a JsonSyntaxException. The json string was created by a .NET MVC3 web service using JsonResult (meaning, I am not manually creating the json, it is being created by a library which I know to work on several other platforms).
This is the json:
[{"PostID":1,"StudentID":39,"StudentName":"Joe Blow",
"Text":"Test message.","CreateDate":"\/Date(1350178408267)\/",
"ModDate":"\/Date(1350178408267)\/","CommentCount":0}]
This is the code:
public class Post {
public int PostID;
public int StudentID;
public String StudentName;
public String Text;
public Date CreateDate;
public Date ModDate;
public Post() { }
}
Type listOfPosts = new TypeToken<ArrayList<Post>>(){}.getType();
ArrayList<Post> posts = new Gson().fromJson(json, listOfPosts);
The exception says that the date format is invalid:
com.google.gson.JsonSyntaxException: /Date(1350178408267)/
Anyone know what is going on?
I found an answer here but I found it strange that there isn't an easier way. Several other json libraries I've used support the .NET json format natively. I was surprised when Gson didn't handle it. There must be a better way. If anyone knows of one, please post it here. All the same, this was my solution:
I created a custom JsonDeserializer and registered it for the Date type. By doing so, Gson will use my deserializer for the Date type instead of its default. The same can be done for any other type if you want to serialize/deserialize it in a custom way.
public class JsonDateDeserializer implements JsonDeserializer<Date> {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String s = json.getAsJsonPrimitive().getAsString();
long l = Long.parseLong(s.substring(6, s.length() - 2));
Date d = new Date(l);
return d;
}
}
Then, when I am creating my Gson object:
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create();
Now my gson object will be capable of parsing the .NET date format (millis since 1970).
Another solution is to use ISO 8601 format. This has to be configured on both Gson side as:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
as well as on the server side, e.g. for ASP.NET MVC in Global.asax.cs file, as follows:
JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(new IsoDateTimeConverter());
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = serializerSettings;
The advantage of the code above is that it handles both serialization and deserialization and thus allows two way transmission of dates/times.
Note: IsoDateTimeConverter class is part of the JSON.NET library.
Serialize and Deserialize methoda. Register this as a Adapter for GSON
JsonSerializer<Date> ser = new JsonSerializer<Date>() {
#Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext
context) {
return src == null ? null : new JsonPrimitive(src.getTime());
}
};
JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {
#Override
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return json == null ? null : new Date(json.getAsLong());
}
};
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, ser)
.registerTypeAdapter(Date.class, deser).create();
This solution works for me by using SqlDateTypeAdapter:
SqlDateTypeAdapter sqlAdapter = new SqlDateTypeAdapter();
Gson gson = new GsonBuilder()
.registerTypeAdapter(java.sql.Date.class, sqlAdapter)
.setDateFormat("yyyy-MM-dd")
.create();
Ref: https://stackoverflow.com/a/30398307/7308789