Example usage for com.google.gwt.core.client JsonUtils unsafeEval

List of usage examples for com.google.gwt.core.client JsonUtils unsafeEval

Introduction

In this page you can find the example usage for com.google.gwt.core.client JsonUtils unsafeEval.

Prototype

public static native <T extends JavaScriptObject> T unsafeEval(String json) ;

Source Link

Document

Evaluates a JSON expression using eval() .

Usage

From source file:com.github.nmorel.gwtjackson.client.stream.impl.DefaultJsonReader.java

License:Apache License

@Override
public JavaScriptObject nextJavaScriptObject(boolean useSafeEval) {
    int p = peeked;
    if (p == PEEKED_NONE) {
        p = doPeek();/*from  w  w w.jav a 2s . co  m*/
    }

    switch (p) {
    case PEEKED_BEGIN_OBJECT:
    case PEEKED_BEGIN_ARRAY:
        JavaScriptObject result;
        int peekStack = stack.get(stackSize - 1);
        if (peekStack == JsonScope.NONEMPTY_DOCUMENT) {
            // start of the document
            String toEval = in.getInput();
            result = useSafeEval ? JsonUtils.safeEval(toEval) : JsonUtils.unsafeEval(toEval);
            // we read everything, we move the pointer to the end of the document
            pos = toEval.length();
            limit = toEval.length();
            peeked = PEEKED_NONE;
        } else {
            String toEval = nextValue();
            result = useSafeEval ? JsonUtils.safeEval(toEval) : JsonUtils.unsafeEval(toEval);
        }
        return result;
    default:
        throw new IllegalStateException("Expected an array or object to evaluate a JavaScriptObject but was "
                + peek() + " at line " + getLineNumber() + " column " + getColumnNumber());
    }
}

From source file:com.github.nmorel.gwtjackson.client.stream.impl.NonBufferedJsonReader.java

License:Apache License

@Override
public JavaScriptObject nextJavaScriptObject(boolean useSafeEval) {
    int p = peeked;
    if (p == PEEKED_NONE) {
        p = doPeek();/*  ww w. j  a v  a 2 s. c  om*/
    }

    switch (p) {
    case PEEKED_BEGIN_OBJECT:
    case PEEKED_BEGIN_ARRAY:
        JavaScriptObject result;
        int peekStack = stack.get(stackSize - 1);
        if (peekStack == JsonScope.NONEMPTY_DOCUMENT) {
            // start of the document
            String toEval = in;
            result = useSafeEval ? JsonUtils.safeEval(toEval) : JsonUtils.unsafeEval(toEval);
            // we read everything, we move the pointer to the end of the document
            pos = toEval.length();
            peeked = PEEKED_NONE;
        } else {
            String toEval = nextValue();
            result = useSafeEval ? JsonUtils.safeEval(toEval) : JsonUtils.unsafeEval(toEval);
        }
        return result;
    default:
        throw new IllegalStateException("Expected an array or object to evaluate a JavaScriptObject but was "
                + peek() + " at line " + getLineNumber() + " column " + getColumnNumber());
    }
}

From source file:fr.mncc.minus.ajax.client.JsonParser.java

License:Open Source License

/**
 * Convert a JSON string to Java object// w w  w  .j  a v a2  s .  com
 * 
 * @param json string
 * @return Java object
 */
public static <T extends JavaScriptObject> T fromJson(String json) {
    return JsonUtils.unsafeEval(json);
}

From source file:gwt.material.design.demo.client.ui.charts.MaterialCandleStick.java

License:Apache License

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);/*  w  w w .ja  v  a2  s  .c om*/

    options.setLegend(Legend.create(LegendPosition.NONE));
    options.setFallingColor(bgColor);
    options.setRisingColor(bgColor);

    // Draw the chart
    chart.draw(dataTable, options);
}

From source file:gwt.material.design.demo.client.ui.charts.MaterialComboCharts.java

License:Apache License

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);//  w  w  w  .ja  v a  2  s.c o  m
    options.setSeries(5, series);

    // Draw the chart
    chart.draw(dataTable, options);
}

From source file:io.reinert.requestor.serialization.json.JsonObjectSerdes.java

License:Apache License

/**
 * Performs evaluation of serialized response obeying the #useSafeEval configuration.
 * <p/>/*from   w w  w . j  a v a 2  s  .  c o  m*/
 *
 * 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);
}

From source file:org.eclipse.che.ide.examples.CheFluxLiveEditExtension.java

License:Open Source License

