}
/**
- * Create an instance of IsProperties. Normally a Properties javascript
- * object in client side, or a proxy object in the JVM
+ * Create an instance of IsProperties, a Properties JavaScriptObject in the client
+ * side and a proxy object in the JVM.
*/
public static IsProperties create(String s) {
return getFactory().create(s);
}
-
+
+ /**
+ * Create an instance of IsProperties, a Properties JavaScriptObject in the client
+ * side and a proxy object in the JVM.
+ *
+ * If fixJson is set, we correct certain errors in the Json string. It is useful
+ * for generating Properties using java strings, so as we can use a more relaxed
+ * syntax.
+ */
+ public static IsProperties create(String s, boolean fixJson) {
+ return getFactory().create(fixJson ? Properties.wrapPropertiesString(s) : s);
+ }
+
/**
* Return the appropriate transport implementation depending on the runtime
* environment: browser or JVM
return create();
}
+ /**
+ * Allows using a more relaxed syntax for creating json objects from strings.
+ *
+ * It is very useful in java, since we dont have to use escaped double quotes,
+ * and we can pass directly css strings.
+ *
+ * Example:
+ * $$("a: b; c: 'n'; d: null") is the same than $$("\"a\": \"b\", "\b\":\"n\"n, \"d\":null)")
+ */
public static String wrapPropertiesString(String s) {
String ret = s //
.replaceAll("\\s*/\\*[\\s\\S]*?\\*/\\s*", "") // Remove comments
} catch (Exception e) {
if (!GWT.isProdMode()) {
System.err.println("Error while parsing json: " + e.getMessage() + ".\n" + json);
+ new RuntimeException().printStackTrace();
}
return Properties.create();
}
import com.google.gwt.query.client.builders.JsonBuilderBase;
import com.google.gwt.query.client.builders.JsonFactory;
import com.google.gwt.query.client.builders.Name;
+import com.google.gwt.query.client.js.JsUtils;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
sw.println("}");
sw.println("public " + IsProperties.class.getName() + " create(String s) {");
sw.indent();
- sw.println("return " + Properties.class.getName() + ".create(s);");
+ sw.println("return (" + IsProperties.class.getName() + ")" + JsUtils.class.getName() + ".parseJSON(s);");
sw.outdent();
sw.println("}");
sw.println("public " + IsProperties.class.getName() + " create() {");
} else if (mname.matches("getProperties|getDataImpl")) {
return jsonObject;
} else if (largs > 0 && ("parse".equals(mname) || "load".equals(mname))) {
- jsonObject = new JSONObject(String.valueOf(args[0]));
+ String json = String.valueOf(args[0]);
+ if (largs > 1 && Boolean.TRUE.equals(args[0])) {
+ json = Properties.wrapPropertiesString(json);
+ }
+ jsonObject = new JSONObject(json);
} else if (mname.matches("toString")) {
return jsonObject.toString();
} else if (mname.matches("toJsonWithName")) {
@Override
public IsProperties create(String s) {
IsProperties ret = createBinder();
- ret.parse(Properties.wrapPropertiesString(s));
+ ret.parse(s);
return ret;
}
import com.google.gwt.core.shared.GWT;
import com.google.gwt.query.client.IsProperties;
+import com.google.gwt.query.client.Properties;
import com.google.gwt.query.client.builders.JsonBuilder;
import com.google.gwt.query.client.builders.JsonFactory;
import com.google.gwt.query.client.plugins.ajax.AjaxTransportJs;
return getFactory().create(s);
}
+ public static IsProperties create(String s, boolean fix) {
+ return getFactory().create(fix ? Properties.wrapPropertiesString(s) : s);
+ }
+
public static IsProperties create() {
return getFactory().create();
}
};
public AjaxTests() {
- jsonGET = GQ.create("data: {a: abc, d: def}");
- json = GQ.create("a: abc, d: def");
+ jsonGET = GQ.create("data: {a: abc, d: def}", true);
+ json = GQ.create("a: abc, d: def", true);
}
private Promise performAjaxJsonTest(Settings s) {
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.GQ;
+import com.google.gwt.query.client.IsProperties;
import com.google.gwt.query.client.builders.JsonBuilder;
import com.google.gwt.query.client.builders.Name;
return null;
}
+ public void testPropertiesCreate() {
+ IsProperties p1 = GQ.create();
+ p1.set("a", "1");
+ p1.set("b", 1);
+ p1.set("c", "null");
+ p1.set("d", null);
+
+ assertEquals("1", p1.get("a"));
+ assertEquals(Double.valueOf(1), p1.get("b"));
+ assertEquals("null", p1.get("c"));
+ assertNull(p1.get("d"));
+
+ p1 = GQ.create(p1.toJson());
+
+ assertEquals("1", p1.get("a"));
+ assertEquals(Double.valueOf(1), p1.get("b"));
+ assertEquals("null", p1.get("c"));
+ assertNull(p1.get("d"));
+ }
+
public interface Item extends JsonBuilder {
public static enum Type {BIG, SMALL}