aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdolfo Panizo <adolfo.panizo@gmail.com>2014-12-24 16:23:39 +0100
committerManolo Carrasco <manolo@apache.org>2014-12-25 15:00:11 +0100
commitb0f52ef82a0e432eeab694add4924b21d103bdd6 (patch)
tree17fea64ca26d3cef9175a6bc766d9826102f609a
parentea1d26c2ffc9d82a515b41636abd8b2da3af61b1 (diff)
downloadgwtquery-b0f52ef82a0e432eeab694add4924b21d103bdd6.tar.gz
gwtquery-b0f52ef82a0e432eeab694add4924b21d103bdd6.zip
Implementing strip() on isProperties
Signed-off-by: Manolo Carrasco <manolo@apache.org>
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/IsProperties.java7
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java6
-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/client/builders/JsonBuilderBase.java9
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java37
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java46
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java16
7 files changed, 101 insertions, 26 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/IsProperties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/IsProperties.java
index bcd79cdb..3b88edf5 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/IsProperties.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/IsProperties.java
@@ -31,6 +31,12 @@ public interface IsProperties {
* parses a json string and loads the resulting properties object.
*/
<T extends IsProperties> T parse(String json);
+
+ /**
+ * Removes the extra JSON and leaves only the setters/getters described
+ * in the JsonBuilder interface.
+ */
+ <T extends IsProperties> T strip();
/**
* Returns the underlying object, normally a Properties jso in client
@@ -77,7 +83,6 @@ public interface IsProperties {
*/
String getJsonName();
-
/**
* converts a JsonBuilder instance into another JsonBuilder type but
* preserving the underlying data object.
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java
index db35f5d6..91259103 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java
@@ -219,6 +219,12 @@ public class Properties extends JavaScriptObject implements IsProperties {
return getDataImpl();
}
+ @SuppressWarnings("unchecked")
+ @Override
+ public final <J extends IsProperties> J strip() {
+ return getDataImpl();
+ }
+
public final <J extends IsProperties> J parse(String json) {
return load(JsUtils.parseJSON(json));
}
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 9dd3df78..b5f30451 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,12 +31,6 @@ 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/client/builders/JsonBuilderBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java
index c09836a5..bd5ad66e 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java
@@ -37,6 +37,15 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J
public J parse(String json, boolean fix) {
return fix ? parse(Properties.wrapPropertiesString(json)) : parse(json);
}
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public J strip() {
+ String[] methods = getFieldNames(); //EXCEPTION
+ String[] jsonMethods = p.getFieldNames(); // OK
+ System.out.println(methods);
+ return (J)this;
+ }
@SuppressWarnings("unchecked")
@Override
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 dfdb4cf2..634a483d 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
@@ -15,6 +15,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Date;
import java.util.List;
@@ -220,11 +221,10 @@ 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 ("strip".equals(mname)) {
+ List<String> keys = Arrays.asList(jsonObject.keys());
+ Class<?> type = proxy.getClass().getInterfaces()[0];
+ strip(keys, type.getMethods());
} else if (mname.matches("toString")) {
return jsonObject.toString();
} else if (mname.matches("toJsonWithName")) {
@@ -251,19 +251,28 @@ 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;
+ private void strip(List<String> keys, Method[] methods) {
+ for (String key: keys) {
+ boolean isInType = isInType(key, methods);
+ if (!isInType) {
+ jsonObject.remove(key);
}
- String attrJs = deCapitalize(m.getName().replaceFirst(regexGetOrSet, ""));
- Object value = jsonArg.get(attrJs);
- setValue(null, jsonObject, attrJs, value);
+ }
+ }
+
+ private boolean isInType(String key, Method[] methods) {
+ if (methods == null || methods.length == 0) {
+ return false;
}
+ for(Method m : methods) {
+ if (m.getName().toLowerCase().contains(key)) {
+ return true;
+ }
+ }
+ return false;
}
-
+
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/client/dbinding/DataBindingTestJre.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java
index 7158fe8d..be0a83f7 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java
@@ -29,6 +29,7 @@ import java.util.Arrays;
import java.util.Date;
import java.util.List;
+
/**
* Tests for Deferred which can run either in JVM and GWT
*/
@@ -152,4 +153,49 @@ public class DataBindingTestJre extends GWTTestCase {
assertEquals(1, c.<Number>get("a").intValue());
}
+
+ public interface GUser extends JsonBuilder{
+ int getAge();
+ void setAge(int age);
+
+ String getName();
+ void setName(String name);
+
+ GUser address(String address);
+ String address();
+ }
+
+ public static final String JSON_USER_EXAMPLE = " { " +
+ " 'email': 'foo@bar.com', " +
+ " 'age': 27, " +
+ " 'name': 'Foo Bar', " +
+ " 'address': 'Street Foo N6' " +
+ " }";
+
+ public void
+ test_parse_json() {
+ GUser entity = GQ.create(GUser.class);
+ entity.parse(JSON_USER_EXAMPLE, true);
+
+ assertEquals(27, entity.getAge());
+ assertEquals("Foo Bar", entity.getName());
+ assertEquals("Street Foo N6", entity.address());
+ assertTrue(entity.toJson().contains("email"));
+ }
+
+ public void
+ test_parse_strict_json() {
+ GUser entity = GQ.create(GUser.class);
+ entity.parse(JSON_USER_EXAMPLE, true);
+ for(String s: entity.getFieldNames()) {
+ System.out.println("Moe: "+s);
+ }
+
+ entity.strip();
+ System.out.println(entity.toJson());
+ assertEquals(27, entity.getAge());
+ assertEquals("Foo Bar", entity.getName());
+ assertEquals("Street Foo N6", entity.address());
+ assertFalse(entity.toJson().contains("email"));
+ }
} \ No newline at end of file
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
index 48272a61..b4ea332f 100644
--- 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
@@ -14,32 +14,38 @@ public class JsonFactoryParseTest {
String getName();
void setName(String name);
+
+ GUser address(String address);
+ String address();
}
public static final String JSON_USER_EXAMPLE = " { " +
" 'email': 'foo@bar.com', " +
" 'age': 27, " +
- " 'name': 'Foo Bar' " +
+ " 'name': 'Foo Bar', " +
+ " 'address': 'Street Foo N6' " +
" }";
@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.assertEquals("Street Foo N6", entity.address());
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);
-
+ entity.parse(JSON_USER_EXAMPLE);
+ entity.strip();
+ System.out.println(entity.toJson());
Assert.assertEquals(27, entity.getAge());
Assert.assertEquals("Foo Bar", entity.getName());
+ Assert.assertEquals("Street Foo N6", entity.address());
Assert.assertFalse(entity.toJson().contains("email"));
}