With the legacy shell, the Timestamp is correctly serialized by JSON.stringify
JSON.stringify(new Timestamp(1765119887, 0))
// {"$timestamp":{"t":1765119887,"i":0}}
But with mongosh, the behavior of this method is changed:
JSON.stringify(new Timestamp(1765119887, 0))
{"$timestamp":"7581132188184215552"}
To make it backward compatible, we can use EJSON.stringify with a replacer the make sure the output is the same.