List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:io.openvidu.client.internal.JsonRoomUtils.java
License:Apache License
public static JsonArray getResponseArray(JsonElement result) { if (!result.isJsonArray()) { throw new OpenViduException(Code.TRANSPORT_RESPONSE_ERROR_CODE, "Invalid response format. The response '" + result + "' should be a Json array"); }/*from w w w.j a va2 s . co m*/ return result.getAsJsonArray(); }
From source file:io.openvidu.client.internal.JsonRoomUtils.java
License:Apache License
@SuppressWarnings("unchecked") private static <T> T getConverted(JsonElement paramValue, String property, Class<T> type, boolean allowNull) { if (paramValue == null) { if (allowNull) { return null; } else {/*ww w .jav a 2s . c om*/ throw new OpenViduException(Code.TRANSPORT_ERROR_CODE, "Invalid method lacking parameter '" + property + "'"); } } if (type == String.class) { if (paramValue.isJsonPrimitive()) { return (T) paramValue.getAsString(); } } if (type == Integer.class) { if (paramValue.isJsonPrimitive()) { return (T) Integer.valueOf(paramValue.getAsInt()); } } if (type == JsonArray.class) { if (paramValue.isJsonArray()) { return (T) paramValue.getAsJsonArray(); } } throw new OpenViduException(Code.TRANSPORT_ERROR_CODE, "Param '" + property + "' with value '" + paramValue + "' is not a " + type.getName()); }
From source file:io.robusta.rra.representation.implementation.GsonRepresentation.java
License:Apache License
/** * throw an exception if the jsonElement is not an array * /*w w w . j a va2s.co m*/ * @param elt * @throws RepresentationException */ protected void throwIfNotArray(JsonElement elt) throws RepresentationException { if (!elt.isJsonArray()) { throw new RepresentationException("The current element is not a JSON array but a " + this.getTypeof() + " and it can't add an object the correct way"); } }
From source file:io.soliton.protobuf.json.JsonRpcRequest.java
License:Apache License
public static JsonRpcRequest fromJson(JsonElement root) throws JsonRpcError { if (!root.isJsonObject()) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "Received payload is not a JSON Object"); }/*from w w w. ja v a 2 s . c o m*/ JsonObject request = root.getAsJsonObject(); JsonElement id = request.get(JsonRpcProtocol.ID); JsonElement methodNameElement = request.get(JsonRpcProtocol.METHOD); JsonElement paramsElement = request.get(JsonRpcProtocol.PARAMETERS); if (id == null) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "Malformed request, missing 'id' property"); } if (methodNameElement == null) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "Malformed request, missing 'method' property"); } if (paramsElement == null) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "Malformed request, missing 'params' property"); } if (!methodNameElement.isJsonPrimitive()) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "Method name is not a JSON primitive"); } JsonPrimitive methodName = methodNameElement.getAsJsonPrimitive(); if (!methodName.isString()) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "Method name is not a string"); } if (!paramsElement.isJsonArray()) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "'params' property is not an array"); } JsonArray params = paramsElement.getAsJsonArray(); if (params.size() != 1) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "'params' property is not an array"); } JsonElement paramElement = params.get(0); if (!paramElement.isJsonObject()) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "Parameter is not an object"); } JsonObject parameter = paramElement.getAsJsonObject(); List<String> serviceAndMethod = Lists.newArrayList(DOT_SPLITTER.split(methodName.getAsString())); String methodNameString = methodName.getAsString(); int dotIndex = methodNameString.lastIndexOf('.'); if (dotIndex < 0) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "'method' property is not properly formatted"); } if (dotIndex == methodNameString.length() - 1) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "'method' property is not properly formatted"); } String service = methodNameString.substring(0, dotIndex); String method = methodNameString.substring(dotIndex + 1); if (service.isEmpty() || method.isEmpty()) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "'method' property is not properly formatted"); } if (serviceAndMethod.size() < 2) { throw new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "'method' property is not properly formatted"); } return new JsonRpcRequest(service, method, id, parameter); }
From source file:io.thekraken.json2hive.HiveUtils.java
License:Apache License
public static String struct(String value, int nasted) { if (value == null) return " "; if (value.isEmpty()) return " "; if (nasted < 0) nasted = 0;/* w w w . j av a2 s . c o m*/ JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(value); JsonObject jsonObject = jsonElement.getAsJsonObject(); String struct = ""; COMMA = 0; for (Entry<String, JsonElement> entry : jsonObject.entrySet()) { String key = entry.getKey(); JsonElement val = entry.getValue(); // 3 Possibilities // 1. structures // 2. array // 3. primitive if (val.isJsonObject() && nasted >= 1) { if (COMMA > 0) { struct += ","; } COMMA = 0; struct += fieldWrapper(key) + ":" + STRUCT + "<" + struct(val.toString(), 1) + ">"; } // Array else if (val.isJsonArray()) { if (COMMA > 0) { struct += ","; } COMMA++; struct += fieldWrapper(key) + ":" + ARRAY + "<" + array(val.toString()) + ">"; } else { // normal field if (COMMA > 0) { struct += ","; } COMMA++; if (val.isJsonNull()) { struct += fieldWrapper(key) + ":" + STRING; } else { struct += fieldWrapper(key) + ":" + findType(val.toString()); } } } return struct; }
From source file:io.thekraken.json2hive.Json.java
License:Apache License
private static HiveTable createHiveTable(String hiveTableName, String jsonObject) { HiveTable hive = new HiveTable(hiveTableName); JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(jsonObject); JsonObject jsonObj = jsonElement.getAsJsonObject(); for (Entry<String, JsonElement> entry : jsonObj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); /** Structure */ if (value.isJsonObject()) { hive.addStructure(key, value.toString()); }/*from w w w . j a va 2 s . c o m*/ /** Array */ else if (value.isJsonArray()) { hive.addArray(key, value.toString()); } else { /** primitive */ if (value.isJsonNull()) { hive.AddUnknow(value.toString()); } else { hive.addPrimitive(key, value.toString()); } } } hive.close(); return hive; }
From source file:io.typefox.lsapi.services.json.MessageJsonHandler.java
License:Open Source License
protected ResponseMessageImpl parseResponse(final JsonObject json, final String responseId) { if ((this.responseMethodResolver == null)) { throw new IllegalStateException("Response methods are not accepted."); }//w w w . java 2 s.c o m try { final ResponseMessageImpl result = new ResponseMessageImpl(); result.setId(responseId); final JsonElement resultElem = json.get("result"); if ((resultElem != null)) { final String method = this.responseMethodResolver.apply(responseId); if ((method != null)) { final Class<?> resultType = MessageJsonHandler.RESPONSE_RESULT_TYPES.get(method); if ((resultType != null)) { boolean _isJsonArray = resultElem.isJsonArray(); if (_isJsonArray) { final JsonArray arrayElem = resultElem.getAsJsonArray(); int _size = arrayElem.size(); final ArrayList<Object> list = Lists.<Object>newArrayListWithExpectedSize(_size); for (final JsonElement e : arrayElem) { Object _fromJson = this.gson.fromJson(e, resultType); list.add(_fromJson); } result.setResult(list); } else { Object _fromJson_1 = this.gson.fromJson(resultElem, resultType); result.setResult(_fromJson_1); } } } } else { JsonElement _get = json.get("error"); JsonObject _asJsonObject = null; if (_get != null) { _asJsonObject = _get.getAsJsonObject(); } final JsonObject error = _asJsonObject; if ((error != null)) { ResponseErrorImpl _fromJson_2 = this.gson.<ResponseErrorImpl>fromJson(error, ResponseErrorImpl.class); result.setError(_fromJson_2); } } return result; } catch (final Throwable _t) { if (_t instanceof Exception) { final Exception e_1 = (Exception) _t; String _message = e_1.getMessage(); String _plus = ("Could not parse response: " + _message); throw new InvalidMessageException(_plus, responseId, e_1); } else { throw Exceptions.sneakyThrow(_t); } } }
From source file:io.vertigo.ccc.console.JSonBeautifier.java
License:Apache License
private static void beautify(final StringBuilder sb, final JsonElement jsonElement, final int inOffset) { int offset = inOffset; if (jsonElement.isJsonArray()) { offset++;/*from w w w . j a v a2 s. c o m*/ for (Iterator<JsonElement> it = jsonElement.getAsJsonArray().iterator(); it.hasNext();) { //sb.append("- "); beautify(sb, it.next(), offset); } offset--; } else if (jsonElement.isJsonObject()) { for (Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) { appendSpaces(sb, offset); sb.append(entry.getKey()); sb.append(" : "); if (entry.getValue().isJsonPrimitive()) { sb.append(entry.getValue()); appendCRLF(sb); } else { offset++; appendCRLF(sb); beautify(sb, entry.getValue(), offset); offset--; } } } else if (jsonElement.isJsonPrimitive()) { // appendSpaces(sb, offset); String s = jsonElement.toString(); if (s.startsWith("\"") && s.endsWith("\"")) { sb.append(s.substring(1, s.length() - 1)); } else { sb.append(s); } appendCRLF(sb); } else { sb.append("???"); //null } }
From source file:io.vertigo.shell.util.JSonBeautifier.java
License:Apache License
private static void beautify(final StringBuilder sb, final JsonElement jsonElement, final int inOffset) { int offset = inOffset; if (jsonElement.isJsonArray()) { offset++;/*from w ww. j a v a 2s . c o m*/ for (final JsonElement jsonElement2 : jsonElement.getAsJsonArray()) { //sb.append("- "); beautify(sb, jsonElement2, offset); } offset--; } else if (jsonElement.isJsonObject()) { for (final Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) { appendSpaces(sb, offset); sb.append(entry.getKey()); sb.append(" : "); if (entry.getValue().isJsonPrimitive()) { sb.append(entry.getValue()); appendCRLF(sb); } else { offset++; appendCRLF(sb); beautify(sb, entry.getValue(), offset); offset--; } } } else if (jsonElement.isJsonPrimitive()) { // appendSpaces(sb, offset); final String s = jsonElement.toString(); if (s.startsWith("\"") && s.endsWith("\"")) { sb.append(s.substring(1, s.length() - 1)); } else { sb.append(s); } appendCRLF(sb); } else { sb.append("???"); //null } }
From source file:io.vertigo.vega.engines.webservice.json.GoogleJsonEngine.java
License:Apache License
private void filterFields(final JsonElement jsonElement, final Set<String> includedFields, final Set<String> excludedFields) { if (jsonElement.isJsonArray()) { final JsonArray jsonArray = jsonElement.getAsJsonArray(); for (final JsonElement jsonSubElement : jsonArray) { filterFields(jsonSubElement, includedFields, excludedFields); }/*from w w w . ja va 2s.c o m*/ } else if (jsonElement.isJsonObject()) { final JsonObject jsonObject = jsonElement.getAsJsonObject(); for (final String excludedField : excludedFields) { jsonObject.remove(excludedField); } if (!includedFields.isEmpty()) { final Set<String> notIncludedFields = new HashSet<>(); for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) { if (!includedFields.contains(entry.getKey())) { notIncludedFields.add(entry.getKey()); } } for (final String notIncludedField : notIncludedFields) { jsonObject.remove(notIncludedField); } } } //else Primitive : no exclude }