下面列出了怎么用com.google.gwt.core.client.JsonUtils的API类实例代码及写法,或者点击链接到github查看源代码。
private void draw() {
JsArrayMixed dataArray = JsonUtils.unsafeEval("[['Mon',20,28,38,45],['Tue',31,38,55,66],['Wed',50,55,77,80],['Thu',77,77,66,50],['Fri',68,66,22,15]]");
// Prepare the data
DataTable dataTable = ChartHelper.arrayToDataTable(dataArray, true);
// Set options
CandlestickChartOptions options = CandlestickChartOptions.create();
BackgroundColor bgColor = BackgroundColor.create();
bgColor.setStroke("#2196f3");
bgColor.setFill("#90caf9");
bgColor.setStrokeWidth(2);
options.setLegend(Legend.create(LegendPosition.NONE));
options.setFallingColor(bgColor);
options.setRisingColor(bgColor);
// Draw the chart
chart.draw(dataTable, options);
}
private void draw() {
JsArrayMixed dataArray = JsonUtils
.unsafeEval("[['Month', 'Bolivia', 'Ecuador', 'Madagascar', 'Papua Guinea', 'Rwanda', 'Average'],['2004/05', 165, 938, 522, 998, 450, 614.6],['2005/06', 135, 1120, 599, 1268, 288, 682],['2006/07', 157, 1167, 587, 807, 397, 623],['2007/08', 139, 1110, 615, 968, 215, 609.4],['2008/09', 136, 691, 629, 1026, 366, 569.6]]");
// Prepare the data
DataTable dataTable = ChartHelper.arrayToDataTable(dataArray);
// Set options
ComboChartOptions options = ComboChartOptions.create();
options.setFontName("Tahoma");
options.setTitle("Monthly Coffee Production by Country");
options.setHAxis(HAxis.create("Cups"));
options.setVAxis(VAxis.create("Month"));
options.setSeriesType(SeriesType.BARS);
ComboChartSeries series = ComboChartSeries.create();
series.setType(SeriesType.LINE);
options.setSeries(5, series);
// Draw the chart
chart.draw(dataTable, options);
}
/**
* Define a new custom annotation.
*
* @param key annotation's name
* @param cssClass a css class for the html container
* @param cssStyle css styles for the html container
*/
public static void define(String key, String cssClass, JavaScriptObject cssStyleObj) throws SEditorException {
if (key == null || key.startsWith("paragraph") || key.startsWith(AnnotationConstants.STYLE_PREFIX) || CANONICAL_KEYS.getString(key) != null) {
throw new SEditorException("Not valid annotation name");
}
JsoView cssStyles = null;
if (JsEditorUtils.isString(cssStyleObj)) {
JavaScriptObject o = JsonUtils.unsafeEval(cssStyleObj.toString());
if (o != null) {
cssStyles = JsoView.as(o);
}
} else {
cssStyles = JsoView.as(cssStyleObj);
}
AnnotationController annotation = new AnnotationController(key);
store.put(key, annotation);
UserAnnotationHandler.register(Editor.ROOT_REGISTRIES, key, cssClass, cssStyles,
annotation.getTextEventHanlder(), annotation.getTextEventHanlder(),
annotation.getTextEventHanlder());
}
private List<Double> getNumberArrayAttribute(SliderOption option, List<Double> defaultValue) {
// Get array attribute
JsArrayNumber array = null;
if (isAttached()) {
array = getNumberArrayAttribute(getElement(), option.getName());
} else {
String value = attributeMixin.getAttribute(option.getDataAttribute());
if (value != null && !value.isEmpty()) {
array = JsonUtils.safeEval(value);
}
}
// Attribute not set
if (array == null) {
return defaultValue;
}
// Put array to list
List<Double> list = new ArrayList<Double>(array.length());
for (int i = 0; i < array.length(); i++) {
list.add(array.get(i));
}
return list;
}
private List<String> getStringArrayAttribute(SliderOption option, List<String> defaultValue) {
// Get array attribute
JsArrayString array = null;
if (isAttached()) {
array = getStringArrayAttribute(getElement(), option.getName());
} else {
String value = attributeMixin.getAttribute(option.getDataAttribute());
if (value != null && !value.isEmpty()) {
array = JsonUtils.safeEval(value);
}
}
// Attribute not set
if (array == null) {
return defaultValue;
}
// Put array to list
List<String> list = new ArrayList<String>(array.length());
for (int i = 0; i < array.length(); i++) {
list.add(array.get(i));
}
return list;
}
public static JSONValue parse(String token) {
try {
if (JsonUtils.safeToEval(token)) {
JSONValue jsonv = JSONParser.parseStrict(token);
return jsonv;
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, token, ex);
}
return null;
}
@Override
protected <O extends ServiceOperation.Options> String toJson(O options) {
if (options != null) {
if (options instanceof JavaScriptObject) {
return JsonUtils.stringify((JavaScriptObject) options);
}
}
return null;
}
@Override
public Object parseJsonObject(String json) {
if (json != null)
return JsonUtils.<JavaScriptObject> safeEval(json);
return null;
}
@Override
public String serializeJsonObject(Object o) {
if (isJsPrimitive(o)) {
return o.toString();
} else if (o instanceof JavaScriptObject) {
return JsonUtils.stringify((JavaScriptObject) o);
}
throw new IllegalStateException("Object can't be serialized to JSON");
}
@Override
public String serialize(SJsonObject object) {
if (object instanceof SJsonObjectWeb) {
SJsonObjectWeb jow = (SJsonObjectWeb) object;
return JsonUtils.stringify(jow.getJsoView());
}
return null;
}
public void testWriteGeoJSON() {
injectUrlAndTest(() -> {
String geoJson = geoJsonFormat.writeFeatures(OLTestUtils.createLineFeatures(), null);
assertNotNull(geoJson);
java.lang.Object geoJsonObject = JsonUtils.safeEval(geoJson);
assertNotNull(geoJsonObject);
});
}
private void updateSliderForNumberArray(SliderOption option, List<Double> value) {
JsArrayNumber array = JavaScriptObject.createArray().cast();
for (Double val : value) {
array.push(val);
}
if (isAttached()) {
setAttribute(getElement(), option.getName(), array);
refresh();
} else {
String arrayStr = JsonUtils.stringify(array);
attributeMixin.setAttribute(option.getDataAttribute(), arrayStr);
}
}
private void updateSliderForStringArray(SliderOption option, List<String> value) {
JsArrayString array = JavaScriptObject.createArray().cast();
for (String val : value) {
array.push(val);
}
if (isAttached()) {
setAttribute(getElement(), option.getName(), array);
refresh();
} else {
String arrayStr = JsonUtils.stringify(array);
attributeMixin.setAttribute(option.getDataAttribute(), arrayStr);
}
}
@Override
protected OperationError parseServiceError(String json) {
return (OperationError) JsonUtils.safeEval(json);
}
protected <T extends JavaScriptObject> T eval(String response) {
return USE_SAFE_EVAL ? JsonUtils.<T>safeEval(response) : JsonUtils.<T>unsafeEval(response);
}
private void string(String value) {
out.append(JsonUtils.escapeValue(value));
}
/**
* Converts the given string to a range instance.<br>
* <br>
* Useful when using UiBinder.
*
* @param value
* @return
*/
public static Range fromString(String value) {
if (value == null || value.isEmpty())
return null;
JsArrayNumber array = JsonUtils.safeEval(value);
return new Range(array);
}
/**
* Sets the window padding to top, right, bottom, and right sides. This
* is useful in cases where the window has areas that the drop-down menu
* should not cover - for instance a fixed header.
*
* @param top
* @param right
* @param bottom
* @param left
*/
public void setWindowPaddingTopRightBottomLeft(final int top, final int right,
final int bottom, final int left) {
JsArrayNumber array = JavaScriptObject.createArray(4).cast();
array.push(top);
array.push(right);
array.push(bottom);
array.push(left);
attrMixin.setAttribute(WINDOW_PADDING, JsonUtils.stringify(array));
}
/**
* Performs evaluation of serialized response obeying the #useSafeEval configuration.
* <p/>
*
* If #useSafeEval is {@code true} then the eval is performed using {@link JsonUtils#safeEval},
* otherwise then content will be loosely evaluated by {@link JsonUtils#unsafeEval}.
*
* @param response The serialized content
*
* @return The converted JavaScriptObject
*/
protected JavaScriptObject eval(String response) {
return useSafeEval() ? JsonUtils.safeEval(response) : JsonUtils.unsafeEval(response);
}