Example usage for com.google.gson JsonObject toString

List of usage examples for com.google.gson JsonObject toString

Introduction

In this page you can find the example usage for com.google.gson JsonObject toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:be.iminds.iot.dianne.jsonrpc.DianneSSEServlet.java

License:Open Source License

@Override
public void handleEvent(Event event) {
    // construct server sent event
    JsonObject data = new JsonObject();

    if (event.getTopic().contains("progress")) {
        // progress
        data.add("type", new JsonPrimitive("progress"));
        data.add("jobId", new JsonPrimitive(event.getProperty("jobId").toString()));

        if (event.containsProperty("iteration")) {
            data.add("iteration", new JsonPrimitive((Long) event.getProperty("iteration")));
        }//from  w w w. j  ava  2s . co m

        if (event.containsProperty("minibatchLoss")) {
            data.add("minibatchLoss", new JsonPrimitive((Float) event.getProperty("minibatchLoss")));
        }

        if (event.containsProperty("validationLoss")) {
            data.add("validationLoss", new JsonPrimitive((Float) event.getProperty("validationLoss")));
        }

        if (event.containsProperty("q")) {
            data.add("q", new JsonPrimitive((Float) event.getProperty("q")));
        }

        if (event.containsProperty("reward")) {
            data.add("reward", new JsonPrimitive((Float) event.getProperty("reward")));
        }

        if (event.containsProperty("sequence")) {
            data.add("sequence", new JsonPrimitive((Long) event.getProperty("sequence")));
        }
    } else {
        // notification
        if (event.containsProperty("jobId"))
            data.add("jobId", new JsonPrimitive(event.getProperty("jobId").toString()));
        data.add("type", new JsonPrimitive("notification"));
        data.add("message", new JsonPrimitive((String) event.getProperty("message")));
        data.add("level", new JsonPrimitive(event.getProperty("level").toString()));
        long timestamp = (Long) event.getProperty("timestamp");
        data.add("timestamp", new JsonPrimitive(timestamp));
    }

    StringBuilder builder = new StringBuilder();
    builder.append("data: ").append(data.toString()).append("\n\n");
    String sse = builder.toString();

    // send to all clients
    Iterator<Entry<String, AsyncContext>> it = clients.entrySet().iterator();
    while (it.hasNext()) {
        AsyncContext client = it.next().getValue();
        try {
            PrintWriter writer = client.getResponse().getWriter();
            writer.write(sse);
            writer.flush();
        } catch (Exception e) {
            it.remove();
        }
    }
}

From source file:beans.cart.Item.java

public String toStringJson() {
    JsonObject json = new JsonObject();

    json.addProperty("id", id);
    json.addProperty("quantity", quantity);
    json.addProperty("title", title);
    json.addProperty("price", price);
    json.addProperty("description", description);

    return json.toString();
}

From source file:blockplus.exports.ContextRepresentation.java

License:Open Source License

@Override
public String toString() {
    final JsonObject data = new JsonObject();
    data.addProperty("color", this.context.side().toString());
    data.addProperty("isTerminal", this.context.isTerminal());
    data.add("board", this.encodeBoard());
    data.add("pieces", this.encodePieces());
    data.add("options", this.encodeOptions());
    return data.toString();
}

From source file:blockplus.transport.BlockplusServer.java

License:Open Source License

@Subscribe
@AllowConcurrentEvents//from  www. j  a v a  2 s. co  m
public void onInPatio(final IInPatio inPatio) {
    final IEndPoint endPoint = inPatio.getEndpoint();
    final JsonObject tables = this.games();
    endPoint.emit("tables", tables.toString());
    for (final IEndPoint other : this.endpointsInPatio) {
        other.emit("tables", tables.toString());
    }
    this.endpointsInPatio.add(endPoint);
}

From source file:blockplus.transport.BlockplusServerEvents.java

License:Open Source License

@Subscribe
@AllowConcurrentEvents/*from  w w  w.j av a 2 s. co m*/
public void onGameConnection(final IGameConnection gameConnection) {
    final IGame<Context> game = this.getServer().getGame(gameConnection.getOrdinal());
    if (!game.isFull()) {
        final IClient oldClient = this.getServer().getClientByEndpoint(gameConnection.getEndpoint());
        final IClient newClient = new Client(gameConnection.getEndpoint(), oldClient.getName(),
                game.getOrdinal());
        this.getServer().updateClients(newClient.getEndpoint(), newClient);

        final BlockplusGame newGame = (BlockplusGame) game.connect(newClient);

        final ImmutableList<IClient> clients = newGame.getClients();
        this.getServer().updateGame(newGame.getOrdinal(), newGame);

        final JsonObject gameInfo = new JsonObject();
        gameInfo.addProperty("id", newGame.getOrdinal());
        gameInfo.addProperty("players", clients.size());

        newClient.getEndpoint().emit("game", gameInfo.toString());

        final JsonObject playerInfo = new JsonObject();
        playerInfo.addProperty("name", newClient.getName());

        for (final IClient client : clients) {
            client.getEndpoint().emit("player", playerInfo.toString());
        }
        this.getServer().removeFromPatio(newClient.getEndpoint());
        for (final IEndPoint endPoint : this.getServer().getEndpointsInPatio()) {
            endPoint.emit("tables", this.getServer().games().toString());
        }
        if (newGame.isFull() && !newGame.isPaused())
            newGame.update();
    }
}

