Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from twitsprout/kryo
Browse files Browse the repository at this point in the history
TFJ-648 Kryo serialization support
  • Loading branch information
yusuke committed Jan 1, 2012
2 parents aeb81a3 + 06789c7 commit 124fd09
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 0 deletions.
6 changes: 6 additions & 0 deletions twitter4j-core/pom.xml
Expand Up @@ -99,6 +99,12 @@
<version>3.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode</groupId>
<artifactId>kryo</artifactId>
<version>1.04</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/GeoLocation.java
Expand Up @@ -37,6 +37,11 @@ public GeoLocation(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}

/* For serialization purposes only. */
/* package */ GeoLocation() {

}

/**
* returns the latitude of the geo location
Expand Down
Expand Up @@ -39,6 +39,11 @@
super();
init(json);
}

/* For serialization purposes only. */
/* package */ HashtagEntityJSONImpl() {

}

private void init(JSONObject json) throws TwitterException {
try {
Expand Down
Expand Up @@ -88,6 +88,11 @@ public MediaEntityJSONImpl(JSONObject json) throws TwitterException {
}

}

/* For serialization purposes only. */
/* package */ MediaEntityJSONImpl() {

}

/**
* {@inheritDoc}
Expand Down
Expand Up @@ -70,6 +70,11 @@ final class PlaceJSONImpl extends TwitterResponseImpl implements Place, java.io.
super();
init(json);
}

/* For serialization purposes only. */
PlaceJSONImpl() {

}

private void init(JSONObject json) throws TwitterException {
try {
Expand Down
Expand Up @@ -80,6 +80,11 @@
super();
init(json);
}

/* Only for serialization purposes. */
/*package*/ StatusJSONImpl() {

}

private void init(JSONObject json) throws TwitterException {
id = getLong("id", json);
Expand Down
Expand Up @@ -45,6 +45,11 @@
super();
init(json);
}

/* For serialization purposes only. */
/* package */ URLEntityJSONImpl() {

}

private void init(JSONObject json) throws TwitterException {
try {
Expand Down
Expand Up @@ -98,6 +98,11 @@
super();
init(json);
}

/* Only for serialization purposes. */
/*package*/UserJSONImpl() {

}

private void init(JSONObject json) throws TwitterException {
try {
Expand Down
Expand Up @@ -41,6 +41,11 @@
super();
init(json);
}

/* For serialization purposes only. */
/* package */ UserMentionEntityJSONImpl() {

}

private void init(JSONObject json) throws TwitterException {
try {
Expand Down
@@ -0,0 +1,45 @@
package twitter4j.internal;

import java.nio.ByteBuffer;

import twitter4j.Status;
import twitter4j.TwitterException;
import twitter4j.json.DataObjectFactory;

import com.esotericsoftware.kryo.Kryo;

import junit.framework.TestCase;

public class KryoSerializationTest extends TestCase {
private final static String TEST_STATUS_JSON = "{\"text\":\"\\\\u5e30%u5e30 &lt;%}& foobar &lt;&Cynthia&gt;\",\"contributors\":null,\"geo\":null,\"retweeted\":false,\"in_reply_to_screen_name\":null,\"truncated\":false,\"entities\":{\"urls\":[],\"hashtags\":[],\"user_mentions\":[]},\"in_reply_to_status_id_str\":null,\"id\":12029015787307008,\"in_reply_to_user_id_str\":null,\"source\":\"web\",\"favorited\":false,\"in_reply_to_status_id\":null,\"in_reply_to_user_id\":null,\"created_at\":\"Tue Dec 07 06:21:55 +0000 2010\",\"retweet_count\":0,\"id_str\":\"12029015787307008\",\"place\":null,\"user\":{\"location\":\"location:\",\"statuses_count\":13405,\"profile_background_tile\":false,\"lang\":\"en\",\"profile_link_color\":\"0000ff\",\"id\":6358482,\"following\":true,\"favourites_count\":2,\"protected\":false,\"profile_text_color\":\"000000\",\"contributors_enabled\":false,\"description\":\"Hi there, I do test a lot!new\",\"verified\":false,\"profile_sidebar_border_color\":\"87bc44\",\"name\":\"twit4j\",\"profile_background_color\":\"9ae4e8\",\"created_at\":\"Sun May 27 09:52:09 +0000 2007\",\"followers_count\":24,\"geo_enabled\":true,\"profile_background_image_url\":\"http://a3.twimg.com/profile_background_images/179009017/t4j-reverse.gif\",\"follow_request_sent\":false,\"url\":\"http://yusuke.homeip.net/twitter4j/\",\"utc_offset\":-32400,\"time_zone\":\"Alaska\",\"notifications\":false,\"friends_count\":4,\"profile_use_background_image\":true,\"profile_sidebar_fill_color\":\"e0ff92\",\"screen_name\":\"twit4j\",\"id_str\":\"6358482\",\"profile_image_url\":\"http://a3.twimg.com/profile_images/1184543043/t4j-reverse_normal.jpeg\",\"show_all_inline_media\":false,\"listed_count\":3},\"coordinates\":null}";

private Kryo kryo;

protected void setUp() throws Exception {
super.setUp();

kryo = new Kryo();
kryo.register(java.lang.String[].class);
kryo.register(long[].class);
kryo.register(java.util.Date.class);
kryo.register(twitter4j.HashtagEntity[].class);
kryo.register(twitter4j.URLEntity[].class);
kryo.register(twitter4j.UserMentionEntity[].class);
kryo.register(Class.forName("twitter4j.internal.json.UserJSONImpl"));
kryo.register(Class.forName("twitter4j.internal.json.StatusJSONImpl"));
}

public void testKryoSerialization() throws TwitterException, ClassNotFoundException {
Status status;
status = DataObjectFactory.createStatus(TEST_STATUS_JSON);

ByteBuffer buffer = ByteBuffer.allocate(512);
kryo.writeObject(buffer, status);
System.out.println(buffer.position() + " vs. " + TEST_STATUS_JSON.length());
buffer.rewind();

Status deserializedStatus = (Status) kryo.readObject(buffer, Class.forName("twitter4j.internal.json.StatusJSONImpl"));
assertNotNull(deserializedStatus);
assertEquals(status, deserializedStatus);
}
}

0 comments on commit 124fd09

Please sign in to comment.