Issue
I have one extended json string.
{"_id": {"oid": "59a47286cfa9a3a73e51e72c"}, "theaterId": {"numberInt": "101100"}, "location": {"address": {"street1": "340 XDW Market", "city": "Bloomington", "state": "MN", "zipcode": "12427"}, "geo": {"type": "Point", "coordinates": [{"$numberDouble": "-193.24565"}, {"$numberDouble": "144.85466"}]}}}
Trying to convert above json string to document in order to insert it into MongoDB. For this I am using org.bson.Document.Document.parse(json_string) constructor.
But the document I am getting after parsing, doesn't preserve the datatype inside geo.coordinate
arraylist (Check below Document). While it preserve datatype of theaterId
.
{
"_id": {
"oid": "59a47286cfa9a3a73e51e72c"
},
"theaterId": {
"numberInt": "101100"
},
"location": {
"address": {
"street1": "340 XDW Market",
"city": "Bloomington",
"state": "MN",
"zipcode": "12427"
},
"geo": {
"type": "Point",
"coordinates": [-193.24565, 144.85466]
}
}
}
Is this a potential issue in Document.parse() API ?
Solution
Your fields in geo.coordinate
are starting with dollar sign $
. In theaterId you have numberInt
, while in coordinate - $numberDouble
.
Check the docs and this question for how to handle it depending on what you need. Considering, that it looks like numberInt
satisfies your needs, you might just need to remove the dollars from field names.
Edit: After digging somewhat deeper into those docs, the one you provided as well, {"numberInt": "101100"}
is not extended json with datatype, it's just a normal json object with property and value for that property. It would need to be {"$numberInt": "101100"}
to be extended json. On the other hand {"$numberDouble": "-193.24565"}
is extended. The datatype is not lost, it's parsed into List<Double>
, since we know each element is of type Double
the datatype can be reconstructed back.
If you take at Document.toJson()
, under the hood it's working with RELAXED
output mode, which will output coordinates as you are seeing them - [-193.24565, 144.85466]
. If you provide EXTENDED
output mode, for example like this:
JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
System.out.println(document.toJson(settings));
then the datatype will be reconstructed back from the java type, and coordinates will look like so:
[{"$numberDouble": "-193.24565"}, {"$numberDouble": "144.85466"}]
In conclusion, there is no problem with Document.parse("json")
, but there might be a problem with the json you are supplying to it.
Edit2:
As in showed in example, the datatypes can be reconstructed back from java types. I am not familiar with the way collection.insertOne(Document.parse(json_string))
works under the hood, but if you don't explicitly specify the mode, it might be using RELAXED
by default, instead of EXTENDED
. The docs here state - This format prioritizes type preservation at the loss of human-readability and interoperability with older formats.
, so it would make sense. But this is just a wild guess on my part though, you would need to dig into docs to make sure.
Answered By - Chaosfire
Answer Checked By - Timothy Miller (JavaFixing Admin)