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?