Search in sources :

Example 1 with ObjectId

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);
    }
}
Also used : BsonParser(de.undercouch.bson4jackson.BsonParser) ObjectId(de.undercouch.bson4jackson.types.ObjectId) TreeNode(com.fasterxml.jackson.core.TreeNode) ValueNode(com.fasterxml.jackson.databind.node.ValueNode)

Example 2 with ObjectId

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);
}
Also used : ObjectId(de.undercouch.bson4jackson.types.ObjectId) Test(org.junit.Test)

Example 3 with ObjectId

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");
}
Also used : ObjectId(de.undercouch.bson4jackson.types.ObjectId) ByteBuffer(java.nio.ByteBuffer)

Example 4 with 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);
}
Also used : ObjectId(de.undercouch.bson4jackson.types.ObjectId) ByteBuffer(java.nio.ByteBuffer)

Example 5 with 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);
}
Also used : Random(java.util.Random) ObjectId(de.undercouch.bson4jackson.types.ObjectId) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

ObjectId (de.undercouch.bson4jackson.types.ObjectId)6 Test (org.junit.Test)3 ByteBuffer (java.nio.ByteBuffer)2 BSONObject (org.bson.BSONObject)2 SerializableString (com.fasterxml.jackson.core.SerializableString)1 TreeNode (com.fasterxml.jackson.core.TreeNode)1 SerializedString (com.fasterxml.jackson.core.io.SerializedString)1 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)1 BsonParser (de.undercouch.bson4jackson.BsonParser)1 Timestamp (de.undercouch.bson4jackson.types.Timestamp)1 LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1 Pattern (java.util.regex.Pattern)1 BasicBSONObject (org.bson.BasicBSONObject)1 BSONTimestamp (org.bson.types.BSONTimestamp)1 Symbol (org.bson.types.Symbol)1