MongoDBから特定の形式のデータを読み込むコードがあります。私はそれをテストする必要があります。MongoDB:JSONからLongを読み込もうとするとjava.lang.Integerがjava.lang.Longにキャストできなくなる
そうするためには、私は私がテストしたいデータをJSONを作成します。
{
"id": ObjectId("57552e32e4b0839ede67e0af"),
"serial" : 574000690,
"startDate" : ISODate("2016-08-22T23:01:56.000Z"),
"endDate" : ISODate("2016-10-22T22:01:56.000Z"),
"reason": ""
}
これが作成されることになったオブジェクトです:
public static class MyObject implements Serializable{
private String id;
private long serial;
private Date startDate;
private Date endDate;
private String reason;
}
私は、コードを持っていますJSONファイルを読み込んでMongoドキュメントを作成し、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);
データが書き込まれるとMongoDBのデータを読み込み、MyObject
インスタンスにそれを移入しようとするコード:
public MyObject(Document doc) {
id = doc.getObjectId(DBConstants.ID).toString();
serial = doc.getLong(DBConstants.SERIAL);
startDate = doc.getDate("startDate");
reason = doc.getString("source");
}
:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
ためのラインserial = doc.getLong(DBConstants.SERIAL);
の。これは基本的に私のJSONから数値を "Integer"として取得し、それを長い間読み取ることができません。
私は次の行を試してみた、それが動作します:
Long.parseLong(doc.get(DBConstants.SERIAL).toString())
しかし、それはここに最適なソリューションですか? .toString()
はいつも私に数字の文字列表現を与えますか? Long
として読み取ることになるJSONの数値を保持する方法はありますか?
更新:
@glytchingは、優れた答えを与えました!
また、別の方法を発見しました。明らかに、JSONの数値要素をNumberLong
にラップすると、Mongoドキュメントから翻訳されたときにはLong
と解析されます。
{
"id": ObjectId("57552e32e4b0839ede67e0af"),
"serial" : NumberLong(574000690),
"startDate" : ISODate("2016-08-22T23:01:56.000Z"),
"endDate" : ISODate("2016-10-22T22:01:56.000Z"),
"reason": ""
}