Skip to content

Commit

Permalink
Mapper : Schema less automatic date detection wrongly detects numbers…
Browse files Browse the repository at this point in the history
… as dates, closes #60.
  • Loading branch information
kimchy committed Mar 12, 2010
1 parent 47c11aa commit 65ed582
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,20 @@ private void serializeValue(JsonParseContext jsonContext, String currentFieldNam

BuilderContext builderContext = new BuilderContext(jsonContext.path());
if (token == JsonToken.VALUE_STRING) {
String text = jsonContext.jp().getText();
// check if it fits one of the date formats
boolean isDate = false;
for (FormatDateTimeFormatter dateTimeFormatter : dateTimeFormatters) {
try {
dateTimeFormatter.parser().parseMillis(jsonContext.jp().getText());
mapper = dateField(currentFieldName).dateTimeFormatter(dateTimeFormatter).build(builderContext);
isDate = true;
break;
} catch (Exception e) {
// failure to parse this, continue
// a safe check since "1" gets parsed as well
if (text.contains(":") || text.contains("-")) {
for (FormatDateTimeFormatter dateTimeFormatter : dateTimeFormatters) {
try {
dateTimeFormatter.parser().parseMillis(text);
mapper = dateField(currentFieldName).dateTimeFormatter(dateTimeFormatter).build(builderContext);
isDate = true;
break;
} catch (Exception e) {
// failure to parse this, continue
}
}
}
if (!isDate) {
Expand Down

0 comments on commit 65ed582

Please sign in to comment.