public final Object getObject(Object name) {
return c().get(String.valueOf(name));
}
-
+
public final Properties getProperties(Object name) {
return getJavaScriptObject(name);
}
/**
* Adds a new native js function to the properties object.
* This native function will wrap the passed java Function.
- *
+ *
* Its useful for exporting or importing to javascript.
- *
+ *
*/
public final native <T> void setFunction(T name, Function f) /*-{
if (!f) return;
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));
}
public final String toJson() {
return toJsonString();
}
-
+
public final String toJsonWithName() {
return toJsonWithName(getJsonName());
}
-
+
public final String toJsonWithName(String name) {
return "{\"" + name + "\":{" + toJson() + "}";
}
import com.google.gwt.core.client.JsArray;
import com.google.gwt.query.client.IsProperties;
import com.google.gwt.query.client.Properties;
+import com.google.gwt.query.client.js.JsCache;
import com.google.gwt.query.client.js.JsObjectArray;
import com.google.gwt.query.client.js.JsUtils;
+import com.google.gwt.user.client.Window;
+
+import java.util.Arrays;
+import java.util.List;
public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements JsonBuilder {
protected Properties p = Properties.create();
+ protected String[] fieldNames = new String[] {};
@SuppressWarnings("unchecked")
@Override
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);
+ List<String> names = Arrays.asList(getFieldNames());
+ for (String jsonName : p.getFieldNames()) {
+ if (!names.contains(jsonName)) {
+ p.<JsCache>cast().delete(jsonName);
+ }
+ }
return (J)this;
}
public String toJson() {
return p.tostring();
}
-
+
public String toJsonWithName() {
return "{\"" + getJsonName() + "\":" + p.tostring() + "}";
}
public Properties getProperties() {
return p;
}
-
+
@Override
public String toQueryString() {
return p.toQueryString();
}
-
+
@SuppressWarnings("unchecked")
@Override
public Properties getDataImpl() {
return p;
}
-
+
public <T> T get(Object key) {
return p.get(key);
}
-
+
@SuppressWarnings("unchecked")
public <T extends IsProperties> T set(Object key, Object val) {
if (val instanceof IsProperties) {
}
return (T)this;
}
-
+
public <T extends JsonBuilder> T as(Class<T> clz) {
return p.as(clz);
}
+
+ public final String[] getFieldNames() {
+ return fieldNames;
+ }
}
for (Iterator<String> it = attrs.iterator(); it.hasNext();) {
ret += (ret.isEmpty() ? "" : ",") + "\"" + it.next() + "\"";
}
- sw.println("public final String[] getFieldNames() {return new String[]{" + ret + "};}");
+ sw.println("{ fieldNames = new String[]{" + ret + "}; }");
}
public void generateMethod(SourceWriter sw, JMethod method, String name, TreeLogger logger)
*/
package com.google.gwt.query.client.dbinding;
-import com.google.gwt.core.shared.GWT;
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.builders.JsonBuilder;
import com.google.gwt.query.client.builders.Name;
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
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());
+++ /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);
-
- 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' " +
- " }";
-
- @Test public void
- test_parse_json() {
- GUser entity = GQ.create(GUser.class);
- entity.parse(JSON_USER_EXAMPLE);
-
- 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.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"));
- }
-
-}