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;
}
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations