Example usage for com.google.gson JsonPrimitive JsonPrimitive

List of usage examples for com.google.gson JsonPrimitive JsonPrimitive

Introduction

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

Prototype

public JsonPrimitive(Character c) 

Source Link

Document

Create a primitive containing a character.

Usage

From source file:be.iminds.iot.dianne.nn.util.DianneJSONRPCRequestFactory.java

License:Open Source License

public static JsonObject createDeployRequest(int id, String nnName) {
    JsonObject request = new JsonObject();
    request.add("jsonrpc", new JsonPrimitive("2.0"));
    request.add("method", new JsonPrimitive("deploy"));
    request.add("id", new JsonPrimitive(id));

    JsonArray params = new JsonArray();
    params.add(new JsonPrimitive(nnName));
    request.add("params", params);

    return request;
}

From source file:be.iminds.iot.dianne.nn.util.DianneJSONRPCRequestFactory.java

License:Open Source License

public static JsonObject createUndeployRequest(int id, String nnId) {
    JsonObject request = new JsonObject();
    request.add("jsonrpc", new JsonPrimitive("2.0"));
    request.add("method", new JsonPrimitive("undeploy"));
    request.add("id", new JsonPrimitive(id));

    JsonArray params = new JsonArray();
    params.add(new JsonPrimitive(nnId));
    request.add("params", params);

    return request;
}

From source file:be.iminds.iot.dianne.nn.util.DianneJSONRPCRequestFactory.java

License:Open Source License

public static JsonObject createForwardRequest(int id, String nnId, int[] dims) {
    JsonObject request = new JsonObject();
    request.add("jsonrpc", new JsonPrimitive("2.0"));
    request.add("method", new JsonPrimitive("forward"));
    request.add("id", new JsonPrimitive(id));

    JsonArray params = new JsonArray();
    params.add(new JsonPrimitive(nnId));

    // create input
    JsonArray input = new JsonArray();
    List<JsonArray> toAdd = new ArrayList<>();
    toAdd.add(input);/*from   w  w w . j ava  2  s . c o  m*/
    for (int i = 0; i < dims.length; i++) {
        List<JsonArray> nextAdd = new ArrayList<>();
        int d = dims[i];
        for (int k = 0; k < d; k++) {
            if (i == dims.length - 1) {
                // add floats
                for (JsonArray a : toAdd) {
                    a.add(new JsonPrimitive(0.0f));
                }
            } else {
                // add jsonarrays
                for (JsonArray a : toAdd) {
                    JsonArray newArray = new JsonArray();
                    a.add(newArray);
                    nextAdd.add(newArray);
                }
            }
        }
        toAdd = nextAdd;
    }
    params.add(input);

    request.add("params", params);

    return request;
}

From source file:be.iminds.iot.dianne.nn.util.DianneJSONRPCRequestFactory.java

License:Open Source License

private static JsonObject createJsonFromMap(Map<String, String> map) {
    JsonObject json = new JsonObject();
    for (Entry<String, String> e : map.entrySet()) {
        json.add(e.getKey(), new JsonPrimitive(e.getValue()));
    }//from  ww  w. j a va  2s. c  o  m
    return json;
}

From source file:beans.TripleStoreBean.java

public static String answerLiteqQuery(String query, boolean useCache) {
    JsonObject response = new JsonObject();
    Gson gson = new Gson();

    if (useCache) {
        String cachedResult = CacheBean.getCachedLiteqQueryResult(query.hashCode());
        if (cachedResult != null) {
            Logger.getLogger(TripleStoreBean.class.getName()).log(Level.INFO, "returning cached result");
            return cachedResult;
        }//from   w  ww .j  a  va2s .c  om
    }
    JsonElement result = null;
    JsonArray values;

    ResultSet rs;
    Connection conn = null;
    try {
        conn = getTripleStoreConnection();
        Statement stmt = conn.createStatement();

        boolean more = stmt.execute(query);
        ResultSetMetaData data = stmt.getResultSet().getMetaData();

        if (data.getColumnCount() == 1) {
            result = new JsonArray();
        } else {
            result = new JsonObject();
        }

        while (more) {
            rs = stmt.getResultSet();
            while (rs.next()) {
                if (data.getColumnCount() > 1) {
                    String key = convertToIRI(rs.getObject(1));
                    if (key == null) {
                        key = rs.getString(1);
                    }
                    String value = convertToIRI(rs.getObject(2));
                    if (value == null) {
                        value = rs.getString(2);
                    }
                    if (((JsonObject) result).has(key)) {
                        values = ((JsonObject) result).get(key).getAsJsonArray();
                        values.add(new JsonPrimitive(value));
                    } else {
                        values = new JsonArray();
                        values.add(new JsonPrimitive(value));
                        ((JsonObject) result).add(key, values);
                    }
                } else if (data.getColumnCount() == 1) {
                    String key = convertToIRI(rs.getObject(1));
                    if (key == null) {
                        key = rs.getString(1);
                    }
                    ((JsonArray) result).add(new JsonPrimitive(key));
                }
            }
            more = stmt.getMoreResults();
        }
        response.add("response", result);

        if (useCache) {
            CacheBean.storeResponseInCache(gson.toJson(response), query.hashCode());
        }
    } catch (SQLException ex) {
        Logger.getLogger(TripleStoreBean.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(TripleStoreBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return gson.toJson(response);
}

From source file:bind.JsonTreeReader.java

License:Apache License

public void promoteNameToValue() throws IOException {
    expect(JsonToken.NAME);//from  w ww  .j  a  v a2 s.c om
    Iterator<?> i = (Iterator<?>) peekStack();
    Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
    stack.add(entry.getValue());
    stack.add(new JsonPrimitive((String) entry.getKey()));
}

From source file:bind.JsonTreeWriter.java

License:Apache License

@Override
public JsonWriter value(String value) throws IOException {
    if (value == null) {
        return nullValue();
    }//from ww w.j  a  v a 2  s.  c  om
    put(new JsonPrimitive(value));
    return this;
}

From source file:bind.JsonTreeWriter.java

License:Apache License

@Override
public JsonWriter value(boolean value) throws IOException {
    put(new JsonPrimitive(value));
    return this;
}

From source file:bind.JsonTreeWriter.java

License:Apache License

@Override
public JsonWriter value(double value) throws IOException {
    if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {
        throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
    }//from   w w  w. j a v  a 2 s.co  m
    put(new JsonPrimitive(value));
    return this;
}

From source file:bind.JsonTreeWriter.java

License:Apache License

@Override
public JsonWriter value(long value) throws IOException {
    put(new JsonPrimitive(value));
    return this;
}