aboutsummaryrefslogtreecommitdiffstats
path: root/gwtquery-core
diff options
context:
space:
mode:
authorAdolfo Panizo <adolfo.panizo@gmail.com>2014-12-24 11:58:39 +0100
committerManolo Carrasco <manolo@apache.org>2014-12-25 14:59:34 +0100
commitea1d26c2ffc9d82a515b41636abd8b2da3af61b1 (patch)
tree254b909e41fdeba38484804962cef0744ff31141 /gwtquery-core
parent45138b470ae618dbe986dc75bdbadae6ffbe991f (diff)
downloadgwtquery-ea1d26c2ffc9d82a515b41636abd8b2da3af61b1.tar.gz
gwtquery-ea1d26c2ffc9d82a515b41636abd8b2da3af61b1.zip
Parsing only methods specified in JsonBuilder interface from a given json.
Signed-off-by: Manolo Carrasco <manolo@apache.org>
Diffstat (limited to 'gwtquery-core')
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java6
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java22
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java46
3 files changed, 73 insertions, 1 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java
index b5f30451..9dd3df78 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java
@@ -31,6 +31,12 @@ public interface JsonBuilder extends IsProperties {
<J> J parse(String json, boolean fix);
/**
+ * Parses a json string and loads the resulting properties object.
+ * It will parse only the json's properties which are in clz.
+ */
+ <J> J parseStrict(String json, Class<J> clz);
+
+ /**
* Returns the wrapped object, normally a Properties jso in client
* but can be used to return the underlying Json implementation in JVM
* @deprecated use asObject() instead
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java
index 7bd8c792..dfdb4cf2 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java
@@ -199,8 +199,10 @@ public class JsonFactoryJre implements JsonFactory {
Class<?>[] classes = method.getParameterTypes();
int largs = classes.length;
+ String regexGetOrSet = "^[gs]et";
+
Name name = method.getAnnotation(Name.class);
- String attr = name != null ? name.value() : deCapitalize(mname.replaceFirst("^[gs]et", ""));
+ String attr = name != null ? name.value() : deCapitalize(mname.replaceFirst(regexGetOrSet, ""));
if ("getFieldNames".equals(mname)) {
return jsonObject.keys();
@@ -218,6 +220,11 @@ public class JsonFactoryJre implements JsonFactory {
json = Properties.wrapPropertiesString(json);
}
jsonObject = Json.parse(json);
+ } else if (largs > 1 && ("parseStrict".equals(mname))) {
+ Class<? extends JsonBuilder> clz = (Class<? extends JsonBuilder>) args[1];
+ Class<? extends JsonBuilder> proxie = (Class<? extends JsonBuilder>) Proxy.getProxyClass(clz.getClassLoader(), new Class[] {clz});
+ JsonObject jsonArg = Json.parse((String)args[0]);
+ parseStrict(proxie, jsonArg, regexGetOrSet);
} else if (mname.matches("toString")) {
return jsonObject.toString();
} else if (mname.matches("toJsonWithName")) {
@@ -244,6 +251,19 @@ public class JsonFactoryJre implements JsonFactory {
return null;
}
+ public void parseStrict(Class<? extends JsonBuilder> proxie, JsonObject jsonArg,
+ String regexGetOrSet) {
+ for(Method m: proxie.getMethods()) {
+ if (!m.getName().matches("^set.+")) {
+ continue;
+ }
+ String attrJs = deCapitalize(m.getName().replaceFirst(regexGetOrSet, ""));
+ Object value = jsonArg.get(attrJs);
+ setValue(null, jsonObject, attrJs, value);
+ }
+
+ }
+
private String deCapitalize(String s) {
return s != null && s.length() > 0 ? s.substring(0, 1).toLowerCase() + s.substring(1) : s;
}
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java
new file mode 100644
index 00000000..48272a61
--- /dev/null
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java
@@ -0,0 +1,46 @@
+package com.google.gwt.query.vm;
+
+import com.google.gwt.query.client.GQ;
+import com.google.gwt.query.client.builders.JsonBuilder;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class JsonFactoryParseTest {
+
+ public interface GUser extends JsonBuilder{
+ int getAge();
+ void setAge(int age);
+
+ String getName();
+ void setName(String name);
+ }
+
+ public static final String JSON_USER_EXAMPLE = " { " +
+ " 'email': 'foo@bar.com', " +
+ " 'age': 27, " +
+ " 'name': 'Foo Bar' " +
+ " }";
+
+ @Test public void
+ test_parse_json() {
+ GUser entity = GQ.create(GUser.class);
+ entity.parse(JSON_USER_EXAMPLE);
+ System.out.println(entity.toJson());
+
+ Assert.assertEquals(27, entity.getAge());
+ Assert.assertEquals("Foo Bar", entity.getName());
+ Assert.assertTrue(entity.toJson().contains("email"));
+ }
+
+ @Test public void
+ test_parse_strict_json() {
+ GUser entity = GQ.create(GUser.class);
+ entity.parseStrict(JSON_USER_EXAMPLE, GUser.class);
+
+ Assert.assertEquals(27, entity.getAge());
+ Assert.assertEquals("Foo Bar", entity.getName());
+ Assert.assertFalse(entity.toJson().contains("email"));
+ }
+
+}