*/
<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
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();
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")) {
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;
}
--- /dev/null
+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"));
+ }
+
+}