Comments (3)
We plan to add a Default Date/Time serialization/deserialization in version
1.2. The
tricky thing about this feature is that we do not know the format that the
client
supports. If the system using the GSON library passes these JSON serialized
object
back and forth and serializing/deserializing on both ends then its a non-issue
for
which format is supported; however, if you used the serialized version for
display
purposes then the date format matters.
For the time being, you can register the following Type Adapter:
/**
* A default type adapter for a {@link Date} object.
*
* @author Joel Leitch
*/
public class DateTypeAdapter implements JsonSerializer<Date>,
JsonDeserializer<Date> {
private final DateFormat format = DateFormat.getInstance();
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext
context) {
String dateFormatAsString = format.format(src);
return new JsonPrimitive(dateFormatAsString);
}
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext
context)
throws JsonParseException {
if (!(json instanceof JsonPrimitive)) {
throw new JsonParseException("The date should be a string value");
}
try {
return format.parse(json.getAsString());
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
}
// Create a GSON instance that can serialize/deserialize "java.util.Date"
objects
Gson gson = new GsonBuilder()
.registerTypeAdapter(new DateTypeAdapter())
.create();
Original comment by [email protected]
on 5 Aug 2008 at 6:41
- Changed state: Accepted
- Added labels: Type-Enhancement
- Removed labels: Type-Defect
from gson.
Submitted the type adapter to support this feature (r130). Still need to
update the
GsonBuilder class to make the Date TypeAdapter configurable without requiring
the
adapter to be registered.
Original comment by [email protected]
on 5 Aug 2008 at 7:30
from gson.
The change r145 contains the last piece of code required to close off this bug.
Removed the DateTypeAdapter from the public API so that it can only be
configured via
the GsonBuilder.
Original comment by [email protected]
on 13 Aug 2008 at 8:28
- Changed state: Fixed
from gson.
Related Issues (20)
- [2.11.0] error_prone_annotations dependency HOT 5
- The module declaration in version 2.11.0 is not visible HOT 11
- TypeAdapterFactory INTEGER_FACTORY will change INTEGER type to LONG type HOT 2
- errorprone cannot be considered optional HOT 4
- Can't read multiline string in JSON5 HOT 3
- Performance regressions from 2.10.1 to 2.11.0 on R8 minification task HOT 2
- Implement fallback if class `java.security.AccessController` does not exist HOT 5
- JDK21 toJson failure HOT 6
- Make com.google.gson.JsonElement Serializeable HOT 1
- Custom Gson, singleton mode, deserialization in multiple coroutines, resulting in crash HOT 2
- error HOT 4
- Read `json_name` from protobuf field annotations for serialization and deserialization
- TypeToken.getParameterized(Response.class, List.class, Long.class) get IllegalArgumentException: requires 1 type arguments, but got 2 HOT 3
- Just a Question for nested JSON HOT 2
- Yes can confirm this; it looks like this was indeed introduced by 3e3266cf48f132928225e1561a6ae4cb5503d08f (by accident): HOT 1
- java.lang.StackOverflowError: stack size 8MB HOT 1
- Gson behaviour is different in Unit test and in actual build HOT 2
- java.util.ConcurrentModificationException HOT 3
- Remove `examples/android-proguard-example`
- Failure to simultaneously resolve two consumers using each of v2.10.1 and v2.11.0 together in Eclipse IDE HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gson.