use of org.bson.BasicBSONObject in project mongo-java-driver by mongodb.
the class JSONTest method testJSONEncoding.
@Test
public void testJSONEncoding() throws ParseException {
String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , " + "'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , " + "'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , " + "'$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , " + "'codews' : { '$code' : 'ggggg' , " + "'$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' : null, " + "'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
BasicDBObject a = (BasicDBObject) JSON.parse(json);
assertEquals("asdfasd", a.get("str"));
assertEquals(5, a.get("int"));
assertEquals(123123123123L, a.get("long"));
assertEquals(0.4d, a.get("float"));
assertEquals(false, a.get("bool"));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
assertEquals(format.parse("2011-05-18T18:56:00Z"), a.get("date"));
Pattern pat = (Pattern) a.get("pat");
Pattern pat2 = Pattern.compile(".*", BSON.regexFlags(""));
assertEquals(pat.pattern(), pat2.pattern());
assertEquals(pat.flags(), pat2.flags());
ObjectId oid = (ObjectId) a.get("oid");
assertEquals(new ObjectId("4d83ab3ea39562db9c1ae2ae"), oid);
// DBRef ref = (DBRef) a.get("ref");
// assertEquals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af")), ref);
assertEquals(new Code("asdfdsa"), a.get("code"));
assertEquals(new CodeWScope("ggggg", new BasicBSONObject()), a.get("codews"));
assertEquals(new BSONTimestamp(1300474885, 10), a.get("ts"));
assertEquals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5"), a.get("uuid"));
String json2 = JSON.serialize(a);
BasicDBObject b = (BasicDBObject) JSON.parse(json2);
assertEquals(JSON.serialize(b), JSON.serialize(b));
}
use of org.bson.BasicBSONObject in project jackrabbit-oak by apache.
the class ReplicaSetInfo method updateReplicaStatus.
void updateReplicaStatus() {
BasicDBObject result;
try {
result = getReplicaStatus();
} catch (MongoException e) {
LOG.error("Can't get replica status", e);
rootRevisions = null;
secondariesSafeTimestamp = 0;
return;
}
@SuppressWarnings("unchecked") Iterable<BasicBSONObject> members = (Iterable<BasicBSONObject>) result.get("members");
if (members == null) {
members = Collections.emptyList();
}
updateRevisions(members);
}
use of org.bson.BasicBSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseMinKey.
/**
* Check if a MinKey can be parsed. Refers issue #51
* @throws Exception if something goes wrong
*/
@Test
public void parseMinKey() throws Exception {
BSONObject o = new BasicBSONObject();
o.put("A", new MinKey());
Map<?, ?> data = parseBsonObject(o);
assertEquals(1, data.size());
assertEquals("MinKey", data.get("A"));
}
use of org.bson.BasicBSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseBig.
/**
* Test if {@link BigDecimal} objects can be deserialized
* @throws Exception if something goes wrong
*/
@Test
public void parseBig() throws Exception {
BSONObject o = new BasicBSONObject();
o.put("Double", 5.0);
o.put("Int32", 1234);
BSONEncoder enc = new BasicBSONEncoder();
byte[] b = enc.encode(o);
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectMapper mapper = new ObjectMapper(new BsonFactory());
mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
mapper.configure(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS, true);
Map<?, ?> data = mapper.readValue(bais, Map.class);
assertEquals(BigDecimal.class, data.get("Double").getClass());
assertEquals(BigInteger.class, data.get("Int32").getClass());
}
use of org.bson.BasicBSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseBinary.
/**
* Test if binary objects can be deserialized
* @throws Exception if something goes wrong
*/
@Test
public void parseBinary() throws Exception {
byte[] b = new byte[] { 1, 2, 3, 4, 5 };
BSONObject o = new BasicBSONObject();
o.put("b1", b);
o.put("b2", new Binary(BsonConstants.SUBTYPE_BINARY, b));
o.put("uuid", new UUID(1L, 2L));
Map<?, ?> data = parseBsonObject(o);
assertEquals(3, data.size());
assertArrayEquals(b, (byte[]) data.get("b1"));
assertArrayEquals(b, (byte[]) data.get("b2"));
assertEquals(new UUID(1L, 2L), data.get("uuid"));
}
Aggregations