use of java.util.SimpleTimeZone in project mongo-java-driver by mongodb.
the class JSONCallback method objectDone.
@Override
public Object objectDone() {
String name = curName();
Object o = super.objectDone();
if (_lastArray) {
return o;
}
BSONObject b = (BSONObject) o;
// override the object if it's a special type
if (b.containsField("$oid")) {
o = new ObjectId((String) b.get("$oid"));
} else if (b.containsField("$date")) {
if (b.get("$date") instanceof Number) {
o = new Date(((Number) b.get("$date")).longValue());
} else {
SimpleDateFormat format = new SimpleDateFormat(_msDateFormat);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
o = format.parse(b.get("$date").toString(), new ParsePosition(0));
if (o == null) {
// try older format with no ms
format = new SimpleDateFormat(_secDateFormat);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
o = format.parse(b.get("$date").toString(), new ParsePosition(0));
}
}
} else if (b.containsField("$regex")) {
o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
} else if (b.containsField("$ts")) {
//Legacy timestamp format
Integer ts = ((Number) b.get("$ts")).intValue();
Integer inc = ((Number) b.get("$inc")).intValue();
o = new BSONTimestamp(ts, inc);
} else if (b.containsField("$timestamp")) {
BSONObject tsObject = (BSONObject) b.get("$timestamp");
Integer ts = ((Number) tsObject.get("t")).intValue();
Integer inc = ((Number) tsObject.get("i")).intValue();
o = new BSONTimestamp(ts, inc);
} else if (b.containsField("$code")) {
if (b.containsField("$scope")) {
o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
} else {
o = new Code((String) b.get("$code"));
}
} else if (b.containsField("$ref")) {
o = new DBRef((String) b.get("$ref"), b.get("$id"));
} else if (b.containsField("$minKey")) {
o = new MinKey();
} else if (b.containsField("$maxKey")) {
o = new MaxKey();
} else if (b.containsField("$uuid")) {
o = UUID.fromString((String) b.get("$uuid"));
} else if (b.containsField("$binary")) {
int type = (b.get("$type") instanceof String) ? Integer.valueOf((String) b.get("$type"), 16) : (Integer) b.get("$type");
byte[] bytes = DatatypeConverter.parseBase64Binary((String) b.get("$binary"));
o = new Binary((byte) type, bytes);
} else if (b.containsField("$undefined") && b.get("$undefined").equals(true)) {
o = new BsonUndefined();
} else if (b.containsField("$numberLong")) {
o = Long.valueOf((String) b.get("$numberLong"));
} else if (b.containsField("$numberDecimal")) {
o = Decimal128.parse((String) b.get("$numberDecimal"));
}
if (!isStackEmpty()) {
_put(name, o);
} else {
o = !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks(o);
setRoot(o);
}
return o;
}
use of java.util.SimpleTimeZone in project mongo-java-driver by mongodb.
the class JSONSerializersTest method testLegacySerialization.
@Test
public void testLegacySerialization() {
ObjectSerializer serializer = JSONSerializers.getLegacy();
BasicDBObject testObj = new BasicDBObject();
// test ARRAY
BasicDBObject[] a = { new BasicDBObject("object1", "value1"), new BasicDBObject("object2", "value2") };
testObj.put("array", a);
StringBuilder buf = new StringBuilder();
serializer.serialize(a, buf);
assertEquals("[ { \"object1\" : \"value1\"} , { \"object2\" : \"value2\"}]", buf.toString());
// test BINARY
byte[] b = { 1, 2, 3, 4 };
testObj = new BasicDBObject("binary", new org.bson.types.Binary(b));
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals("{ \"binary\" : <Binary Data>}", buf.toString());
// test BOOLEAN
testObj = new BasicDBObject("boolean", new Boolean(true));
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals(buf.toString(), "{ \"boolean\" : true}");
// test BSON_TIMESTAMP,
testObj = new BasicDBObject("timestamp", new BSONTimestamp());
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals("{ \"timestamp\" : { \"$ts\" : 0 , \"$inc\" : 0}}", buf.toString());
// test BYTE_ARRAY
testObj = new BasicDBObject("byte_array", b);
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals("{ \"byte_array\" : <Binary Data>}", buf.toString());
// test CODE
testObj = new BasicDBObject("code", new Code("test code"));
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals("{ \"code\" : { \"$code\" : \"test code\"}}", buf.toString());
// test CODE_W_SCOPE
testObj = new BasicDBObject("scope", "scope of code");
CodeWScope codewscope = new CodeWScope("test code", testObj);
buf = new StringBuilder();
serializer.serialize(codewscope, buf);
assertEquals("{ \"$code\" : \"test code\" , \"$scope\" : { \"scope\" : \"scope of code\"}}", buf.toString());
// test DATE
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
buf = new StringBuilder();
serializer.serialize(d, buf);
assertEquals("{ \"$date\" : \"" + format.format(d) + "\"}", buf.toString());
// test DB_OBJECT implicit in preceding tests
// test DB_REF_BASE
DBRef dbref = new com.mongodb.DBRef("test.test", "4d83ab59a39562db9c1ae2af");
buf = new StringBuilder();
serializer.serialize(dbref, buf);
assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\"}", buf.toString());
DBRef dbrefWithDatabaseName = new com.mongodb.DBRef("mydb", "test.test", "4d83ab59a39562db9c1ae2af");
buf = new StringBuilder();
serializer.serialize(dbrefWithDatabaseName, buf);
assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\" , \"$db\" : \"mydb\"}", buf.toString());
// test ITERABLE
BasicBSONList testList = new BasicBSONList();
testList.add(new BasicDBObject("key1", "val1"));
testList.add(new BasicDBObject("key2", "val2"));
buf = new StringBuilder();
serializer.serialize(testList, buf);
assertEquals("[ { \"key1\" : \"val1\"} , { \"key2\" : \"val2\"}]", buf.toString());
// test MAP
Map<String, String> testMap = new TreeMap<String, String>();
testMap.put("key1", "val1");
testMap.put("key2", "val2");
buf = new StringBuilder();
serializer.serialize(testMap, buf);
assertEquals("{ \"key1\" : \"val1\" , \"key2\" : \"val2\"}", buf.toString());
// test MAXKEY
buf = new StringBuilder();
serializer.serialize(new MaxKey(), buf);
assertEquals("{ \"$maxKey\" : 1}", buf.toString());
// test MINKEY
buf = new StringBuilder();
serializer.serialize(new MinKey(), buf);
assertEquals("{ \"$minKey\" : 1}", buf.toString());
// test NULL
buf = new StringBuilder();
serializer.serialize(null, buf);
assertEquals(" null ", buf.toString());
// test NUMBER
Random rand = new Random();
long val = rand.nextLong();
Long longVal = new Long(val);
buf = new StringBuilder();
serializer.serialize(longVal, buf);
assertEquals(String.valueOf(val), buf.toString());
// test OBJECT_ID
buf = new StringBuilder();
serializer.serialize(new ObjectId("4d83ab3ea39562db9c1ae2ae"), buf);
assertEquals("{ \"$oid\" : \"4d83ab3ea39562db9c1ae2ae\"}", buf.toString());
// test PATTERN
buf = new StringBuilder();
serializer.serialize(Pattern.compile("test"), buf);
assertEquals("{ \"$regex\" : \"test\"}", buf.toString());
// test STRING
buf = new StringBuilder();
serializer.serialize("test string", buf);
assertEquals("\"test string\"", buf.toString());
// test UUID;
UUID uuid = UUID.randomUUID();
buf = new StringBuilder();
serializer.serialize(uuid, buf);
assertEquals("{ \"$uuid\" : \"" + uuid.toString() + "\"}", buf.toString());
// test Decimal128
Decimal128 decimal128 = Decimal128.parse("3.140");
buf = new StringBuilder();
serializer.serialize(decimal128, buf);
assertEquals("{ \"$numberDecimal\" : \"3.140\"}", buf.toString());
}
use of java.util.SimpleTimeZone in project mongo-java-driver by mongodb.
the class JSONCallbackTest method dateParsing.
@Test
public void dateParsing() {
SimpleDateFormat format = new SimpleDateFormat(JSONCallback._msDateFormat);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
Date rightNow = new Date();
rightNow.setTime(System.currentTimeMillis());
Date parsedDate = (Date) JSON.parse("{ \"$date\" : " + rightNow.getTime() + "}");
assertEquals(0, rightNow.compareTo(parsedDate));
// Test formatted dates with ms granularity
parsedDate = (Date) JSON.parse("{ \"$date\" : \"" + format.format(rightNow) + "\"}");
assertEquals(0, parsedDate.compareTo(format.parse(format.format(rightNow), new ParsePosition(0))));
// Test formatted dates with sec granularity
format = new SimpleDateFormat(JSONCallback._secDateFormat);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
parsedDate = (Date) JSON.parse("{ \"$date\" : \"" + format.format(rightNow) + "\"}");
assertEquals(0, parsedDate.compareTo(format.parse(format.format(rightNow), new ParsePosition(0))));
}
use of java.util.SimpleTimeZone in project robovm by robovm.
the class SimpleTimeZoneTest method test_getDSTSavings.
/**
* java.util.SimpleTimeZone#getDSTSavings()
*/
public void test_getDSTSavings() {
// Test for method int java.util.SimpleTimeZone.getDSTSavings()
st1 = new SimpleTimeZone(0, "TEST");
assertEquals("Non-zero default daylight savings", 0, st1.getDSTSavings());
st1.setStartRule(0, 1, 1, 1);
st1.setEndRule(11, 1, 1, 1);
assertEquals("Incorrect default daylight savings", 3600000, st1.getDSTSavings());
st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL, 1, -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 3600000, 7200000);
assertEquals("Incorrect daylight savings from constructor", 7200000, st1.getDSTSavings());
}
use of java.util.SimpleTimeZone in project robovm by robovm.
the class SimpleTimeZoneTest method test_hashCode.
/**
* java.util.SimpleTimeZone#hashCode()
*/
public void test_hashCode() {
// Test for method int java.util.SimpleTimeZone.hashCode()
// For lack of a better test.
st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL, 1, -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 3600000);
assertTrue(TimeZone.getTimeZone("EST").hashCode() != 0);
assertTrue(st1.hashCode() != 0);
}
Aggregations