Skip to content

Commit

Permalink
Move elemental json dependency to DWS (#15544)
Browse files Browse the repository at this point in the history
Change-Id: I1b525e4d8df60f8e36bad9e5054d948da5b34813
  • Loading branch information
Artur- committed Jan 11, 2015
1 parent 38a9f36 commit 44d34f5
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 80 deletions.
3 changes: 3 additions & 0 deletions client/src/com/vaadin/DefaultWidgetSet.gwt.xml
Expand Up @@ -8,6 +8,9 @@

<inherits name="com.vaadin.Vaadin" />

<!-- Elemental is used for handling Json only -->
<inherits name="elemental.Json" />

<entry-point class="com.vaadin.client.ApplicationConfiguration" />

<generate-with
Expand Down
2 changes: 0 additions & 2 deletions client/src/com/vaadin/Vaadin.gwt.xml
Expand Up @@ -10,8 +10,6 @@

<inherits name="com.google.gwt.http.HTTP" />

<inherits name="elemental.Json" />

<inherits name="com.google.gwt.logging.Logging" />
<set-property name="gwt.logging.enabled" value="TRUE" />

Expand Down
4 changes: 2 additions & 2 deletions client/src/com/vaadin/client/ApplicationConnection.java
Expand Up @@ -2158,7 +2158,7 @@ private JsArrayObject<StateChangeEvent> updateConnectorState(

JavaScriptObject jso = states
.getJavaScriptObject(connectorId);
JsonObject stateJson = WidgetUtil.jso2json(jso);
JsonObject stateJson = Util.jso2json(jso);

if (connector instanceof HasJavaScriptConnectorHelper) {
((HasJavaScriptConnectorHelper) connector)
Expand Down Expand Up @@ -2530,7 +2530,7 @@ private void handleRpcInvocations(ValueMap json) {

VConsole.log(" * Performing server to client RPC calls");

JsonArray rpcCalls = WidgetUtil.jso2json(json
JsonArray rpcCalls = Util.jso2json(json
.getJavaScriptObject("rpc"));

int rpcLength = rpcCalls.length();
Expand Down
9 changes: 4 additions & 5 deletions client/src/com/vaadin/client/JavaScriptConnectorHelper.java
Expand Up @@ -319,7 +319,7 @@ private void fireRpc(String iface, String method,
iface = findWildcardInterface(method);
}

JsonArray argumentsArray = WidgetUtil.jso2json(arguments);
JsonArray argumentsArray = Util.jso2json(arguments);
Object[] parameters = new Object[arguments.length()];
for (int i = 0; i < parameters.length; i++) {
parameters[i] = argumentsArray.get(i);
Expand Down Expand Up @@ -383,7 +383,7 @@ private static native void updateNativeState(JavaScriptObject state,
}-*/;

public Object[] decodeRpcParameters(JsonArray parametersJson) {
return new Object[] { WidgetUtil.json2jso(parametersJson) };
return new Object[] { Util.json2jso(parametersJson) };
}

public void setTag(int tag) {
Expand All @@ -397,11 +397,10 @@ public void invokeJsRpc(MethodInvocation invocation,
if ("com.vaadin.ui.JavaScript$JavaScriptCallbackRpc".equals(iface)
&& "call".equals(method)) {
String callbackName = parametersJson.getString(0);
JavaScriptObject arguments = WidgetUtil.json2jso(parametersJson
.get(1));
JavaScriptObject arguments = Util.json2jso(parametersJson.get(1));
invokeCallback(getConnectorWrapper(), callbackName, arguments);
} else {
JavaScriptObject arguments = WidgetUtil.json2jso(parametersJson);
JavaScriptObject arguments = Util.json2jso(parametersJson);
invokeJsRpc(rpcMap, iface, method, arguments);
// Also invoke wildcard interface
invokeJsRpc(rpcMap, "", method, arguments);
Expand Down
65 changes: 65 additions & 0 deletions client/src/com/vaadin/client/Util.java
Expand Up @@ -22,6 +22,8 @@
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.KeyEvent;
Expand All @@ -37,6 +39,9 @@
import com.vaadin.shared.ui.ComponentStateUtil;
import com.vaadin.shared.util.SharedUtil;

import elemental.js.json.JsJsonValue;
import elemental.json.JsonValue;

public class Util {

/**
Expand Down Expand Up @@ -895,4 +900,64 @@ public static void setSelectionRange(Element elem, int pos, int length,
WidgetUtil.setSelectionRange(elem, pos, length, direction);
}

/**
* Converts a native {@link JavaScriptObject} into a {@link JsonValue}. This
* is a no-op in GWT code compiled to javascript, but needs some special
* handling to work when run in JVM.
*
* @param jso
* the java script object to represent as json
* @return the json representation
*/
public static <T extends JsonValue> T jso2json(JavaScriptObject jso) {
if (GWT.isProdMode()) {
return (T) jso.<JsJsonValue> cast();
} else {
return elemental.json.Json.instance().parse(stringify(jso));
}
}

/**
* Converts a {@link JsonValue} into a native {@link JavaScriptObject}. This
* is a no-op in GWT code compiled to javascript, but needs some special
* handling to work when run in JVM.
*
* @param jsonValue
* the json value
* @return a native javascript object representation of the json value
*/
public static JavaScriptObject json2jso(JsonValue jsonValue) {
if (GWT.isProdMode()) {
return ((JavaScriptObject) jsonValue.toNative()).cast();
} else {
return parse(jsonValue.toJson());
}
}

/**
* Convert a {@link JavaScriptObject} into a string representation.
*
* @param json
* a JavaScript object to be converted to a string
* @return JSON in string representation
*/
private native static String stringify(JavaScriptObject json)
/*-{
return JSON.stringify(json);
}-*/;

/**
* Parse a string containing JSON into a {@link JavaScriptObject}.
*
* @param <T>
* the overlay type to expect from the parse
* @param jsonAsString
* @return a JavaScript object constructed from the parse
*/
public native static <T extends JavaScriptObject> T parse(
String jsonAsString)
/*-{
return JSON.parse(jsonAsString);
}-*/;

}
2 changes: 1 addition & 1 deletion client/src/com/vaadin/client/VUIDLBrowser.java
Expand Up @@ -161,7 +161,7 @@ class SharedStateItem extends StateChangeItem {
} else {
setText("Unknown connector (" + connectorId + ")");
}
dir((JsonObject) WidgetUtil.jso2json(stateChanges), this);
dir((JsonObject) Util.jso2json(stateChanges), this);
}

@Override
Expand Down
65 changes: 0 additions & 65 deletions client/src/com/vaadin/client/WidgetUtil.java
Expand Up @@ -21,8 +21,6 @@
import java.util.Map;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.AnchorElement;
Expand All @@ -48,9 +46,6 @@
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.shared.util.SharedUtil;

import elemental.js.json.JsJsonValue;
import elemental.json.JsonValue;

/**
* Utility methods which are related to client side code only
*/
Expand Down Expand Up @@ -1223,66 +1218,6 @@ public native static void setSelectionRange(Element elem, int pos,
}
}-*/;

/**
* Converts a native {@link JavaScriptObject} into a {@link JsonValue}. This
* is a no-op in GWT code compiled to javascript, but needs some special
* handling to work when run in JVM.
*
* @param jso
* the java script object to represent as json
* @return the json representation
*/
public static <T extends JsonValue> T jso2json(JavaScriptObject jso) {
if (GWT.isProdMode()) {
return (T) jso.<JsJsonValue> cast();
} else {
return elemental.json.Json.instance().parse(stringify(jso));
}
}

/**
* Converts a {@link JsonValue} into a native {@link JavaScriptObject}. This
* is a no-op in GWT code compiled to javascript, but needs some special
* handling to work when run in JVM.
*
* @param jsonValue
* the json value
* @return a native javascript object representation of the json value
*/
public static JavaScriptObject json2jso(JsonValue jsonValue) {
if (GWT.isProdMode()) {
return ((JavaScriptObject) jsonValue.toNative()).cast();
} else {
return parse(jsonValue.toJson());
}
}

/**
* Convert a {@link JavaScriptObject} into a string representation.
*
* @param json
* a JavaScript object to be converted to a string
* @return JSON in string representation
*/
private native static String stringify(JavaScriptObject json)
/*-{
return JSON.stringify(json);
}-*/;

/**
* Parse a string containing JSON into a {@link JavaScriptObject}.
*
* @param <T>
* the overlay type to expect from the parse
* @param jsonAsString
* @return a JavaScript object constructed from the parse
*/
public native static <T extends JavaScriptObject> T parse(
String jsonAsString)
/*-{
return JSON.parse(jsonAsString);
}-*/;

/**
* The allowed value inaccuracy when comparing two double-typed pixel
* values.
Expand Down
Expand Up @@ -25,11 +25,12 @@
import com.vaadin.client.JsArrayObject;
import com.vaadin.client.Profiler;
import com.vaadin.client.ServerConnector;
import com.vaadin.client.WidgetUtil;
import com.vaadin.client.Util;
import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
import com.vaadin.client.metadata.NoDataException;
import com.vaadin.client.metadata.Property;
import com.vaadin.client.ui.AbstractConnector;

import elemental.json.JsonObject;

public class StateChangeEvent extends
Expand Down Expand Up @@ -204,7 +205,7 @@ public boolean hasPropertyChanged(String property) {
return true;
} else if (stateJson != null) {
// Check whether it's in the json object
return isInJson(property, WidgetUtil.json2jso(stateJson));
return isInJson(property, Util.json2jso(stateJson));
} else {
// Legacy cases
if (changedProperties != null) {
Expand Down
Expand Up @@ -22,7 +22,7 @@
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.vaadin.client.ServerConnector;
import com.vaadin.client.WidgetUtil;
import com.vaadin.client.Util;
import com.vaadin.client.communication.JavaScriptMethodInvocation;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.extensions.AbstractExtensionConnector;
Expand Down Expand Up @@ -116,8 +116,7 @@ private static native void eval(String script)
}-*/;

public void sendRpc(String name, JsArray<JavaScriptObject> arguments) {
Object[] parameters = new Object[] { name,
WidgetUtil.jso2json(arguments) };
Object[] parameters = new Object[] { name, Util.jso2json(arguments) };

/*
* Must invoke manually as the RPC interface can't be used in GWT
Expand Down

0 comments on commit 44d34f5

Please sign in to comment.