Skip to content

Commit 5370977

Browse files
committed
Add Json support for new ISODate()
JAVA-2022
1 parent accad21 commit 5370977

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

bson/src/main/org/bson/json/JsonReader.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,16 +728,20 @@ private long visitNumberLongConstructor() {
728728

729729
private long visitISODateTimeConstructor() {
730730
verifyToken("(");
731-
JsonToken valueToken = popToken();
732-
if (valueToken.getType() != JsonTokenType.STRING) {
733-
throw new JsonParseException("JSON reader expected a string but found '%s'.", valueToken.getValue());
731+
732+
JsonToken token = popToken();
733+
if (token.getType() == JsonTokenType.RIGHT_PAREN) {
734+
return new Date().getTime();
735+
} else if (token.getType() != JsonTokenType.STRING) {
736+
throw new JsonParseException("JSON reader expected a string but found '%s'.", token.getValue());
734737
}
738+
735739
verifyToken(")");
736740
String[] patterns = {"yyyy-MM-dd", "yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd'T'HH:mm:ss.SSSz"};
737741

738742
SimpleDateFormat format = new SimpleDateFormat(patterns[0], Locale.ENGLISH);
739743
ParsePosition pos = new ParsePosition(0);
740-
String s = valueToken.getValue(String.class);
744+
String s = token.getValue(String.class);
741745

742746
if (s.endsWith("Z")) {
743747
s = s.substring(0, s.length() - 1) + "GMT-00:00";

bson/src/test/unit/org/bson/json/JsonReaderTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
import org.bson.types.ObjectId;
2727
import org.junit.Test;
2828

29+
import java.util.Date;
30+
2931
import static org.junit.Assert.assertArrayEquals;
3032
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertTrue;
3134

3235

3336
public class JsonReaderTest {
@@ -704,22 +707,43 @@ public void testDateTimeConstructorWithNew() {
704707

705708
@Test
706709
public void testEmptyDateTimeConstructorWithNew() {
710+
long currentTime = new Date().getTime();
707711
String json = "new Date()";
708712
bsonReader = new JsonReader(json);
709713
assertEquals(BsonType.DATE_TIME, bsonReader.readBsonType());
710-
bsonReader.readDateTime();
714+
assertTrue(bsonReader.readDateTime() >= currentTime);
711715
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
712716
}
713717

714718
@Test
715719
public void testEmptyDateTimeConstructor() {
720+
long currentTime = new Date().getTime();
716721
String json = "Date()";
717722
bsonReader = new JsonReader(json);
718723
assertEquals(BsonType.DATE_TIME, bsonReader.readBsonType());
719-
bsonReader.readDateTime();
724+
assertTrue(bsonReader.readDateTime() >= currentTime);
725+
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
726+
}
727+
728+
@Test
729+
public void testEmptyISODateTimeConstructorWithNew() {
730+
long currentTime = new Date().getTime();
731+
String json = "new ISODate()";
732+
bsonReader = new JsonReader(json);
733+
assertEquals(BsonType.DATE_TIME, bsonReader.readBsonType());
734+
assertTrue(bsonReader.readDateTime() >= currentTime);
720735
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
721736
}
722737

738+
@Test
739+
public void testEmptyISODateTimeConstructor() {
740+
long currentTime = new Date().getTime();
741+
String json = "ISODate()";
742+
bsonReader = new JsonReader(json);
743+
assertEquals(BsonType.DATE_TIME, bsonReader.readBsonType());
744+
assertTrue(bsonReader.readDateTime() >= currentTime);
745+
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
746+
}
723747

724748
@Test
725749
public void testRegExp() {

0 commit comments

Comments
 (0)