mongodb java json bson_Jackson ObjectMapper from MongoDB BSON

I got a JSON that I serialize to a MongoDB BasicDBObject and insert it into the DB:

String serialized = "";

try {

serialized = OBJECT_MAPPER.writeValueAsString(customEx.getOut().getBody());

} catch (JsonProcessingException e) {

e.printStackTrace();

}

collection.update(upsertQuery, BasicDBObject.parse(serialized), true, false);

On reading the DBObject from the DB I want to convert it to a POJO using the ObjectMappers' 'readValue' with a given class:

public static T fromDB(DBObject o, Class clazz) {

try {

return OBJECT_MAPPER.readValue(o.toString(), clazz);

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

The class I want to convert it to is generated from an xsd sheme and also contains timestamps/long values as following:

@XmlRootElement(name = "ItemType")

public class ItemType {

@XmlSchemaType(name = "dateTime")

protected XMLGregorianCalendar date;

[...]

However this worked fine for older Java MongoDB versions. Now the long values are serialized as BSON looking like this:

"date": {

"$numberLong": "1551172199214"

}

When I try to deserialize this using jacksons ObjectMapper I get

Cannot deserialize instance of javax.xml.datatype.XMLGregorianCalendar out of START_OBJECT token

The reason for this is clear to me because the long value is in an own BSON - Style object.

So far I already tried using BsonDocument like this:

public static T fromDB(DBObject o, Class clazz) {

try {

BsonDocument parse = BsonDocument.parse(o.toString());

return OBJECT_MAPPER.readValue(parse.toJson(), clazz);

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

But that still does not convert the BSON parts to JSON.

Is there any way to deserialize the BSON to a given Class using jacksons ObjectMapper? Or just convert it to a DBObject without using BSON parts?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值