-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade Jackson 1.x to Jackson 2.x #22
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/java/com/socrata/utils/ObjectMapperFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.socrata.utils; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.util.StdDateFormat; | ||
import com.fasterxml.jackson.datatype.joda.JodaModule; | ||
|
||
import java.text.*; | ||
import java.util.Date; | ||
import java.util.TimeZone; | ||
|
||
/** | ||
* Produces an instances of {@link ObjectMapper} for use with the Socrata API | ||
* | ||
* This is required to get "slightly" custom date parsing. The default Jackson behaviour will | ||
* turn a floating timestamp <code>2012-06-20T07:00:00</code> into Zulu time <code>2012-06-20T07:00:00<b>Z</b></code> | ||
* | ||
* This leads to problems if transforming into a Date or DateTime (although Joda's LocalDateTime would work fine) | ||
*/ | ||
public final class ObjectMapperFactory { | ||
|
||
protected static final SimpleDateFormat SOCRATA_WRITING_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); | ||
protected static final SimpleDateFormat SOCRATA_FLOATING_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | ||
protected static final SimpleDateFormat SOCRATA_FLOATING_FORMAT_MILLIS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); | ||
protected static final DateFormat[] SPECIAL_DATE_LIST = new DateFormat[] {SOCRATA_FLOATING_FORMAT_MILLIS, SOCRATA_FLOATING_FORMAT} ; | ||
|
||
public static ObjectMapper create() { | ||
return new ObjectMapper() | ||
.registerModule(new JodaModule()) | ||
.setDateFormat(new ObjectMapperFactory.SocrataDateFormat()); | ||
} | ||
|
||
/** | ||
* A class that special cases ISO 8601 dates, and assumes that no "Z" at | ||
* the end means that it should be translated as local time instead of Zulu time. | ||
* | ||
* This makes the Socrata floating date types work. | ||
*/ | ||
protected static class SocrataDateFormat extends StdDateFormat | ||
{ | ||
|
||
//Although, SimpleDateFormat is not thread safe, the entire SocrataDateFormat should be getting cloned by Jackson. | ||
final SimpleDateFormat SOCRATA_WRITE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); | ||
final SimpleDateFormat SOCRATA_FLOATING_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | ||
final SimpleDateFormat SOCRATA_FLOATING_FORMAT_MILLIS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); | ||
|
||
final DateFormat[] SPECIAL_DATE_LIST = new DateFormat[] {SOCRATA_FLOATING_FORMAT_MILLIS, SOCRATA_FLOATING_FORMAT} ; | ||
|
||
|
||
public SocrataDateFormat() | ||
{ | ||
final TimeZone localTimezone = TimeZone.getDefault(); | ||
SOCRATA_WRITE_FORMAT.setTimeZone(localTimezone); | ||
} | ||
|
||
/** | ||
* Overrides this method to special case ISO 8601 dates without 'Z' | ||
* to be local date/time. | ||
* | ||
* @param dateStr the string to parse | ||
* @param pos current position in the string | ||
* @return the Date returned. | ||
*/ | ||
@Override | ||
protected Date parseAsISO8601(String dateStr, ParsePosition pos, boolean throwErrors) throws ParseException { | ||
final Date retVal = parseAsFloatingISO8601(dateStr, pos); | ||
if (retVal != null) { | ||
return retVal; | ||
} | ||
return super.parseAsISO8601(dateStr, pos, throwErrors); | ||
} | ||
|
||
@Override | ||
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) | ||
{ | ||
return SOCRATA_WRITING_FORMAT.format(date, toAppendTo, fieldPosition); | ||
} | ||
|
||
/** | ||
* Parses an ISO8601 that does not contain a Z as a floating type. | ||
* | ||
* @param dateString the date string to parse | ||
* @param pos the position to start parsin gat. | ||
* @return | ||
*/ | ||
private Date parseAsFloatingISO8601(final String dateString, final ParsePosition pos) | ||
{ | ||
final int len = dateString.length(); | ||
final char c = dateString.charAt(len-1); | ||
if (c != 'z' && c != 'Z') { | ||
|
||
for (DateFormat format : SPECIAL_DATE_LIST) | ||
{ | ||
final ParsePosition testPos = new ParsePosition(pos.getIndex()); | ||
final DateFormat safeFormat = (DateFormat) format.clone(); | ||
final Date retVal = safeFormat.parse(dateString, testPos); | ||
|
||
if (retVal != null && testPos.getIndex()==len) { | ||
pos.setIndex(testPos.getIndex()); | ||
return retVal; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Need to override this, because StdDateFormat overrode this. | ||
* | ||
* @return A new SocrataDateFormat | ||
*/ | ||
@Override | ||
public StdDateFormat clone() | ||
{ | ||
return new SocrataDateFormat(); | ||
} | ||
} | ||
|
||
/** | ||
* static members only | ||
*/ | ||
private ObjectMapperFactory() { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
response.getEntity doesn't work with creating assets /api/assets. Keep the original mapper.readValue and error handlers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot find any documentation for the "assets" resource of the Import API. Does
response.getEntity
not work because this resource does not accept requests forapplication/json
so theMessageBodyReader
will not be triggered?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jackson looks for json content-type in getEntity. But the actual content type returned from the server is text/plain despite that it is json. Using readValue from input stream get around this problem.