use of de.undercouch.bson4jackson.types.ObjectId in project bson4jackson by michel-kraemer.
the class BsonObjectIdDeserializer method deserialize.
@Override
@SuppressWarnings("deprecation")
public ObjectId deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
if (jp instanceof BsonParser) {
BsonParser bsonParser = (BsonParser) jp;
if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_OBJECTID) {
throw ctxt.mappingException(ObjectId.class);
}
return (ObjectId) bsonParser.getEmbeddedObject();
} else {
TreeNode tree = jp.getCodec().readTree(jp);
int time = ((ValueNode) tree.get("$time")).asInt();
int machine = ((ValueNode) tree.get("$machine")).asInt();
int inc = ((ValueNode) tree.get("$inc")).asInt();
return new ObjectId(time, machine, inc);
}
}
use of de.undercouch.bson4jackson.types.ObjectId in project bson4jackson by michel-kraemer.
the class BsonSerializersTest method objectId.
/**
* Tests {@link BsonObjectIdSerializer}
* @throws Exception if something goes wrong
*/
@Test
public void objectId() throws Exception {
ObjectId id = new ObjectId(1, 2, 3);
org.bson.types.ObjectId roid = (org.bson.types.ObjectId) generateAndParse(id);
assertEquals(org.bson.types.ObjectId.createFromLegacyFormat(id.getTime(), id.getMachine(), id.getInc()), roid);
}
use of de.undercouch.bson4jackson.types.ObjectId in project immutables by immutables.
the class BsonReader method nextObjectId.
public byte[] nextObjectId() throws IOException {
if (peekedObjectId()) {
consumePeek();
ObjectId id = (ObjectId) parser.getEmbeddedObject();
byte[] bytes = new byte[12];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.putInt(id.getTime());
buffer.putInt(id.getMachine());
buffer.putInt(id.getInc());
return bytes;
}
throw unexpectedFor("ObjectID");
}
use of de.undercouch.bson4jackson.types.ObjectId in project immutables by immutables.
the class BsonWriter method valueObjectId.
public void valueObjectId(byte[] data) throws IOException {
Preconditions.checkArgument(data.length == 12, "ObjectId byte[] should have exactly 12 bytes");
ByteBuffer bytes = ByteBuffer.wrap(data);
ObjectId objectId = new ObjectId(bytes.getInt(), bytes.getInt(), bytes.getInt());
generator.writeObjectId(objectId);
}
use of de.undercouch.bson4jackson.types.ObjectId in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method objectIds.
/**
* Test if {@link ObjectId}s can be serialized
* @throws Exception if something goes wrong
*/
@Test
public void objectIds() throws Exception {
Map<String, Object> data = new LinkedHashMap<String, Object>();
ObjectId objectId = new ObjectId((int) (System.currentTimeMillis() / 1000), new Random().nextInt(), 100);
data.put("_id", objectId);
BSONObject obj = generateAndParse(data);
org.bson.types.ObjectId result = (org.bson.types.ObjectId) obj.get("_id");
assertNotNull(result);
assertEquals(org.bson.types.ObjectId.createFromLegacyFormat(objectId.getTime(), objectId.getMachine(), objectId.getInc()), result);
}
Aggregations