Search in sources :

Example 1 with Timestamp

use of de.undercouch.bson4jackson.types.Timestamp in project bson4jackson by michel-kraemer.

the class BsonTimestampDeserializer method deserialize.

@Override
@SuppressWarnings("deprecation")
public Timestamp 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_TIMESTAMP) {
            throw ctxt.mappingException(Timestamp.class);
        }
        return (Timestamp) bsonParser.getEmbeddedObject();
    } else {
        TreeNode tree = jp.getCodec().readTree(jp);
        int time = ((ValueNode) tree.get("$time")).asInt();
        int inc = ((ValueNode) tree.get("$inc")).asInt();
        return new Timestamp(time, inc);
    }
}
Also used : BsonParser(de.undercouch.bson4jackson.BsonParser) TreeNode(com.fasterxml.jackson.core.TreeNode) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) Timestamp(de.undercouch.bson4jackson.types.Timestamp)

Example 2 with Timestamp

use of de.undercouch.bson4jackson.types.Timestamp in project bson4jackson by michel-kraemer.

the class BsonGeneratorTest method timestamps.

/**
 * Test if {@link Timestamp} objects can be serialized
 * @throws Exception if something goes wrong
 */
@Test
public void timestamps() throws Exception {
    Timestamp timestamp = new Timestamp(100, 200);
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    data.put("timestamp", timestamp);
    BSONObject obj = generateAndParse(data);
    BSONTimestamp result = (BSONTimestamp) obj.get("timestamp");
    assertNotNull(result);
    assertEquals(timestamp.getInc(), result.getInc());
    assertEquals(timestamp.getTime(), result.getTime());
}
Also used : BSONObject(org.bson.BSONObject) BSONTimestamp(org.bson.types.BSONTimestamp) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) Timestamp(de.undercouch.bson4jackson.types.Timestamp) BSONTimestamp(org.bson.types.BSONTimestamp) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 3 with Timestamp

use of de.undercouch.bson4jackson.types.Timestamp in project bson4jackson by michel-kraemer.

the class BsonSerializersTest method timestamp.

/**
 * Tests {@link BsonTimestampSerializer}
 * @throws Exception if something goes wrong
 */
@Test
public void timestamp() throws Exception {
    Timestamp ts = new Timestamp(1, 2);
    org.bson.types.BSONTimestamp rts = (org.bson.types.BSONTimestamp) generateAndParse(ts);
    assertEquals(ts.getTime(), rts.getTime());
    assertEquals(ts.getInc(), rts.getInc());
}
Also used : Timestamp(de.undercouch.bson4jackson.types.Timestamp) Test(org.junit.Test)

Example 4 with Timestamp

use of de.undercouch.bson4jackson.types.Timestamp in project bson4jackson by michel-kraemer.

the class BsonParserTest method parseComplex.

/**
 * Test if a complex BSON object containing various values can be
 * deserialized
 * @throws Exception if something goes wrong
 */
@Test
public void parseComplex() throws Exception {
    BSONObject o = new BasicBSONObject();
    o.put("Timestamp", new BSONTimestamp(0xAABB, 0xCCDD));
    o.put("Symbol", new Symbol("Test"));
    o.put("ObjectId", org.bson.types.ObjectId.createFromLegacyFormat(Integer.MAX_VALUE, -2, Integer.MIN_VALUE));
    Pattern p = Pattern.compile(".*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNICODE_CASE);
    o.put("Regex", p);
    Map<?, ?> data = parseBsonObject(o);
    assertEquals(new Timestamp(0xAABB, 0xCCDD), data.get("Timestamp"));
    assertEquals(new de.undercouch.bson4jackson.types.Symbol("Test"), data.get("Symbol"));
    ObjectId oid = (ObjectId) data.get("ObjectId");
    assertEquals(Integer.MAX_VALUE, oid.getTime());
    assertEquals(-2, oid.getMachine());
    assertEquals(Integer.MIN_VALUE, oid.getInc());
    Pattern p2 = (Pattern) data.get("Regex");
    assertEquals(p.flags(), p2.flags());
    assertEquals(p.pattern(), p2.pattern());
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) Pattern(java.util.regex.Pattern) ObjectId(de.undercouch.bson4jackson.types.ObjectId) Symbol(org.bson.types.Symbol) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BSONTimestamp(org.bson.types.BSONTimestamp) Timestamp(de.undercouch.bson4jackson.types.Timestamp) BSONTimestamp(org.bson.types.BSONTimestamp) Test(org.junit.Test)

Aggregations

Timestamp (de.undercouch.bson4jackson.types.Timestamp)4 Test (org.junit.Test)3 BSONObject (org.bson.BSONObject)2 BSONTimestamp (org.bson.types.BSONTimestamp)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 ObjectId (de.undercouch.bson4jackson.types.ObjectId)1 LinkedHashMap (java.util.LinkedHashMap)1 Pattern (java.util.regex.Pattern)1 BasicBSONObject (org.bson.BasicBSONObject)1 Symbol (org.bson.types.Symbol)1