* 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
*/
String getJsonName();
-
/**
* converts a JsonBuilder instance into another JsonBuilder type but
* preserving the underlying data object.
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));
}
*/
<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
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
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;
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")) {
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;
}
import java.util.Date;
import java.util.List;
+
/**
* Tests for Deferred which can run either in JVM and GWT
*/
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
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"));
}