List of usage examples for com.google.gson JsonPrimitive getAsString
@Override
public String getAsString()
From source file:org.immutables.mongo.fixture.holder.HolderJsonSerializer.java
License:Apache License
@Override public Holder deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject root = (JsonObject) json; ImmutableHolder.Builder builder = ImmutableHolder.builder(); if (root.has("id")) { builder.id(root.get("id").getAsString()); }/*w w w . j av a2 s . co m*/ JsonElement value = root.get(VALUE_PROPERTY); if (value == null) { throw new JsonParseException(String.format("%s not found for %s in JSON", VALUE_PROPERTY, type)); } if (value.isJsonObject()) { final String valueTypeName = value.getAsJsonObject().get(Holder.TYPE_PROPERTY).getAsString(); try { Class<?> valueType = Class.forName(valueTypeName); builder.value(context.deserialize(value, valueType)); } catch (ClassNotFoundException e) { throw new JsonParseException( String.format("Couldn't construct value class %s for %s", valueTypeName, type), e); } } else if (value.isJsonPrimitive()) { final JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isString()) { builder.value(primitive.getAsString()); } else if (primitive.isNumber()) { builder.value(primitive.getAsInt()); } else if (primitive.isBoolean()) { builder.value(primitive.getAsBoolean()); } } else { throw new JsonParseException(String.format("Couldn't deserialize %s : %s. Not a primitive or object", VALUE_PROPERTY, value)); } return builder.build(); }
From source file:org.jboss.aerogear.android.impl.datamanager.SQLStore.java
License:Apache License
private void saveElement(JsonObject serialized, String path, Serializable id) { String sql = String.format( "insert into %s_property (PROPERTY_NAME, PROPERTY_VALUE, PARENT_ID) values (?,?,?)", className); Set<Entry<String, JsonElement>> members = serialized.entrySet(); String pathVar = path.isEmpty() ? "" : "."; for (Entry<String, JsonElement> member : members) { JsonElement jsonValue = member.getValue(); String propertyName = member.getKey(); if (jsonValue.isJsonObject()) { saveElement((JsonObject) jsonValue, path + pathVar + propertyName, id); } else {/* w w w . j av a 2s. co m*/ if (jsonValue.isJsonPrimitive()) { JsonPrimitive primitive = jsonValue.getAsJsonPrimitive(); if (primitive.isBoolean()) { Integer value = primitive.getAsBoolean() ? 1 : 0; database.execSQL(sql, new Object[] { path + pathVar + propertyName, value, id }); } else if (primitive.isNumber()) { Number value = primitive.getAsNumber(); database.execSQL(sql, new Object[] { path + pathVar + propertyName, value, id }); } else if (primitive.isString()) { String value = primitive.getAsString(); database.execSQL(sql, new Object[] { path + pathVar + propertyName, value, id }); } else { throw new IllegalArgumentException(jsonValue + " isn't a number, boolean, or string"); } } else { throw new IllegalArgumentException(jsonValue + " isn't a JsonPrimitive"); } } } }
From source file:org.jboss.aerogear.android.impl.datamanager.SQLStore.java
License:Apache License
private void buildKeyValuePairs(JsonObject where, List<Pair<String, String>> keyValues, String parentPath) { Set<Entry<String, JsonElement>> keys = where.entrySet(); String pathVar = parentPath.isEmpty() ? "" : ".";//Set a dot if parent path is not empty for (Entry<String, JsonElement> entry : keys) { String key = entry.getKey(); String path = parentPath + pathVar + key; JsonElement jsonValue = entry.getValue(); if (jsonValue.isJsonObject()) { buildKeyValuePairs((JsonObject) jsonValue, keyValues, path); } else {//from w w w . j a va 2 s . c o m if (jsonValue.isJsonPrimitive()) { JsonPrimitive primitive = jsonValue.getAsJsonPrimitive(); if (primitive.isBoolean()) { Integer value = primitive.getAsBoolean() ? 1 : 0; keyValues.add(new Pair<String, String>(path, value.toString())); } else if (primitive.isNumber()) { Number value = primitive.getAsNumber(); keyValues.add(new Pair<String, String>(path, value.toString())); } else if (primitive.isString()) { String value = primitive.getAsString(); keyValues.add(new Pair<String, String>(path, value)); } else { throw new IllegalArgumentException(jsonValue + " isn't a number, boolean, or string"); } } else { throw new IllegalArgumentException(jsonValue + " isn't a JsonPrimitive"); } } } }
From source file:org.jboss.aerogear.android.store.sql.SQLStore.java
License:Apache License
private void saveElement(JsonElement serialized, String path, Serializable id) { String sql = String.format( "insert into %s_property (PROPERTY_NAME, PROPERTY_VALUE, PARENT_ID) values (?,?,?)", className); if (serialized.isJsonObject()) { Set<Entry<String, JsonElement>> members = ((JsonObject) serialized).entrySet(); String pathVar = path.isEmpty() ? "" : "."; for (Entry<String, JsonElement> member : members) { JsonElement jsonValue = member.getValue(); String propertyName = member.getKey(); if (jsonValue.isJsonArray()) { JsonArray jsonArray = jsonValue.getAsJsonArray(); for (int index = 0; index < jsonArray.size(); index++) { saveElement(jsonArray.get(index), path + pathVar + propertyName + String.format("[%d]", index), id); }// w w w. jav a 2 s. co m } else { saveElement(jsonValue, path + pathVar + propertyName, id); } } } else if (serialized.isJsonPrimitive()) { JsonPrimitive primitive = serialized.getAsJsonPrimitive(); if (primitive.isBoolean()) { String value = primitive.getAsBoolean() ? "true" : "false"; database.execSQL(sql, new Object[] { path, value, id }); } else if (primitive.isNumber()) { Number value = primitive.getAsNumber(); database.execSQL(sql, new Object[] { path, value, id }); } else if (primitive.isString()) { String value = primitive.getAsString(); database.execSQL(sql, new Object[] { path, value, id }); } else { throw new IllegalArgumentException(serialized + " isn't a number, boolean, or string"); } } else { throw new IllegalArgumentException(serialized + " isn't a JsonObject or JsonPrimitive"); } }
From source file:org.jboss.aerogear.android.store.sql.SQLStore.java
License:Apache License
private void buildKeyValuePairs(JsonObject where, List<Pair<String, String>> keyValues, String parentPath) { Set<Entry<String, JsonElement>> keys = where.entrySet(); String pathVar = parentPath.isEmpty() ? "" : ".";// Set a dot if parent path is not empty for (Entry<String, JsonElement> entry : keys) { String key = entry.getKey(); String path = parentPath + pathVar + key; JsonElement jsonValue = entry.getValue(); if (jsonValue.isJsonObject()) { buildKeyValuePairs((JsonObject) jsonValue, keyValues, path); } else {/* w w w. j av a 2 s .c o m*/ if (jsonValue.isJsonPrimitive()) { JsonPrimitive primitive = jsonValue.getAsJsonPrimitive(); if (primitive.isBoolean()) { String value = primitive.getAsBoolean() ? "true" : "false"; keyValues.add(new Pair<String, String>(path, value)); } else if (primitive.isNumber()) { Number value = primitive.getAsNumber(); keyValues.add(new Pair<String, String>(path, value.toString())); } else if (primitive.isString()) { String value = primitive.getAsString(); keyValues.add(new Pair<String, String>(path, value)); } else { throw new IllegalArgumentException(jsonValue + " isn't a number, boolean, or string"); } } else { throw new IllegalArgumentException(jsonValue + " isn't a JsonPrimitive"); } } } }
From source file:org.jclouds.json.internal.ParseObjectFromElement.java
License:Apache License
public Object apply(JsonElement input) { Object value = null;/*from ww w. j av a 2 s.co m*/ if (input == null || input.isJsonNull()) { value = null; } else if (input.isJsonPrimitive()) { JsonPrimitive primitive = input.getAsJsonPrimitive(); if (primitive.isNumber()) { value = primitive.getAsNumber(); } else if (primitive.isBoolean()) { value = primitive.getAsBoolean(); } else { value = primitive.getAsString(); } } else if (input.isJsonArray()) { value = Lists.newArrayList(Iterables.transform(input.getAsJsonArray(), this)); } else if (input.isJsonObject()) { value = Maps.<String, Object>newLinkedHashMap( Maps.transformValues(JsonObjectAsMap.INSTANCE.apply(input.getAsJsonObject()), this)); } return value; }
From source file:org.jenkinsci.plugins.fod.FoDAPI.java
public Map<String, String> getApplicationList() throws IOException { final String METHOD_NAME = CLASS_NAME + ".getApplicationList"; String endpoint = baseUrl + "/api/v1/Application/?fields=applicationId,applicationName,isMobile&limit=9999"; //TODO make this consistent elsewhere by a global config HttpGet connection = (HttpGet) getHttpUriRequest("GET", endpoint); InputStream is = null;//w ww . j av a2 s . com try { // Get Response HttpResponse response = getHttpClient().execute(connection); is = response.getEntity().getContent(); StringBuffer buffer = collectInputStream(is); JsonArray arr = getDataJsonArray(buffer); Map<String, String> map = new TreeMap<String, String>(); for (int ix = 0; ix < arr.size(); ix++) { JsonElement entity = arr.get(ix); JsonObject obj = entity.getAsJsonObject(); JsonPrimitive name = obj.getAsJsonPrimitive("applicationName"); JsonPrimitive id = obj.getAsJsonPrimitive("applicationID"); JsonPrimitive isMobile = obj.getAsJsonPrimitive("isMobile"); if (map.containsKey(name.getAsString())) { continue; } if (!isMobile.getAsBoolean()) { map.put(name.getAsString(), id.getAsString()); } } return map; } finally { if (is != null) { is.close(); } } }
From source file:org.jenkinsci.plugins.fod.FoDAPI.java
public Map<String, String> getAssessmentTypeListWithRetry() throws IOException { final String METHOD_NAME = CLASS_NAME + ".getReleaseList"; PrintStream out = FodBuilder.getLogger(); if (null == out) { out = System.out;/* w w w . ja v a 2s . c om*/ } int attempts = 0; int maxattempts = 5; Map<String, String> map = new TreeMap<String, String>(); String endpoint = baseUrl + "/api/v1/AssessmentType"; while ((null == map || map.isEmpty()) && (attempts < maxattempts)) { HttpGet connection = (HttpGet) getHttpUriRequest("GET", endpoint); InputStream is = null; try { // Get Response HttpResponse response = getHttpClient().execute(connection); is = response.getEntity().getContent(); StringBuffer buffer = collectInputStream(is); int responseCode = response.getStatusLine().getStatusCode(); out.println(METHOD_NAME + ": calling GET " + endpoint); out.println(METHOD_NAME + ": responseCode = " + responseCode); out.println(METHOD_NAME + ": response = " + buffer); System.out.println(METHOD_NAME + ": called, " + attempts + " previous attempts."); JsonArray arr = getDataJsonArray(buffer); String staticTypeRegex = ".*static.*"; Pattern p = Pattern.compile(staticTypeRegex, Pattern.CASE_INSENSITIVE); for (int ix = 0; ix < arr.size(); ix++) { JsonElement entity = arr.get(ix); JsonObject obj = entity.getAsJsonObject(); JsonPrimitive name = obj.getAsJsonPrimitive("Name"); JsonPrimitive id = obj.getAsJsonPrimitive("AssessmentTypeId"); Matcher m = p.matcher(name.getAsString()); if (map.containsKey(name.getAsString()) && m.matches()) continue; map.put(name.getAsString(), id.getAsString()); } if (!(null == map || map.isEmpty())) { return map; } } finally { attempts++; if (is != null) { is.close(); } } } out.println(METHOD_NAME + ": Unable to refresh assessment types, please contact your Technical Account Manager for assistance. "); return map; }
From source file:org.json.rpc.server.InjectingJsonRpcExecutor.java
License:Apache License
public void execute(JsonRpcServerTransport transport, HttpServletRequest httpReq, HttpServletResponse httpResp, ServletContext servletContext) { if (!locked) { synchronized (handlers) { locked = true;/*from ww w .ja v a 2 s . com*/ } LOG.info("locking executor to avoid modification"); } String uuid = null; String methodName = null; JsonArray params = null; JsonObject resp = new JsonObject(); resp.addProperty("jsonrpc", "2.0"); String errorMessage = null; Integer errorCode = null; String errorData = null; JsonObject req = null; try { String requestData = transport.readRequest(); if (LOG.isDebugEnabled()) { LOG.debug("JSON-RPC >> {}", requestData); } JsonParser parser = new JsonParser(); req = (JsonObject) parser.parse(new StringReader(requestData)); } catch (Throwable t) { errorCode = JsonRpcErrorCodes.PARSE_ERROR_CODE; errorMessage = "unable to parse json-rpc request"; errorData = getStackTrace(t); LOG.warn(errorMessage, t); sendError(transport, resp, errorCode, errorMessage, errorData); return; } try { assert req != null; resp.add("id", req.get("id")); JsonPrimitive uuidPrimitive = req.getAsJsonPrimitive("uuid"); if (uuidPrimitive != null) { uuid = uuidPrimitive.getAsString(); } methodName = req.getAsJsonPrimitive("method").getAsString(); params = (JsonArray) req.get("params"); if (params == null) { params = new JsonArray(); } } catch (Throwable t) { errorCode = JsonRpcErrorCodes.INVALID_REQUEST_ERROR_CODE; errorMessage = "unable to read request"; errorData = getStackTrace(t); LOG.warn(errorMessage, t); sendError(transport, resp, errorCode, errorMessage, errorData); return; } try { JsonElement result = executeMethod(methodName, params, uuid, httpReq, httpResp, servletContext); resp.add("result", result); } catch (Throwable t) { LOG.warn("exception occured while executing : " + methodName, t); if (t instanceof JsonRpcRemoteException) { sendError(transport, resp, (JsonRpcRemoteException) t); return; } errorCode = JsonRpcErrorCodes.getServerError(1); errorMessage = t.getMessage(); errorData = getStackTrace(t); sendError(transport, resp, errorCode, errorMessage, errorData); return; } try { String responseData = resp.toString(); LOG.debug("JSON-RPC result << {}", responseData); transport.writeResponse(responseData); } catch (Exception e) { LOG.warn("unable to write response : " + resp, e); } }
From source file:org.kurento.commons.BasicJsonUtils.java
License:Apache License
private static Object convertValue(JsonElement value) { if (value.isJsonNull()) { return null; } else if (value.isJsonPrimitive()) { JsonPrimitive prim = value.getAsJsonPrimitive(); if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isNumber()) { Number n = prim.getAsNumber(); if (n.doubleValue() == n.intValue()) { return n.intValue(); } else { return n.doubleValue(); }/*w w w. j a v a2 s . c o m*/ } else if (prim.isString()) { return prim.getAsString(); } else { throw new RuntimeException("Unrecognized value: " + value); } } else { return value.toString(); } }