Skip to content

Commit

Permalink
Fix issues on get hourly trends feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vergnesOL committed Mar 21, 2012
1 parent a6d8573 commit 705d9b7
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 17 deletions.
Expand Up @@ -26,9 +26,9 @@
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

public class DateDeserializer extends JsonDeserializer<Date> {
public class DateFormatDeserializer extends JsonDeserializer<Date> {

public DateDeserializer() {
public DateFormatDeserializer() {
super();
}

Expand Down
@@ -0,0 +1,36 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api.impl.json;

import java.io.IOException;
import java.util.Date;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

public class DateInSecondsDeserializer extends JsonDeserializer<Date> {

private static final int MILLISECONDS = 1000;

@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return new Date(jp.getLongValue() * MILLISECONDS);
}

}
Expand Up @@ -36,7 +36,7 @@ class RateLimitStatusMixin {
@JsonProperty("remaining_user_hits")
int remainingUserHits;
@JsonProperty("reset_time")
@JsonDeserialize(using = DateDeserializer.class)
@JsonDeserialize(using = DateFormatDeserializer.class)
Date resetTime;
@JsonProperty("reset_time_in_seconds")
int resetTimeInSeconds;
Expand Down
Expand Up @@ -15,7 +15,7 @@
*/
package org.springframework.social.weibo.api.impl.json;

class TimelineDateDeserializer extends DateDeserializer {
class TimelineDateDeserializer extends DateFormatDeserializer {

private static final String TIMELINE_DATE_FORMAT = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";

Expand Down
Expand Up @@ -61,8 +61,7 @@ public int compare(Trends o1, Trends o2) {
public SortedSet<Trends> deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException,
JsonProcessingException {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
dateFormat.setLenient(true);
TreeSet<Trends> result = new TreeSet<Trends>(comparator);
for (Iterator<Entry<String, JsonNode>> iterator = jp.readValueAsTree()
Expand All @@ -71,18 +70,18 @@ public SortedSet<Trends> deserialize(JsonParser jp,
Trends trends = new Trends();
try {
trends.setDate(dateFormat.parse(next.getKey()));
JsonNode trendsNode = next.getValue();
for (Iterator<JsonNode> iterator2 = trendsNode.getElements(); iterator2
.hasNext();) {
JsonParser nodeParser = iterator2.next().traverse();
nodeParser.setCodec(jp.getCodec());
Trend readValueAs = nodeParser.readValueAs(Trend.class);
trends.getTrends().add(readValueAs);
}
result.add(trends);
} catch (ParseException e) {
logger.warn("Unable to parse date", e);
}
JsonNode trendsNode = next.getValue();
for (Iterator<JsonNode> iterator2 = trendsNode.getElements(); iterator2
.hasNext();) {
JsonParser nodeParser = iterator2.next().traverse();
nodeParser.setCodec(jp.getCodec());
Trend readValueAs = nodeParser.readValueAs(Trend.class);
trends.getTrends().add(readValueAs);
}
result.add(trends);
}
return result;
}
Expand Down
Expand Up @@ -36,6 +36,7 @@ abstract class TrendsWrapperMixin {
SortedSet<Trend> trends;

@JsonProperty("as_of")
@JsonDeserialize(using = DateInSecondsDeserializer.class)
Date asOf;

}
Expand Up @@ -108,7 +108,7 @@ public void testGetHourlyTrends() {
SortedSet<Trends> trendsSet = hourlyTrends.getTrends();
assertEquals(2, trendsSet.size());
Trends trends = trendsSet.iterator().next();
assertEquals(1306809992000L, trends.getDate().getTime());
assertEquals(1306809960000L, trends.getDate().getTime());
Trend firstTrend = trends.getTrends().iterator().next();
assertEquals(123, firstTrend.getAmount());
assertEquals(0, firstTrend.getDelta());
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/json/hourlyTrends.json
Expand Up @@ -23,5 +23,5 @@
}
]
},
"as_of": 1280833537000
"as_of": 1280833537
}

0 comments on commit 705d9b7

Please sign in to comment.