Mapper : Schema less automatic date detection wrongly detects numbers as dates, closes #60.

This commit is contained in:
kimchy 2010-03-12 18:16:35 +02:00
parent 47c11aa538
commit 65ed582a90
1 changed files with 12 additions and 8 deletions

View File

@ -306,16 +306,20 @@ public class JsonObjectMapper implements JsonMapper {
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) {