4242 */
4343public class JodaMapping {
4444
45+ private static final String DATE_FIELD_NAME = "$date" ;
46+
4547 public static class MongoDateTimeSerializer extends JsonSerializer <DateTime > {
4648
4749 @ Override
4850 public void serialize (DateTime value , JsonGenerator jgen , SerializerProvider provider ) throws IOException {
4951 jgen .writeStartObject ();
50- jgen .writeStringField ("$date" , value .withZone (DateTimeZone .UTC ).toString (ISODateTimeFormat .dateTime ()));
52+ jgen .writeStringField (JodaMapping . DATE_FIELD_NAME , value .withZone (DateTimeZone .UTC ).toString (ISODateTimeFormat .dateTime ()));
5153 jgen .writeEndObject ();
5254 }
5355
@@ -58,8 +60,22 @@ public static class MongoDateTimeDeserializer extends JsonDeserializer<DateTime>
5860 @ Override
5961 public DateTime deserialize (JsonParser jp , DeserializationContext ctxt ) throws IOException {
6062 JsonNode node = jp .getCodec ().readTree (jp );
61- String date = node .get ("$date" ).asText ();
62- return ISODateTimeFormat .dateTimeParser ().withZoneUTC ().parseDateTime (date );
63+ String date = node .get (JodaMapping .DATE_FIELD_NAME ).asText ();
64+ try {
65+ return ISODateTimeFormat .dateTimeParser ().withZoneUTC ().parseDateTime (date );
66+ } catch (IllegalArgumentException e ) {
67+ // Fallback for old dates (before 1970)
68+ JsonNode dateNode = node .get (JodaMapping .DATE_FIELD_NAME );
69+ if (dateNode != null ) {
70+ JsonNode numberLongNode = dateNode .get ("$numberLong" );
71+ if (numberLongNode != null ) {
72+ // Parse as BSON timestamp (milliseconds since epoch)
73+ long timestamp = numberLongNode .asLong ();
74+ return new DateTime (timestamp , DateTimeZone .UTC );
75+ }
76+ }
77+ }
78+ return null ;
6379 }
6480 }
6581}
0 commit comments