I have a code that reads data in a specific format from MongoDB. I need to test it.
In order to do so, I create a JSON with the data I want to test:
{
"id": ObjectId("57552e32e4b0839ede67e0af"),
"serial" : 574000690,
"startDate" : ISODate("2016-08-22T23:01:56.000Z"),
"endDate" : ISODate("2016-10-22T22:01:56.000Z"),
"reason": ""
}
This is the object that supposed to be created:
public static class MyObject implements Serializable{
private String id;
private long serial;
private Date startDate;
private Date endDate;
private String reason;
}
I have a code that reads a JSON file and creates a Mongo document, and write to a DB:
List<Document> docs = dirAsDbObjects(dir + File.separator +
subDir.getName()).collect(Collectors.toList());
docs.forEach(docManipulator);
docs.forEach(doc -> doc.putIfAbsent("_id", new ObjectId()));
ret.addAll(docs);
MongoDatabase db = mongoClient.getDatabase(dbName);
MongoCollection<Document> coll = db.getCollection(subDir.getName());
List<InsertOneModel<Document>> inserts = docs.stream().map(InsertOneModel::new).collect(Collectors.toList());
coll.bulkWrite(inserts);
Once the data is written, there's a code that tries to read the data from MongoDB and populate it into a MyObject instance:
public MyObject(Document doc) {
id = doc.getObjectId(DBConstants.ID).toString();
serial = doc.getLong(DBConstants.SERIAL);
startDate = doc.getDate("startDate");
reason = doc.getString("source");
}
Which fails on a error message:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
Because of the line serial = doc.getLong(DBConstants.SERIAL);. It basically gets the number from my JSON as "Integer" thus unable to read it as long.
I've tried the following line, and it works:
Long.parseLong(doc.get(DBConstants.SERIAL).toString())
But is it the best solution here? Will .toString() always gets me the string representation of a number? Is there a way to keep a number in JSON that would cause it to be read as Long?
Update:
@glytching gave an excellent answer!
I also discovered another way, apparently if you wrap the numerical elemnt of JSON in NumberLong it will be parsed as Long when translated from Mongo document.
So using the old code, I was able to make it work by changing my JSON like so:
{
"id": ObjectId("57552e32e4b0839ede67e0af"),
"serial" : NumberLong(574000690),
"startDate" : ISODate("2016-08-22T23:01:56.000Z"),
"endDate" : ISODate("2016-10-22T22:01:56.000Z"),
"reason": ""
}