Search in sources :

Example 1 with BsonParser

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

the class BsonCalendarDeserializer method deserialize.

@Override
@SuppressWarnings("deprecation")
public Calendar 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_DATETIME) {
            throw ctxt.mappingException(Date.class);
        }
        Object obj = bsonParser.getEmbeddedObject();
        if (obj == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime((Date) obj);
        return cal;
    } else {
        Date date = new Date(jp.getLongValue());
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }
}
Also used : BsonParser(de.undercouch.bson4jackson.BsonParser) Calendar(java.util.Calendar) Date(java.util.Date)

Example 2 with BsonParser

use of de.undercouch.bson4jackson.BsonParser 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 3 with BsonParser

use of de.undercouch.bson4jackson.BsonParser 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 4 with BsonParser

use of de.undercouch.bson4jackson.BsonParser in project immutables by immutables.

the class BsonEncoding method unmarshalDbObject.

public static <T> T unmarshalDbObject(DBObject dbObject, TypeAdapter<T> adaper) throws IOException {
    BasicOutputBuffer buffer = new BasicOutputBuffer();
    encoder().writeObject(buffer, dbObject);
    BsonParser parser = BSON_FACTORY.createParser(buffer.toByteArray());
    BsonReader reader = new BsonReader(parser);
    T instance = adaper.read(reader);
    reader.close();
    return instance;
}
Also used : BsonParser(de.undercouch.bson4jackson.BsonParser) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 5 with BsonParser

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

the class BsonJavaScriptDeserializer method deserialize.

@Override
@SuppressWarnings("deprecation")
public JavaScript 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_JAVASCRIPT && bsonParser.getCurrentBsonType() != BsonConstants.TYPE_JAVASCRIPT_WITH_SCOPE)) {
            throw ctxt.mappingException(JavaScript.class);
        }
        return (JavaScript) bsonParser.getEmbeddedObject();
    } else {
        TreeNode tree = jp.getCodec().readTree(jp);
        String code = null;
        TreeNode codeNode = tree.get("$code");
        if (codeNode instanceof ValueNode) {
            code = ((ValueNode) codeNode).asText();
        }
        Map<String, Object> scope = null;
        TreeNode scopeNode = tree.get("$scope");
        if (scopeNode instanceof ObjectNode) {
            @SuppressWarnings("unchecked") Map<String, Object> scope2 = jp.getCodec().treeToValue(scopeNode, Map.class);
            scope = scope2;
        }
        return new JavaScript(code, scope);
    }
}
Also used : BsonParser(de.undercouch.bson4jackson.BsonParser) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TreeNode(com.fasterxml.jackson.core.TreeNode) JavaScript(de.undercouch.bson4jackson.types.JavaScript) ValueNode(com.fasterxml.jackson.databind.node.ValueNode)

Aggregations

BsonParser (de.undercouch.bson4jackson.BsonParser)6 TreeNode (com.fasterxml.jackson.core.TreeNode)4 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 JavaScript (de.undercouch.bson4jackson.types.JavaScript)1 ObjectId (de.undercouch.bson4jackson.types.ObjectId)1 Timestamp (de.undercouch.bson4jackson.types.Timestamp)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Pattern (java.util.regex.Pattern)1 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)1