Created
May 31, 2011 15:54
-
-
Save vvakame/1000751 to your computer and use it in GitHub Desktop.
ReadItLater Retreive a User's List
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
package net.vvakame.sample.duma; | |
import net.vvakame.util.jsonpullparser.annotation.JsonKey; | |
import net.vvakame.util.jsonpullparser.annotation.JsonModel; | |
/** | |
* へっへぇーいい!! | |
* @author vvakame | |
*/ | |
@JsonModel(treatUnknownKeyAsError = true, decamelize = true) | |
public class Item { | |
@JsonKey | |
String itemId; | |
@JsonKey | |
String url; | |
@JsonKey | |
String title; | |
@JsonKey | |
String timeUpdated; | |
@JsonKey | |
String timeAdded; | |
@JsonKey | |
String tags; | |
@JsonKey | |
String state; | |
/** | |
* @return the itemId | |
* @category accessor | |
*/ | |
public String getItemId() { | |
return itemId; | |
} | |
/** | |
* @param itemId the itemId to set | |
* @category accessor | |
*/ | |
public void setItemId(String itemId) { | |
this.itemId = itemId; | |
} | |
/** | |
* @return the url | |
* @category accessor | |
*/ | |
public String getUrl() { | |
return url; | |
} | |
/** | |
* @param url the url to set | |
* @category accessor | |
*/ | |
public void setUrl(String url) { | |
this.url = url; | |
} | |
/** | |
* @return the title | |
* @category accessor | |
*/ | |
public String getTitle() { | |
return title; | |
} | |
/** | |
* @param title the title to set | |
* @category accessor | |
*/ | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
/** | |
* @return the timeUpdated | |
* @category accessor | |
*/ | |
public String getTimeUpdated() { | |
return timeUpdated; | |
} | |
/** | |
* @param timeUpdated the timeUpdated to set | |
* @category accessor | |
*/ | |
public void setTimeUpdated(String timeUpdated) { | |
this.timeUpdated = timeUpdated; | |
} | |
/** | |
* @return the timeAdded | |
* @category accessor | |
*/ | |
public String getTimeAdded() { | |
return timeAdded; | |
} | |
/** | |
* @param timeAdded the timeAdded to set | |
* @category accessor | |
*/ | |
public void setTimeAdded(String timeAdded) { | |
this.timeAdded = timeAdded; | |
} | |
/** | |
* @return the tags | |
* @category accessor | |
*/ | |
public String getTags() { | |
return tags; | |
} | |
/** | |
* @param tags the tags to set | |
* @category accessor | |
*/ | |
public void setTags(String tags) { | |
this.tags = tags; | |
} | |
/** | |
* @return the state | |
* @category accessor | |
*/ | |
public String getState() { | |
return state; | |
} | |
/** | |
* @param state the state to set | |
* @category accessor | |
*/ | |
public void setState(String state) { | |
this.state = state; | |
} | |
} |
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
package net.vvakame.sample.duma; | |
import java.io.IOException; | |
import java.io.Writer; | |
import java.util.HashMap; | |
import java.util.Map; | |
import net.vvakame.util.jsonpullparser.JsonFormatException; | |
import net.vvakame.util.jsonpullparser.JsonPullParser; | |
import net.vvakame.util.jsonpullparser.JsonPullParser.State; | |
import net.vvakame.util.jsonpullparser.util.JsonUtil; | |
import net.vvakame.util.jsonpullparser.util.OnJsonObjectAddListener; | |
import net.vvakame.util.jsonpullparser.util.TokenConverter; | |
import static net.vvakame.util.jsonpullparser.util.JsonUtil.*; | |
/** | |
* へっへぇーいい!! | |
* @author vvakame | |
*/ | |
public class ItemMapConverter extends TokenConverter<Map<String, Item>> { | |
static ItemMapConverter conv = null; | |
/** | |
* へっちょーん!!! | |
* @return へちょーん? | |
* @author vvakame | |
*/ | |
public static ItemMapConverter getInstance() { | |
if (conv == null) { | |
conv = new ItemMapConverter(); | |
} | |
return conv; | |
} | |
@Override | |
public Map<String, Item> parse(JsonPullParser parser, OnJsonObjectAddListener listener) | |
throws IOException, JsonFormatException { | |
State state = parser.lookAhead(); | |
if (state == State.VALUE_NULL) { | |
return null; | |
} | |
Map<String, Item> resultMap = new HashMap<String, Item>(); | |
state = parser.getEventType(); | |
if (state != State.START_HASH) { | |
throw new JsonFormatException("expected state is START_HASH, but get=" + state); | |
} | |
while ((state = parser.lookAhead()) != State.END_HASH) { | |
state = parser.getEventType(); | |
if (state != State.KEY) { | |
throw new JsonFormatException("expected state is VALUE_STRING, but get=" + state); | |
} | |
String key = parser.getValueString(); | |
Item item = ItemGenerated.get(parser, listener); | |
resultMap.put(key, item); | |
} | |
parser.getEventType(); | |
return resultMap; | |
} | |
@Override | |
public void encodeNullToNull(Writer writer, Map<String, Item> obj) throws IOException { | |
if (obj == null) { | |
writer.write("null"); | |
return; | |
} | |
startHash(writer); | |
int size = obj.size(); | |
int i = 0; | |
for (String key : obj.keySet()) { | |
Item item = obj.get(key); | |
JsonUtil.putKey(writer, key); | |
ItemGenerated.encode(writer, item); | |
if (i + 1 < size) { | |
addSeparator(writer); | |
} | |
i++; | |
} | |
endHash(writer); | |
} | |
} |
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
package net.vvakame.sample.duma; | |
import java.util.Map; | |
import net.vvakame.util.jsonpullparser.annotation.JsonKey; | |
import net.vvakame.util.jsonpullparser.annotation.JsonModel; | |
/** | |
* へっへぇーいい!! | |
* @author vvakame | |
*/ | |
@JsonModel(treatUnknownKeyAsError = true, decamelize = true) | |
public class ReadItLater { | |
@JsonKey | |
String status; | |
@JsonKey | |
String since; | |
@JsonKey(converter = ItemMapConverter.class) | |
Map<String, Item> list; | |
/** | |
* @return the status | |
* @category accessor | |
*/ | |
public String getStatus() { | |
return status; | |
} | |
/** | |
* @param status the status to set | |
* @category accessor | |
*/ | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
/** | |
* @return the since | |
* @category accessor | |
*/ | |
public String getSince() { | |
return since; | |
} | |
/** | |
* @param since the since to set | |
* @category accessor | |
*/ | |
public void setSince(String since) { | |
this.since = since; | |
} | |
/** | |
* @return the list | |
* @category accessor | |
*/ | |
public Map<String, Item> getList() { | |
return list; | |
} | |
/** | |
* @param list the list to set | |
* @category accessor | |
*/ | |
public void setList(Map<String, Item> list) { | |
this.list = list; | |
} | |
} |
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
package net.vvakame.sample.duma; | |
import java.io.IOException; | |
import java.io.StringWriter; | |
import net.vvakame.util.jsonpullparser.JsonFormatException; | |
import org.junit.Test; | |
import static org.hamcrest.CoreMatchers.*; | |
import static org.junit.Assert.*; | |
/** | |
* へぷちゃーい!! | |
* @author vvakame | |
*/ | |
public class ReadItLaterTest { | |
/** | |
* テスト | |
* @throws JsonFormatException | |
* @throws IOException | |
*/ | |
@Test | |
public void hoge() throws IOException, JsonFormatException { | |
String json = | |
"{\"status\":\"1\",\"since\":\"1245626956\",\"list\":{\"93817\":{\"item_id\":\"93817\",\"url\":\"http://url.com\",\"title\":\"Page Title\",\"time_updated\":\"1245626956\",\"time_added\":\"1245626956\",\"tags\":\"comma,seperated,list\",\"state\":\"0\"},\"935812\":{\"item_id\":\"935812\",\"url\":\"http://google.com\",\"title\":\"Google\",\"time_updated\":\"1245626956\",\"time_added\":\"1245626956\",\"tags\":\"comma,seperated,list\",\"state\":\"1\"}}}"; | |
// JSON→POJO | |
ReadItLater later = ReadItLaterGenerated.get(json); | |
assertThat(later.getStatus(), is("1")); | |
assertThat(later.getSince(), is("1245626956")); | |
assertThat(later.getList().size(), is(2)); | |
Item item; | |
item = later.getList().get("93817"); | |
assertThat(item.getItemId(), is("93817")); | |
assertThat(item.getUrl(), is("http://url.com")); | |
assertThat(item.getTitle(), is("Page Title")); | |
assertThat(item.getTimeUpdated(), is("1245626956")); | |
assertThat(item.getTimeAdded(), is("1245626956")); | |
assertThat(item.getTags(), is("comma,seperated,list")); | |
assertThat(item.getState(), is("0")); | |
item = later.getList().get("935812"); | |
assertThat(item.getItemId(), is("935812")); | |
assertThat(item.getUrl(), is("http://google.com")); | |
assertThat(item.getTitle(), is("Google")); | |
assertThat(item.getTimeUpdated(), is("1245626956")); | |
assertThat(item.getTimeAdded(), is("1245626956")); | |
assertThat(item.getTags(), is("comma,seperated,list")); | |
assertThat(item.getState(), is("1")); | |
// POJO→JSON | |
// めんどいからもっかい復号しておkだったらおk | |
StringWriter writer = new StringWriter(); | |
ReadItLaterGenerated.encode(writer, later); | |
json = writer.toString(); | |
// JSON→POJO | |
later = ReadItLaterGenerated.get(json); | |
assertThat(later.getStatus(), is("1")); | |
assertThat(later.getSince(), is("1245626956")); | |
assertThat(later.getList().size(), is(2)); | |
item = later.getList().get("93817"); | |
assertThat(item.getItemId(), is("93817")); | |
assertThat(item.getUrl(), is("http://url.com")); | |
assertThat(item.getTitle(), is("Page Title")); | |
assertThat(item.getTimeUpdated(), is("1245626956")); | |
assertThat(item.getTimeAdded(), is("1245626956")); | |
assertThat(item.getTags(), is("comma,seperated,list")); | |
assertThat(item.getState(), is("0")); | |
item = later.getList().get("935812"); | |
assertThat(item.getItemId(), is("935812")); | |
assertThat(item.getUrl(), is("http://google.com")); | |
assertThat(item.getTitle(), is("Google")); | |
assertThat(item.getTimeUpdated(), is("1245626956")); | |
assertThat(item.getTimeAdded(), is("1245626956")); | |
assertThat(item.getTags(), is("comma,seperated,list")); | |
assertThat(item.getState(), is("1")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment