Skip to content

Commit

Permalink
serializing and deserializing of java.util.Date can now handle timezo…
Browse files Browse the repository at this point in the history
…ne offset - see ISO 8601
  • Loading branch information
Oliver Otzen committed Jan 18, 2011
1 parent c4ada42 commit 3459a92
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ src/test/ed/webtests/webtest-local.bash

*.ipr
*.iws
*.iml
.idea
10 changes: 5 additions & 5 deletions src/main/com/mongodb/util/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*/
public class JSON {

/**
protected static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";

/**
* Serializes an object into it's JSON form
*
* @param o object to serialize
Expand Down Expand Up @@ -136,10 +138,8 @@ public static void serialize( Object o , StringBuilder buf ){

if (o instanceof Date) {
Date d = (Date) o;
SimpleDateFormat format =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
serialize(new BasicDBObject("$date", format.format(d)), buf);
SimpleDateFormat format = new SimpleDateFormat(ISO_8601_DATE_FORMAT);
serialize(new BasicDBObject("$date", format.format(d)), buf);
return;
}

Expand Down
40 changes: 19 additions & 21 deletions src/main/com/mongodb/util/JSONCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,32 @@ public Object objectDone(){
BSONObject b = (BSONObject)o;

if ( ! _lastArray ) {
if ( b.containsKey( "$oid" ) ) {
o = new ObjectId((String)b.get("$oid"));
if (!isStackEmpty()) {
if ( b.containsKey( "$oid" ) ) {
o = new ObjectId((String)b.get("$oid"));
if (!isStackEmpty()) {
gotObjectId( _lastName, (ObjectId)o);
} else {
} else {
setRoot(o);
}
} else if ( b.containsKey( "$date" ) ) {
SimpleDateFormat format =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
o = format.parse((String)b.get("$date"), new ParsePosition(0));
if (!isStackEmpty()) {
}
} else if ( b.containsKey( "$date" ) ) {
SimpleDateFormat format = new SimpleDateFormat(JSON.ISO_8601_DATE_FORMAT);
o = format.parse((String)b.get("$date"), new ParsePosition(0));
if (!isStackEmpty()) {
cur().put( _lastName, o );
} else {
} else {
setRoot(o);
}
} else if ( b.containsKey( "$regex" ) ) {
o = Pattern.compile( (String)b.get( "$regex" ),
BSON.regexFlags( (String)b.get( "$options" )) );
if (!isStackEmpty()) {
}
} else if ( b.containsKey( "$regex" ) ) {
o = Pattern.compile( (String)b.get( "$regex" ),
BSON.regexFlags( (String)b.get( "$options" )) );
if (!isStackEmpty()) {
cur().put( _lastName, o );
} else {
} else {
setRoot(o);
}
}
}
}
}

return o;
}

Expand Down
5 changes: 2 additions & 3 deletions src/test/com/mongodb/util/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,7 @@ public void testObjectId() {
@org.testng.annotations.Test
public void testDate() {
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String formattedDate = format.format(d);

String serialized = JSON.serialize(d);
Expand All @@ -303,5 +302,5 @@ public void testDate() {
public static void main( String args[] ){
(new JSONTest()).runConsole();
}

}

0 comments on commit 3459a92

Please sign in to comment.