Adding a converter could work, but of course the result json differs:
JsonWriterSettings settings = JsonWriterSettings.builder()
.outputMode(JsonMode.EXTENDED)
.dateTimeConverter((value, writer) -> writer.writeNumber(value.toString()))
.build();
try (StringReader sReader = new StringReader(bson.toJson(settings));
{
"entryDate":1647457718815
}
Which differs from
"entryDate": {
"$date": 1647457718815
}
That’s a breaking change.
Changing JsonMode
to STRICT
works, but that option is deprecated.
JsonWriterSettings settings = JsonWriterSettings.builder()
.outputMode(JsonMode.STRICT)
.build();
In looking at the documentation, it says:
Strict Mode: Legacy representation. Though now deprecated, this is still the default mode when writing JSON in order to avoid breaking backwards compatibility. This may change in a future major release of the driver.
That’s obviously not true, because the default (no JsonWriterSettings
supplied to the toJson()
method) clearly behaves differently from supplying .outputMode(JsonMode.STRICT)
.
Either the documentation is wrong, or there is a bug in the driver.