Search in sources :

Example 6 with BsonParser

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

the class BsonRegexDeserializer method deserialize.

@Override
@SuppressWarnings("deprecation")
public Pattern 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_REGEX) {
            throw ctxt.mappingException(Pattern.class);
        }
        return (Pattern) bsonParser.getEmbeddedObject();
    } else {
        TreeNode tree = jp.getCodec().readTree(jp);
        String pattern = null;
        TreeNode patternNode = tree.get("$pattern");
        if (patternNode instanceof ValueNode) {
            pattern = ((ValueNode) patternNode).asText();
        }
        int flags = 0;
        TreeNode flagsNode = tree.get("$flags");
        if (flagsNode instanceof ValueNode) {
            flags = ((ValueNode) flagsNode).asInt();
        }
        return Pattern.compile(pattern, flags);
    }
}
Also used : Pattern(java.util.regex.Pattern) BsonParser(de.undercouch.bson4jackson.BsonParser) TreeNode(com.fasterxml.jackson.core.TreeNode) 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