From 705d9b701c3ee1b5a3d4b3ae95ceebe53cb03ac3 Mon Sep 17 00:00:00 2001 From: Denis Vergnes Date: Wed, 21 Mar 2012 09:41:31 +0800 Subject: [PATCH] Fix issues on get hourly trends feature --- ...lizer.java => DateFormatDeserializer.java} | 4 +-- .../impl/json/DateInSecondsDeserializer.java | 36 +++++++++++++++++++ .../api/impl/json/RateLimitStatusMixin.java | 2 +- .../impl/json/TimelineDateDeserializer.java | 2 +- .../api/impl/json/TrendsDeserializer.java | 21 ++++++----- .../api/impl/json/TrendsWrapperMixin.java | 1 + .../weibo/api/impl/TrendTemplateTest.java | 2 +- src/test/resources/json/hourlyTrends.json | 2 +- 8 files changed, 53 insertions(+), 17 deletions(-) rename src/main/java/org/springframework/social/weibo/api/impl/json/{DateDeserializer.java => DateFormatDeserializer.java} (90%) create mode 100644 src/main/java/org/springframework/social/weibo/api/impl/json/DateInSecondsDeserializer.java diff --git a/src/main/java/org/springframework/social/weibo/api/impl/json/DateDeserializer.java b/src/main/java/org/springframework/social/weibo/api/impl/json/DateFormatDeserializer.java similarity index 90% rename from src/main/java/org/springframework/social/weibo/api/impl/json/DateDeserializer.java rename to src/main/java/org/springframework/social/weibo/api/impl/json/DateFormatDeserializer.java index 2af3646..03006fc 100644 --- a/src/main/java/org/springframework/social/weibo/api/impl/json/DateDeserializer.java +++ b/src/main/java/org/springframework/social/weibo/api/impl/json/DateFormatDeserializer.java @@ -26,9 +26,9 @@ import org.codehaus.jackson.map.DeserializationContext; import org.codehaus.jackson.map.JsonDeserializer; -public class DateDeserializer extends JsonDeserializer { +public class DateFormatDeserializer extends JsonDeserializer { - public DateDeserializer() { + public DateFormatDeserializer() { super(); } diff --git a/src/main/java/org/springframework/social/weibo/api/impl/json/DateInSecondsDeserializer.java b/src/main/java/org/springframework/social/weibo/api/impl/json/DateInSecondsDeserializer.java new file mode 100644 index 0000000..3b34d18 --- /dev/null +++ b/src/main/java/org/springframework/social/weibo/api/impl/json/DateInSecondsDeserializer.java @@ -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 { + + private static final int MILLISECONDS = 1000; + + @Override + public Date deserialize(JsonParser jp, DeserializationContext ctxt) + throws IOException, JsonProcessingException { + return new Date(jp.getLongValue() * MILLISECONDS); + } + +} diff --git a/src/main/java/org/springframework/social/weibo/api/impl/json/RateLimitStatusMixin.java b/src/main/java/org/springframework/social/weibo/api/impl/json/RateLimitStatusMixin.java index 9a6dc34..6275d53 100644 --- a/src/main/java/org/springframework/social/weibo/api/impl/json/RateLimitStatusMixin.java +++ b/src/main/java/org/springframework/social/weibo/api/impl/json/RateLimitStatusMixin.java @@ -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; diff --git a/src/main/java/org/springframework/social/weibo/api/impl/json/TimelineDateDeserializer.java b/src/main/java/org/springframework/social/weibo/api/impl/json/TimelineDateDeserializer.java index c10fdcd..a486529 100644 --- a/src/main/java/org/springframework/social/weibo/api/impl/json/TimelineDateDeserializer.java +++ b/src/main/java/org/springframework/social/weibo/api/impl/json/TimelineDateDeserializer.java @@ -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"; diff --git a/src/main/java/org/springframework/social/weibo/api/impl/json/TrendsDeserializer.java b/src/main/java/org/springframework/social/weibo/api/impl/json/TrendsDeserializer.java index 3c14342..3f66de9 100644 --- a/src/main/java/org/springframework/social/weibo/api/impl/json/TrendsDeserializer.java +++ b/src/main/java/org/springframework/social/weibo/api/impl/json/TrendsDeserializer.java @@ -61,8 +61,7 @@ public int compare(Trends o1, Trends o2) { public SortedSet 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 result = new TreeSet(comparator); for (Iterator> iterator = jp.readValueAsTree() @@ -71,18 +70,18 @@ public SortedSet deserialize(JsonParser jp, Trends trends = new Trends(); try { trends.setDate(dateFormat.parse(next.getKey())); + JsonNode trendsNode = next.getValue(); + for (Iterator 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 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; } diff --git a/src/main/java/org/springframework/social/weibo/api/impl/json/TrendsWrapperMixin.java b/src/main/java/org/springframework/social/weibo/api/impl/json/TrendsWrapperMixin.java index bc41149..2ff924f 100644 --- a/src/main/java/org/springframework/social/weibo/api/impl/json/TrendsWrapperMixin.java +++ b/src/main/java/org/springframework/social/weibo/api/impl/json/TrendsWrapperMixin.java @@ -36,6 +36,7 @@ abstract class TrendsWrapperMixin { SortedSet trends; @JsonProperty("as_of") + @JsonDeserialize(using = DateInSecondsDeserializer.class) Date asOf; } diff --git a/src/test/java/org/springframework/social/weibo/api/impl/TrendTemplateTest.java b/src/test/java/org/springframework/social/weibo/api/impl/TrendTemplateTest.java index 571d421..f4560a9 100644 --- a/src/test/java/org/springframework/social/weibo/api/impl/TrendTemplateTest.java +++ b/src/test/java/org/springframework/social/weibo/api/impl/TrendTemplateTest.java @@ -108,7 +108,7 @@ public void testGetHourlyTrends() { SortedSet 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()); diff --git a/src/test/resources/json/hourlyTrends.json b/src/test/resources/json/hourlyTrends.json index 88f0281..9c3b328 100644 --- a/src/test/resources/json/hourlyTrends.json +++ b/src/test/resources/json/hourlyTrends.json @@ -23,5 +23,5 @@ } ] }, - "as_of": 1280833537000 + "as_of": 1280833537 } \ No newline at end of file