incubator-isis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From danhayw...@apache.org
Subject svn commit: r1154153 - in /incubator/isis/trunk/framework/viewer/json/json-applib/src: main/java/org/apache/isis/viewer/json/applib/ test/java/org/apache/isis/viewer/json/applib/
Date Fri, 05 Aug 2011 09:59:38 GMT
Author: danhaywood
Date: Fri Aug  5 09:59:37 2011
New Revision: 1154153

URL: http://svn.apache.org/viewvc?rev=1154153&view=rev
Log:
ISIS-109: added JsonRepresentation#getRepresentation(String)

Added:
    incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getRepresentation.java
    incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_isArray.java
Modified:
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java?rev=1154153&r1=1154152&r2=1154153&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java
Fri Aug  5 09:59:37 2011
@@ -2,7 +2,6 @@ package org.apache.isis.viewer.json.appl
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 
 import net.sf.json.JSON;
 import net.sf.json.JSONSerializer;
@@ -23,11 +22,9 @@ import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.JsonParseException;
 import org.codehaus.jackson.map.JsonMappingException;
 
-import com.google.common.base.Charsets;
-
 /**
  * A wrapper around {@link JsonNode} that provides some additional helper
- * methods.
+ * methods, including searching using xpath (requires optional XOM dependency).
  */
 public class JsonRepresentation {
 
@@ -71,20 +68,18 @@ public class JsonRepresentation {
         }
     }
 
+    public JsonRepresentation getRepresentation(String key) {
+        JsonNode subNode = jsonNode.get(key);
+        // TODO: extra checking here required
+
+        return as(subNode, JsonRepresentation.class);
+    }
+
     public Link getLink(String key) throws JsonMappingException {
         JsonNode subNode = jsonNode.get(key);
         // TODO: extra checking here required
 
-        try {
-            // TODO: review, rather heavyweight
-            return JsonMapper.instance().read(subNode.toString(), Link.class);
-        } catch (JsonParseException e) {
-            // shouldn't happen
-            throw new RuntimeException(e);
-        } catch (IOException e) {
-            // shouldn't happen
-            throw new RuntimeException(e);
-        }
+        return as(subNode, Link.class);
     }
 
     /**
@@ -153,6 +148,21 @@ public class JsonRepresentation {
     public String toString() {
         return jsonNode.toString();
     }
-    
+
+
+    private static <T> T as(JsonNode subNode, Class<T> requiredType) {
+        try {
+            // TODO: review, rather heavyweight
+            return JsonMapper.instance().read(subNode.toString(), requiredType);
+        } catch (JsonParseException e) {
+            // shouldn't happen
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            // shouldn't happen
+            throw new RuntimeException(e);
+        }
+    }
+
+
 
 }

Added: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getRepresentation.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getRepresentation.java?rev=1154153&view=auto
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getRepresentation.java
(added)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getRepresentation.java
Fri Aug  5 09:59:37 2011
@@ -0,0 +1,22 @@
+package org.apache.isis.viewer.json.applib;
+
+import static org.apache.isis.viewer.json.applib.JsonUtils.readJson;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+import java.io.IOException;
+
+import org.codehaus.jackson.JsonParseException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.junit.Test;
+
+public class JsonRepresentationTest_getRepresentation {
+
+    @Test
+    public void getRepresentation_forMap() throws JsonParseException, JsonMappingException,
IOException {
+        JsonRepresentation jsonRepresentation = new JsonRepresentation(readJson("map.json"));
+        JsonRepresentation subRepresentation = jsonRepresentation.getRepresentation("aLink");
+        assertThat(subRepresentation.getString("rel"), is("someRel"));
+    }
+    
+}

Added: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_isArray.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_isArray.java?rev=1154153&view=auto
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_isArray.java
(added)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_isArray.java
Fri Aug  5 09:59:37 2011
@@ -0,0 +1,32 @@
+package org.apache.isis.viewer.json.applib;
+
+import static org.apache.isis.viewer.json.applib.JsonUtils.readJson;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.io.IOException;
+
+import org.codehaus.jackson.JsonParseException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.junit.Before;
+import org.junit.Test;
+
+public class JsonRepresentationTest_isArray {
+
+    @Before
+    public void setUp() throws Exception {
+    }
+
+    @Test
+    public void isArray_forList() throws JsonParseException, JsonMappingException, IOException
{
+        JsonRepresentation jsonRepresentation = new JsonRepresentation(readJson("list.json"));
+        assertThat(jsonRepresentation.isArray(), is(true));
+    }
+
+    @Test
+    public void isArray_forMap() throws JsonParseException, JsonMappingException, IOException
{
+        JsonRepresentation jsonRepresentation = new JsonRepresentation(readJson("map.json"));
+        assertThat(jsonRepresentation.isArray(), is(false));
+    }
+
+}



Mime
View raw message