Permalink
Browse files

- template support

  • Loading branch information...
1 parent 59d7352 commit 6aa362840b29784851ed8ddd85c7e7154eb9fd42 @hamnis committed Aug 7, 2011
@@ -27,8 +27,6 @@
/**
* Parser for a vnd.collection+json document.
- *
- *
*/
public class JsonCollectionParser {
private final ObjectMapper mapper = new ObjectMapper();
@@ -59,8 +57,24 @@ private JsonCollection parseCollection(JsonNode collectionNode) {
ImmutableList<Link> links = parseLinks(collectionNode);
ImmutableList<Item> items = parseItems(collectionNode);
+ Template template = parseTemplate(collectionNode);
+
+ return new DefaultJsonCollection(href, version, links, items, template);
+ }
- return new DefaultJsonCollection(href, version, links, items, new Template());
+ private Template parseTemplate(JsonNode collectionNode) {
+ JsonNode node = collectionNode.get("template");
+ if (node != null) {
+ ImmutableList.Builder<Property> properties = ImmutableList.builder();
+ JsonNode data = node.get("data");
+ if (data != null) {
+ for (JsonNode dataNode : data) {
+ properties.add(toProperty(dataNode));
+ }
+ }
+ return new Template(properties.build());
+ }
+ return null;
}
private ErrorMessage parseError(JsonNode collectionNode) {
@@ -127,7 +141,7 @@ private Version getVersion(JsonNode collectionNode) {
Link link = new Link(
createURI(linkNode),
linkNode.get("rel").getTextValue(),
- prompt != null ? prompt.getTextValue() : null
+ prompt != null ? prompt.getTextValue() : null
);
linkBuilder.add(link);
}
@@ -16,20 +16,16 @@
package net.hamnaberg.json.parser;
-import com.google.common.collect.ImmutableList;
import net.hamnaberg.json.*;
-import net.hamnaberg.json.parser.JsonCollectionParser;
import org.junit.Assert;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
public class JsonCollectionParserTest {
private JsonCollectionParser parser;
@@ -78,4 +74,12 @@ public void parseErrorCollection() throws IOException {
assertEquals(URI.create("http://example.org/friends/"), collection.getHref());
assertNotNull("Error was null", collection.getError());
}
+
+ @Test
+ public void parseTemplateCollection() throws IOException {
+ JsonCollection collection = parser.parse(new InputStreamReader(getClass().getResourceAsStream("/template.json")));
+ assertNotNull(collection);
+ assertEquals(URI.create("http://example.org/friends/"), collection.getHref());
+ assertNotNull("Template was null", collection.getTemplate());
+ }
}

0 comments on commit 6aa3628

Please sign in to comment.