aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2014-02-18 16:10:17 +0100
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2014-02-18 16:10:17 +0100
commit8276113ee99e4bf00656017ae52a05f99fec5391 (patch)
treefda28302effbf078c58519c3d14530837537c565
parent859a65dac1dc46c16f558f083d9640276cd7d1ad (diff)
downloadgwtquery-8276113ee99e4bf00656017ae52a05f99fec5391.tar.gz
gwtquery-8276113ee99e4bf00656017ae52a05f99fec5391.zip
Dont try to fix json strings by default, to avoiding quoted numbers and null be unquoted
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java18
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java9
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java1
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java3
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java8
-rw-r--r--gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java5
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java4
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java21
8 files changed, 61 insertions, 8 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java
index 292321b8..a780602c 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java
@@ -64,13 +64,25 @@ public abstract class GQ {
}
/**
- * 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
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 ed39d4eb..db35f5d6 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
@@ -45,6 +45,15 @@ public class Properties extends JavaScriptObject implements IsProperties {
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
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
index ac01f7ca..6acee884 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
@@ -432,6 +432,7 @@ public class JsUtils {
} catch (Exception e) {
if (!GWT.isProdMode()) {
System.err.println("Error while parsing json: " + e.getMessage() + ".\n" + json);
+ new RuntimeException().printStackTrace();
}
return Properties.create();
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java
index 270db704..f85177de 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java
@@ -43,6 +43,7 @@ import com.google.gwt.query.client.builders.JsonBuilder;
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;
@@ -324,7 +325,7 @@ public class JsonBuilderGenerator extends Generator {
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() {");
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 97243244..775a789f 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
@@ -179,7 +179,11 @@ public class JsonFactoryJre implements JsonFactory {
} 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")) {
@@ -296,7 +300,7 @@ public class JsonFactoryJre implements JsonFactory {
@Override
public IsProperties create(String s) {
IsProperties ret = createBinder();
- ret.parse(Properties.wrapPropertiesString(s));
+ ret.parse(s);
return ret;
}
diff --git a/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java b/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java
index 7f8fb42e..3647538b 100644
--- a/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java
+++ b/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java
@@ -17,6 +17,7 @@ package com.google.gwt.query.client;
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;
@@ -44,6 +45,10 @@ public class GQ {
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();
}
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java
index f5d0cbf5..1920afc3 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java
@@ -50,8 +50,8 @@ public abstract class AjaxTests extends GWTTestCase {
};
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) {
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 874fb6ef..965bf161 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
@@ -22,6 +22,7 @@ import java.util.List;
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;
@@ -34,6 +35,26 @@ public class DataBindingTestJre extends GWTTestCase {
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}