From ea1d26c2ffc9d82a515b41636abd8b2da3af61b1 Mon Sep 17 00:00:00 2001 From: Adolfo Panizo Date: Wed, 24 Dec 2014 11:58:39 +0100 Subject: Parsing only methods specified in JsonBuilder interface from a given json. Signed-off-by: Manolo Carrasco --- .../gwt/query/client/builders/JsonBuilder.java | 6 +++ .../com/google/gwt/query/vm/JsonFactoryJre.java | 22 ++++++++++- .../google/gwt/query/vm/JsonFactoryParseTest.java | 46 ++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java 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 @@ -30,6 +30,12 @@ public interface JsonBuilder extends IsProperties { */ 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 parseStrict(String json, Class clz); + /** * Returns the wrapped object, normally a Properties jso in client * but can be used to return the underlying Json implementation in JVM 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 clz = (Class) args[1]; + Class proxie = (Class) 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 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")); + } + +} -- cgit v1.2.3