Deserializing Dates from mongodb with custom CodecProvider in Java gives null results

1.1k views Asked by At

I have implemented a custom MongoDB CodecProvider to map to my java objects, using this Github gist. However, i cannot deserialize Date values, rather null values are returned. Here is the snippet of my custom encoder implementation for my pojo - AuditLog:

    public void encode(BsonWriter writer, AuditLog value, EncoderContext encoderContext) {
    Document document = new Document();
    DateCodec dateCodec = new DateCodec();
    ObjectId id = value.getLogId();
    Date timestamp = value.getTimestamp();
    String deviceId = value.getDeviceId();
    String userId = value.getUserId();
    String requestId = value.getRequestId();
    String operationType = value.getOperationType();
    String message = value.getMessage();
    String serviceName = value.getServiceName();
    String className = value.getClassName();

    if (null != id) {
        document.put("_id", id);
    }
    if (null != timestamp) {
        document.put("timestamp", timestamp);
    }
    if (null != deviceId) {
        document.put("deviceId", deviceId);
    }
    if (null != userId) {
        document.put("userId", userId);
    }
    if (null != requestId) {
        document.put("requestId", requestId);
    }
    if (null != operationType) {
        document.put("operationType", operationType);
    }
    if (null != message) {
        document.put("message", message);
    }
    if (null != serviceName) {
        document.put("serviceName", serviceName);
    }
    if (null != className) {
        document.put("className", className);
    }

    documentCodec.encode(writer, document, encoderContext);

}

and decoder:

public AuditLog decode(BsonReader reader, DecoderContext decoderContext) {
    Document document = documentCodec.decode(reader, decoderContext);
    System.out.println("document " + document);

    AuditLog auditLog = new AuditLog();

    auditLog.setLogId(document.getObjectId("_id"));
    auditLog.setTimestamp(document.getDate("timestamp"));
    auditLog.setDeviceId(document.getString("deviceId"));
    auditLog.setUserId(document.getString("userId"));
    auditLog.setRequestId(document.getString("requestId"));
    auditLog.setOperationType(document.getString("operationType"));
    auditLog.setMessage(document.getString("message"));
    auditLog.setServiceName(document.getString("serviceName"));
    auditLog.setClassName(document.getString("className"));

    return auditLog;
}

and the way I an reading:

public void getAuthenticationEntries() {

    Codec<Document> defaultDocumentCodec = MongoClient.getDefaultCodecRegistry().get(Document.class);

    AuditLogCodec auditLogCodec = new AuditLogCodec(defaultDocumentCodec);

    CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(),
            CodecRegistries.fromCodecs(auditLogCodec));

    MongoClientOptions options = MongoClientOptions.builder().codecRegistry(codecRegistry).build();

    MongoClient mc = new MongoClient("1.2.3.4:27017", options);
    MongoCollection<AuditLog> collection = mc.getDatabase("myDB").getCollection("myCol",
            AuditLog.class);
    BasicDBObject neQuery = new BasicDBObject();

    neQuery.put("myFiltr", new BasicDBObject("$eq", "mystuffr"));

    FindIterable<AuditLog> cursor = collection.find(neQuery);
    List<AuditLog> cleanList = new ArrayList<AuditLog>();
    for (AuditLog object : cursor) {

        System.out.println("timestamp: " + object.getTimestamp());

    }

}

My pojo:

public class AuditLog implements Bson {

@Id
private ObjectId  logId;

@JsonProperty("@timestamp")
private Date timestamp;
@JsonProperty("deviceId")
private String deviceId;
@JsonProperty("userId")
private String userId;
@JsonProperty("requestId")
private String requestId;
@JsonProperty("operationType")
private String operationType;

@JsonProperty("message")
private String message;
@JsonProperty("serviceName")
private String serviceName;
@JsonProperty("className")
private String className;
1

There are 1 answers

0
SyCode On BEST ANSWER

After a thorough research, I fixed the problem of returned null values. The mongoimport command was used to import the log files into Mongodbfrom elasticsearch. However, the time format was not converted to ISODate during the import operation. What I had to do was to update the time format to ISODate using the below command:

db.Collection.find().forEach(function (doc){  
doc.time = Date(time);
}); 
   db.dummy.save(doc);

Here is a related question that tackles a similar challenge.