@Inject
public CheFluxLiveEditExtension(final MessageBusProvider messageBusProvider, final EventBus eventBus,
        final MachineManager machineManager, final MachineServiceClient machineServiceClient,
        final DtoUnmarshallerFactory dtoUnmarshallerFactory, final AppContext appContext,
        final CommandManager commandManager,
        final CommandPropertyValueProviderRegistry commandPropertyValueProviderRegistry) {
    this.commandManager = commandManager;
    this.messageBus = messageBusProvider.getMessageBus();
    this.commandPropertyValueProviderRegistry = commandPropertyValueProviderRegistry;

    injectSocketIO();// w ww.j ava 2 s  .  c  o m

    eventBus.addHandler(ProjectExplorerLoadedEvent.getType(),
            new ProjectExplorerLoadedEvent.ProjectExplorerLoadedHandler() {

                @Override
                public void onProjectsLoaded(ProjectExplorerLoadedEvent event) {

                    String machineId = appContext.getDevMachineId();
                    Promise<List<MachineProcessDto>> processesPromise = machineServiceClient
                            .getProcesses(machineId);
                    processesPromise.then(new Operation<List<MachineProcessDto>>() {
                        @Override
                        public void apply(final List<MachineProcessDto> descriptors) throws OperationException {

                            if (descriptors.isEmpty()) {
                                return;
                            }
                            Log.info(getClass(), "machine processes");
                            for (MachineProcessDto machineProcessDto : descriptors) {
                                Log.info(getClass(), " - " + machineProcessDto);
                                if (connectIfFluxMicroservice(machineProcessDto)) {
                                    break;
                                }
                            }
                        }

                    });

                }
            });

    String machineId = appContext.getDevMachineId();
    final Unmarshallable<MachineProcessEvent> unmarshaller = dtoUnmarshallerFactory
            .newWSUnmarshaller(MachineProcessEvent.class);
    final String processStateChannel = "machine:process:" + machineId;
    final MessageHandler handler = new SubscriptionHandler<MachineProcessEvent>(unmarshaller) {
        @Override
        protected void onMessageReceived(MachineProcessEvent result) {
            Log.info(getClass(), "process event" + result);

            if (MachineProcessEvent.EventType.STARTED.equals(result.getEventType())) {
                final int processId = result.getProcessId();
                machineServiceClient.getProcesses(appContext.getDevMachineId())
                        .then(new Operation<List<MachineProcessDto>>() {
                            @Override
                            public void apply(List<MachineProcessDto> descriptors) throws OperationException {
                                if (descriptors.isEmpty()) {
                                    return;
                                }

                                for (final MachineProcessDto machineProcessDto : descriptors) {
                                    if (machineProcessDto.getPid() == processId) {
                                        Log.info(getClass(), "Started Process" + machineProcessDto);
                                        new Timer() {
                                            @Override
                                            public void run() {
                                                if (connectIfFluxMicroservice(machineProcessDto)) {
                                                    // break;
                                                }
                                            }
                                        }.schedule(8000);
                                        return;
                                    }
                                }
                            }

                        });
            }
        }

        @Override
        protected void onErrorReceived(Throwable exception) {
            Log.error(getClass(), exception);
        }
    };
    wsSubscribe(processStateChannel, handler);

    eventBus.addHandler(DocumentReadyEvent.TYPE, new DocumentReadyHandler() {
        @Override
        public void onDocumentReady(DocumentReadyEvent event) {
            liveDocuments.put(event.getDocument().getFile().getPath(), event.getDocument());

            final DocumentHandle documentHandle = event.getDocument().getDocumentHandle();

            documentHandle.getDocEventBus().addHandler(DocumentChangeEvent.TYPE, new DocumentChangeHandler() {
                @Override
                public void onDocumentChange(DocumentChangeEvent event) {
                    if (socket != null) {
                        // full path start with /, so substring
                        String fullPath = event.getDocument().getDocument().getFile().getPath().substring(1);
                        String project = fullPath.substring(0, fullPath.indexOf('/'));
                        String resource = fullPath.substring(fullPath.indexOf('/') + 1);
                        String text = JsonUtils.escapeValue(event.getText());
                        String json = "{"//
                                + "\"username\":\"USER\","//
                                + "\"project\":\"" + project + "\","//
                                + "\"resource\":\"" + resource + "\"," //
                                + "\"offset\":" + event.getOffset() + "," //
                                + "\"removedCharCount\":" + event.getRemoveCharCount() + "," //
                                + "\"addedCharacters\": " + text //
                                + "}";
                        if (isUpdatingModel) {
                            return;
                        }
                        socket.emit("liveResourceChanged", JsonUtils.unsafeEval(json));
                    }
                }
            });
        }
    });

}

From source file:org.eclipse.che.ide.flux.liveedit.FluxMessageBuilder.java

License:Open Source License

public Message buildResourceRequestMessage() {
    String json = "{"//
            + "\"username\":\"" + username + "\","//
            + "\"project\":\"" + project + "\","//
            + "\"resource\":\"" + resource + "\","//
            + "\"channelName\":\"" + channelName + "\"" //
            + "}";

    return new Message().withType("getResourceRequest")//
            .withJsonContent(JsonUtils.unsafeEval(json));
}

From source file:org.eclipse.che.ide.flux.liveedit.FluxMessageBuilder.java

License:Open Source License

public Message buildLiveResourceChangeMessage() {
    String json = "{"//
            + "\"username\":\"" + username + "\","//
            + "\"project\":\"" + project + "\","//
            + "\"resource\":\"" + resource + "\"," //
            + "\"channelName\":\"" + channelName + "\"," //
            + "\"offset\":" + offset + "," //
            + "\"removedCharCount\":" + removeCharCount + "," //
            + "\"addedCharacters\": " + addedCharacters //
            + "}";
    return new Message().withType("liveResourceChanged")//
            .withJsonContent(JsonUtils.unsafeEval(json));
}

From source file:org.eclipse.che.ide.flux.liveedit.FluxMessageBuilder.java

License:Open Source License

public Message buildLiveCursorOffsetChangeMessage() {
    String json = "{"//
            + "\"username\":\"" + username + "\","//
            + "\"project\":\"" + project + "\","//
            + "\"resource\":\"" + resource + "\"," //
            + "\"channelName\":\"" + channelName + "\"," //
            + "\"offset\":" + offset + "," //
            + "}";
    return new Message().withType("liveCursorOffsetChanged")//
            .withJsonContent(JsonUtils.unsafeEval(json));
}