From source file:blockplus.transport.BlockplusServerEvents.java

License:Open Source License

@Subscribe
@AllowConcurrentEvents/*from   w w  w. jav a 2s .c  o  m*/
public void onNotification(final INotification notificationInterface) {
    final IClient client = this.getServer().getClientByEndpoint(notificationInterface.getEndpoint());
    final Integer game = client.getGame();
    final BlockplusGame blockplusGame = (BlockplusGame) this.getServer().getGame(game);
    final Colors from = Colors.valueOf(notificationInterface.getFrom());
    final Colors to = Colors.valueOf(notificationInterface.getTo());
    final String message = notificationInterface.getMessage();
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("message", message);
    jsonObject.addProperty("from", from.toString());
    jsonObject.addProperty("to", to.toString());
    final IClient toClient = blockplusGame.getPlayer(to);
    toClient.getEndpoint().emit("notification", jsonObject.toString());
}

From source file:blockplus.transport.events.Client.java

License:Open Source License

@Override
public String toString() {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("type", this.getClass().getSimpleName());
    final JsonObject data = new JsonObject();
    data.addProperty("name", this.getName());
    data.addProperty("game", this.getGame().toString());
    data.addProperty("io", this.getEndpoint().toString());
    jsonObject.add("data", data);
    return jsonObject.toString();
}

From source file:blockplus.transport.messages.Client.java

License:Open Source License

@Override
public String toString() {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("type", this.getType());
    jsonObject.add("data", this.getData());
    return jsonObject.toString();
}

From source file:br.com.thiaguten.contrib.JsonContrib.java

License:Apache License

/**
 * Appends key and/or value to json//from   w  w  w.  j  a  v a2 s .co m
 *
 * @param json  string json which will be modified
 * @param key   key that will be appended
 * @param value value that will be appended
 * @return the specified JSON string with appended key and/or value
 */
public static String appendValueToJson(String json, String key, Object value) {
    JsonElement jsonElement = parseObject(json);

    if (jsonElement != null) {
        if (jsonElement.isJsonPrimitive()) {
            JsonArray jsonArray = new JsonArray();
            jsonArray.add(jsonElement);
            jsonArray.add(objectToJsonElementTree(value));
            return jsonArray.toString();
        } else if (jsonElement.isJsonObject()) {
            if (key == null) {
                throw new IllegalArgumentException(
                        "to append some value into a JsonObject the 'key' parameter must not be null");
            }
            JsonObject jsonObject = (JsonObject) jsonElement;
            jsonObject.add(key, objectToJsonElementTree(value));
            return jsonObject.toString();
        } else if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = (JsonArray) jsonElement;
            jsonArray.add(objectToJsonElementTree(value));
            return jsonArray.toString();
        }
    }
    return getJsonElementAsString(jsonElement);
}

From source file:br.com.thiaguten.contrib.JsonContrib.java

License:Apache License

/**
 * Converts the object to json string without the complexity
 * of relational hierarchy between them, creating a json "flatten".
 *
 * @param object object you wish to converts into a json flatten
 * @return flattened json/*from  w w  w .j  a  v  a2s  .  c o m*/
 */
public static String plainJson(Object object) {
    JsonElement jsonElement = parseObject(object);

    if (jsonElement != null) {
        if (jsonElement.isJsonObject()) {
            JsonObject jsonObjectFlattened = new JsonObject();
            deepJsonObjectSearchToJsonObjectFlattened((JsonObject) jsonElement, jsonObjectFlattened);
            return jsonObjectFlattened.toString();
        } else if (jsonElement.isJsonArray()) {
            JsonArray jsonArrayFlattened = new JsonArray();
            deepJsonArraySearchToJsonArrayFlattened((JsonArray) jsonElement, jsonArrayFlattened);
            return jsonArrayFlattened.toString();
        }
    }
    return getJsonElementAsString(jsonElement);
}