You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2011/09/23 20:51:46 UTC
svn commit: r1174953 - 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/
Author: danhaywood
Date: Fri Sep 23 18:51:45 2011
New Revision: 1174953
URL: http://svn.apache.org/viewvc?rev=1174953&view=rev
Log:
ISIS-109: more on json applib JsonRepresentation methods
Modified:
incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java
incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getBigInteger.java
incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getBoolean.java
incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getDouble.java
incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getInt.java
incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getLong.java
incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getNull_isNull.java
incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getString_isString.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=1174953&r1=1174952&r2=1174953&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 Sep 23 18:51:45 2011
@@ -137,14 +137,6 @@ public class JsonRepresentation {
}
- /////////////////////////////////////////////////////////////////////////
- // isArray, isMap, isValue, isNull
- /////////////////////////////////////////////////////////////////////////
-
- public boolean isArray() {
- return jsonNode.isArray();
- }
-
/**
* Node is a value (nb: could be {@link #isNull() null}).
*/
@@ -152,13 +144,6 @@ public class JsonRepresentation {
return jsonNode.isValueNode();
}
- public boolean isNull() {
- return jsonNode.isNull();
- }
-
- public boolean isMap() {
- return !isArray() && !isValue();
- }
@@ -176,15 +161,35 @@ public class JsonRepresentation {
}
/////////////////////////////////////////////////////////////////////////
- // getArray, isArray, asArray
+ // isArray, getArray, asArray
/////////////////////////////////////////////////////////////////////////
-
+
+ public boolean isArray(String path) {
+ return isArray(getNode(path));
+ }
+
+ public boolean isArray() {
+ return isArray(asJsonNode());
+ }
+
+ private boolean isArray(final JsonNode node) {
+ return !representsNull(node) && node.isArray();
+ }
+
+
public JsonRepresentation getArray(String path) {
- JsonNode node = getNode(path);
+ return getArray(path, getNode(path));
+ }
+
+ public JsonRepresentation asArray() {
+ return getArray(null, asJsonNode());
+ }
+
+ private JsonRepresentation getArray(String path, JsonNode node) {
if (representsNull(node)) {
return null;
}
- if (!node.isArray()) {
+ if (!isArray(node)) {
throw new IllegalArgumentException(formatExMsg(path, "is not an array"));
}
return new JsonRepresentation(node);
@@ -193,15 +198,34 @@ public class JsonRepresentation {
/////////////////////////////////////////////////////////////////////////
- // getMap, isMap, asMap
+ // isMap, getMap, asMap
/////////////////////////////////////////////////////////////////////////
-
+
+ public boolean isMap(String path) {
+ return isMap(getNode(path));
+ }
+
+ public boolean isMap() {
+ return isMap(asJsonNode());
+ }
+
+ private boolean isMap(final JsonNode node) {
+ return !representsNull(node) && !node.isArray() && !node.isValueNode();
+ }
+
public JsonRepresentation getMap(String path) {
- JsonNode node = getNode(path);
+ return getMap(path, getNode(path));
+ }
+
+ public JsonRepresentation asMap() {
+ return getMap(null, asJsonNode());
+ }
+
+ private JsonRepresentation getMap(String path, JsonNode node) {
if (representsNull(node)) {
return null;
}
- if (node.isArray() || node.isValueNode()) {
+ if (isArray(node) || node.isValueNode()) {
throw new IllegalArgumentException(formatExMsg(path, "is not a map"));
}
return new JsonRepresentation(node);
@@ -432,10 +456,9 @@ public class JsonRepresentation {
/////////////////////////////////////////////////////////////////////////
- // getLink, asLink, isLink
+ // isLink, getLink, asLink
/////////////////////////////////////////////////////////////////////////
-
public boolean isLink() {
return isLink(asJsonNode());
}
@@ -445,7 +468,7 @@ public class JsonRepresentation {
}
public boolean isLink(JsonNode node) {
- if (representsNull(node) || node.isArray() || node.isValueNode()) {
+ if (representsNull(node) || isArray(node) || node.isValueNode()) {
return false;
}
@@ -469,7 +492,7 @@ public class JsonRepresentation {
return null;
}
- if (node.isArray()) {
+ if (isArray(node)) {
throw new IllegalArgumentException(formatExMsg(path, "is an array that does not represent a link"));
}
if (node.isValueNode()) {
@@ -488,13 +511,21 @@ public class JsonRepresentation {
// getNull, isNull
/////////////////////////////////////////////////////////////////////////
+ public boolean isNull() {
+ return isNull(asJsonNode());
+ }
+
/**
* Indicates that the wrapped node has <tt>null</tt> value
* (ie {@link JsonRepresentation#isNull()}), or returns <tt>null</tt> if there was no node with the
* provided path.
*/
public Boolean isNull(String path) {
- JsonNode node = getNode(path);
+ return isNull(getNode(path));
+ }
+
+
+ private Boolean isNull(JsonNode node) {
if (node == null || node.isMissingNode()) {
// not exclude if node.isNull, cos that's the point of this.
return null;
@@ -508,14 +539,22 @@ public class JsonRepresentation {
* provided path.
*/
public JsonRepresentation getNull(String path) {
- JsonNode node = getNode(path);
+ return getNull(path, getNode(path));
+ }
+
+ public JsonRepresentation asNull() {
+ return getNull(null, asJsonNode());
+ }
+
+
+ private JsonRepresentation getNull(String path, JsonNode node) {
if (node == null || node.isMissingNode()) {
- // not exclude if node.isNull, cos that's the point of this.
+ // exclude if node.isNull, cos that's the point of this.
return null;
}
checkValue(path, node, "the null value");
if (!node.isNull()) {
- throw new IllegalArgumentException("'" + path + "' (" + node.toString() + ") is not the null value");
+ throw new IllegalArgumentException(formatExMsg(path, "is not the null value"));
}
return new JsonRepresentation(node);
}
@@ -538,6 +577,20 @@ public class JsonRepresentation {
return getLink(linkPropertyName);
}
+
+ /////////////////////////////////////////////////////////////////////////
+ // asInputStream
+ /////////////////////////////////////////////////////////////////////////
+
+ public InputStream asInputStream() {
+ return JsonNodeUtils.asInputStream(jsonNode);
+ }
+
+
+ /////////////////////////////////////////////////////////////////////////
+ // asArrayNode, asObjectNode
+ /////////////////////////////////////////////////////////////////////////
+
/**
* Convert underlying representation into an array.
*/
@@ -548,6 +601,7 @@ public class JsonRepresentation {
return (ArrayNode) asJsonNode();
}
+
/**
* Convert underlying representation into an object (map).
*/
@@ -559,15 +613,6 @@ public class JsonRepresentation {
}
-
- /////////////////////////////////////////////////////////////////////////
- // asInputStream
- /////////////////////////////////////////////////////////////////////////
-
- public InputStream asInputStream() {
- return JsonNodeUtils.asInputStream(jsonNode);
- }
-
/////////////////////////////////////////////////////////////////////////
// xpath support
/////////////////////////////////////////////////////////////////////////
@@ -898,6 +943,8 @@ public class JsonRepresentation {
}
+
+
/////////////////////////////////////////////////////////////////////////
// helpers
/////////////////////////////////////////////////////////////////////////
@@ -910,11 +957,7 @@ public class JsonRepresentation {
if (node.isValueNode()) {
return;
}
- if (node.isArray()) {
- throw new IllegalArgumentException("'" + path + "' (an array) is not " + requiredType);
- } else {
- throw new IllegalArgumentException("'" + path + "' (a map) is not " + requiredType);
- }
+ throw new IllegalArgumentException(formatExMsg(path, "is not " + requiredType));
}
private static boolean representsNull(JsonNode node) {
Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getBigInteger.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_getBigInteger.java?rev=1174953&r1=1174952&r2=1174953&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getBigInteger.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getBigInteger.java Fri Sep 23 18:51:45 2011
@@ -67,7 +67,7 @@ public class JsonRepresentationTest_getB
jsonRepresentation.getBigInteger("aSubMap");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubMap' (a map) is not a biginteger"));
+ assertThat(e.getMessage(), is("'aSubMap' is not a biginteger"));
}
}
@@ -77,7 +77,7 @@ public class JsonRepresentationTest_getB
jsonRepresentation.getBigInteger("aSubList");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubList' (an array) is not a biginteger"));
+ assertThat(e.getMessage(), is("'aSubList' is not a biginteger"));
}
}
Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getBoolean.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_getBoolean.java?rev=1174953&r1=1174952&r2=1174953&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getBoolean.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getBoolean.java Fri Sep 23 18:51:45 2011
@@ -66,7 +66,7 @@ public class JsonRepresentationTest_getB
jsonRepresentation.getBoolean("aSubMap");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubMap' (a map) is not a boolean"));
+ assertThat(e.getMessage(), is("'aSubMap' is not a boolean"));
}
}
@@ -76,7 +76,7 @@ public class JsonRepresentationTest_getB
jsonRepresentation.getBoolean("aSubList");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubList' (an array) is not a boolean"));
+ assertThat(e.getMessage(), is("'aSubList' is not a boolean"));
}
}
Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getDouble.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_getDouble.java?rev=1174953&r1=1174952&r2=1174953&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getDouble.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getDouble.java Fri Sep 23 18:51:45 2011
@@ -66,7 +66,7 @@ public class JsonRepresentationTest_getD
jsonRepresentation.getDouble("aSubMap");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubMap' (a map) is not a double"));
+ assertThat(e.getMessage(), is("'aSubMap' is not a double"));
}
}
@@ -76,7 +76,7 @@ public class JsonRepresentationTest_getD
jsonRepresentation.getDouble("aSubList");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubList' (an array) is not a double"));
+ assertThat(e.getMessage(), is("'aSubList' is not a double"));
}
}
Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getInt.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_getInt.java?rev=1174953&r1=1174952&r2=1174953&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getInt.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getInt.java Fri Sep 23 18:51:45 2011
@@ -66,7 +66,7 @@ public class JsonRepresentationTest_getI
jsonRepresentation.getInt("aSubMap");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubMap' (a map) is not an int"));
+ assertThat(e.getMessage(), is("'aSubMap' is not an int"));
}
}
@@ -76,7 +76,7 @@ public class JsonRepresentationTest_getI
jsonRepresentation.getInt("aSubList");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubList' (an array) is not an int"));
+ assertThat(e.getMessage(), is("'aSubList' is not an int"));
}
}
Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getLong.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_getLong.java?rev=1174953&r1=1174952&r2=1174953&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getLong.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getLong.java Fri Sep 23 18:51:45 2011
@@ -66,7 +66,7 @@ public class JsonRepresentationTest_getL
jsonRepresentation.getLong("aSubMap");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubMap' (a map) is not a long"));
+ assertThat(e.getMessage(), is("'aSubMap' is not a long"));
}
}
@@ -76,7 +76,7 @@ public class JsonRepresentationTest_getL
jsonRepresentation.getLong("aSubList");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubList' (an array) is not a long"));
+ assertThat(e.getMessage(), is("'aSubList' is not a long"));
}
}
Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getNull_isNull.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_getNull_isNull.java?rev=1174953&r1=1174952&r2=1174953&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getNull_isNull.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getNull_isNull.java Fri Sep 23 18:51:45 2011
@@ -63,7 +63,7 @@ public class JsonRepresentationTest_getN
jsonRepresentation.getNull("anEmptyArray");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'anEmptyArray' (an array) is not the null value"));
+ assertThat(e.getMessage(), is("'anEmptyArray' is not the null value"));
}
}
@@ -90,7 +90,7 @@ public class JsonRepresentationTest_getN
jsonRepresentation.getNull("aSubMap");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubMap' (a map) is not the null value"));
+ assertThat(e.getMessage(), is("'aSubMap' is not the null value"));
}
}
Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getString_isString.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_getString_isString.java?rev=1174953&r1=1174952&r2=1174953&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getString_isString.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getString_isString.java Fri Sep 23 18:51:45 2011
@@ -70,7 +70,7 @@ public class JsonRepresentationTest_getS
jsonRepresentation.getString("aSubMap");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubMap' (a map) is not a string"));
+ assertThat(e.getMessage(), is("'aSubMap' is not a string"));
}
}
@@ -81,7 +81,7 @@ public class JsonRepresentationTest_getS
jsonRepresentation.getString("aSubList");
fail();
} catch (IllegalArgumentException e) {
- assertThat(e.getMessage(), is("'aSubList' (an array) is not a string"));
+ assertThat(e.getMessage(), is("'aSubList' is not a string"));
}
}