List of usage examples for com.google.gson JsonParser parse
@Deprecated public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException
From source file:club.jmint.crossing.client.CrossingClient.java
License:Apache License
public CallResult serviceCall(String inf, JsonObject params, boolean isEncrypt) { String result = null;//from ww w.jav a2s.co m CallResult cr = new CallResult(); try { if (isEncrypt) { result = call(inf, params.toString(), true); } else { result = call(inf, params.toString()); } } catch (CrossException e) { CrossLog.printStackTrace(e); CrossLog.logger.error("service call failed."); return null; } JsonParser jp = new JsonParser(); JsonObject jo; try { jo = (JsonObject) jp.parse(result); } catch (JsonSyntaxException e) { CrossLog.printStackTrace(e); CrossLog.logger.error("service call failed."); return null; } cr.errorCode = jo.getAsJsonPrimitive("errorCode").getAsInt(); if (cr.errorCode == ErrorCode.SUCCESS.getCode()) { cr.isSuccess = true; } else { cr.isSuccess = false; } cr.errorInfo = jo.getAsJsonPrimitive("errorInfo").getAsString(); if (jo.has("params")) { cr.data = jo.getAsJsonObject("params"); } return cr; }
From source file:club.jmint.crossing.specs.CrossingService.java
License:Apache License
protected JsonObject parseInputParams(String params, boolean encrypt) throws CrossException { //parsing// ww w .j av a 2s.c om JsonParser jp = new JsonParser(); JsonObject jo, jode, joparams; try { jo = (JsonObject) jp.parse(params); } catch (JsonSyntaxException e) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } String encrypted, jsonParams; if (encrypt) { if (!jo.has("encrypted")) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(), ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo()); } encrypted = jo.getAsJsonPrimitive("encrypted").getAsString(); jsonParams = buildDecrypt(encrypted); if (jsonParams == null) { throw new CrossException(ErrorCode.COMMON_ERR_DECRYPTION.getCode(), ErrorCode.COMMON_ERR_DECRYPTION.getInfo()); } try { jode = (JsonObject) jp.parse(jsonParams); } catch (JsonSyntaxException e) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } jo = jode; } //Check signature if (!jo.has("sign")) { throw new CrossException(ErrorCode.COMMON_ERR_SIGN_MISSING.getCode(), ErrorCode.COMMON_ERR_SIGN_MISSING.getInfo()); } String signValue = jo.getAsJsonPrimitive("sign").getAsString(); if (!jo.has("params")) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(), ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo()); } String inputparams = jo.getAsJsonObject("params").toString(); String signCheck = buildSign(inputparams, signKey); if (!signCheck.equals(signValue)) { throw new CrossException(ErrorCode.COMMON_ERR_SIGN_BAD.getCode(), ErrorCode.COMMON_ERR_SIGN_BAD.getInfo()); } try { joparams = (JsonObject) jp.parse(inputparams); } catch (JsonSyntaxException e) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } return joparams; }
From source file:club.jmint.crossing.specs.ParamBuilder.java
License:Apache License
public static String getErrorInfo(String jsonParams) throws CrossException { JsonParser jp = new JsonParser(); JsonObject jo;/*w ww .j av a 2 s .com*/ try { jo = (JsonObject) jp.parse(jsonParams); } catch (JsonSyntaxException e) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } if (jo.has("errorInfo")) { String value = jo.getAsJsonPrimitive("errorInfo").getAsString(); return value; } else { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(), ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo()); } }
From source file:club.jmint.crossing.specs.ParamBuilder.java
License:Apache License
public static int getErrorCode(String jsonParams) throws CrossException { JsonParser jp = new JsonParser(); JsonObject jo;/*w ww .j ava2 s . c om*/ try { jo = (JsonObject) jp.parse(jsonParams); } catch (JsonSyntaxException e) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } if (jo.has("errorCode")) { String value = jo.getAsJsonPrimitive("errorCode").getAsString(); return Integer.parseInt(value); } else { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(), ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo()); } }
From source file:club.jmint.crossing.specs.ParamBuilder.java
License:Apache License
public static JsonObject getParams(String jsonParams) throws CrossException { JsonParser jp = new JsonParser(); JsonObject jo;/* w ww . j a v a2s . co m*/ try { jo = (JsonObject) jp.parse(jsonParams); } catch (JsonSyntaxException e) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } if (jo.has("params")) { return jo.getAsJsonObject("params"); } else { return null; } }
From source file:club.jmint.crossing.specs.ParamBuilder.java
License:Apache License
public static String buildSignedParams(String p, String signKey) throws CrossException { String md5Value = Security.crossingSign(p, signKey, ""); JsonObject jo = new JsonObject(); jo.addProperty("sign", md5Value); JsonParser jp = new JsonParser(); JsonElement jop = null;/*from ww w. j av a 2s . com*/ try { jop = jp.parse(p); if (!jop.isJsonObject()) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } } catch (JsonSyntaxException jse) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } jo.add("params", jop); return jo.toString(); }
From source file:club.jmint.crossing.specs.ParamBuilder.java
License:Apache License
public static String checkSignAndRemove(String p, String signKey) throws CrossException { JsonParser jp = new JsonParser(); JsonElement jop = null;//from w w w. j a va 2 s . co m try { jop = jp.parse(p); if (!jop.isJsonObject()) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } } catch (JsonSyntaxException jse) { //jse.printStackTrace(); throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } JsonObject jo = (JsonObject) jop; String np = null; if (jo.has("sign") || jo.has("params")) { String sign = jo.getAsJsonPrimitive("sign").getAsString(); np = jo.getAsJsonObject("params").toString(); String signValue = Security.crossingSign(np, signKey, ""); if (!sign.equals(signValue)) { throw new CrossException(ErrorCode.COMMON_ERR_SIGN_BAD.getCode(), ErrorCode.COMMON_ERR_SIGN_BAD.getInfo()); } //remove sign field only and return Iterator<Entry<String, JsonElement>> it = jo.entrySet().iterator(); JsonObject rjo = new JsonObject(); Entry<String, JsonElement> en; while (it.hasNext()) { en = it.next(); if (!en.getKey().equals("sign")) { rjo.add(en.getKey(), en.getValue()); } } return rjo.toString(); } return p; }
From source file:club.jmint.crossing.specs.ParamBuilder.java
License:Apache License
public static String buildDecryptedParams(String encryptParams, String decryptKey) throws CrossException { JsonParser jp = new JsonParser(); JsonElement jop = null;//from ww w . ja va2 s .co m try { jop = jp.parse(encryptParams); if (!jop.isJsonObject()) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } } catch (JsonSyntaxException jse) { //jse.printStackTrace(); throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } JsonObject jo = (JsonObject) jop; String np = null; if (jo.has("encrypted")) { String encrypted = jo.getAsJsonPrimitive("encrypted").getAsString(); String paramsValue = Security.crossingDecrypt(encrypted, decryptKey, "DES"); int ec = jo.getAsJsonPrimitive("errorCode").getAsInt(); String ed = jo.getAsJsonPrimitive("errorInfo").getAsString(); JsonParser jpr = new JsonParser(); JsonObject jor = null; try { jor = (JsonObject) jpr.parse(paramsValue); } catch (JsonSyntaxException je) { throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); } jor.addProperty("errorCode", ec); jor.addProperty("errorInfo", ed); np = jor.toString(); return np; } return encryptParams; }
From source file:club.jmint.mifty.service.MiftyService.java
License:Apache License
public String callProxy(String method, String params, boolean isEncrypt) throws TException { //parse parameters and verify signature CrossLog.logger.debug("callProxy: " + method + "(in: " + params + ")"); JsonObject ip;//from w w w. j a v a 2 s . co m try { ip = parseInputParams(params, isEncrypt); } catch (CrossException ce) { return buildOutputByCrossException(ce); } //extract all parameters HashMap<String, String> inputMap = new HashMap<String, String>(); Iterator<Entry<String, JsonElement>> it = ip.entrySet().iterator(); Entry<String, JsonElement> en = null; String key; JsonElement je; while (it.hasNext()) { en = it.next(); key = en.getKey(); je = en.getValue(); if (je.isJsonArray()) { inputMap.put(key, je.getAsJsonArray().toString()); //System.out.println(je.getAsJsonArray().toString()); } else if (je.isJsonNull()) { inputMap.put(key, je.getAsJsonNull().toString()); //System.out.println(je.getAsJsonNull().toString()); } else if (je.isJsonObject()) { inputMap.put(key, je.getAsJsonObject().toString()); //System.out.println(je.getAsJsonObject().toString()); } else if (je.isJsonPrimitive()) { inputMap.put(key, je.getAsJsonPrimitive().getAsString()); //System.out.println(je.getAsJsonPrimitive().toString()); } else { //unkown type; } } //execute specific method Method[] ma = this.getClass().getMethods(); int idx = 0; for (int i = 0; i < ma.length; i++) { if (ma[i].getName().equals(method)) { idx = i; break; } } HashMap<String, String> outputMap = null; try { Method m = this.getClass().getDeclaredMethod(method, ma[idx].getParameterTypes()); outputMap = (HashMap<String, String>) m.invoke(this, inputMap); CrossLog.logger.debug("callProxy: " + method + "() executed."); } catch (NoSuchMethodException nsm) { CrossLog.logger.error("callProxy: " + method + "() not found."); CrossLog.printStackTrace(nsm); return buildOutputError(ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getCode(), ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getInfo()); } catch (Exception e) { CrossLog.logger.error("callProxy: " + method + "() executed with exception."); CrossLog.printStackTrace(e); if (e instanceof CrossException) { return buildOutputByCrossException((CrossException) e); } else { return buildOutputError(ErrorCode.COMMON_ERR_UNKOWN.getCode(), ErrorCode.COMMON_ERR_UNKOWN.getInfo()); } } //if got error then return immediately String ec = outputMap.get("errorCode"); String ecInfo = outputMap.get("errorInfo"); if ((ec != null) && (!ec.isEmpty())) { return buildOutputError(Integer.parseInt(ec), ecInfo); } //build response parameters JsonObject op = new JsonObject(); Iterator<Entry<String, String>> ito = outputMap.entrySet().iterator(); Entry<String, String> eno = null; JsonParser jpo = new JsonParser(); JsonElement jeo = null; while (ito.hasNext()) { eno = ito.next(); try { jeo = jpo.parse(eno.getValue()); } catch (JsonSyntaxException e) { // MyLog.logger.error("callProxy: Malformed output parameters["+eno.getKey()+"]."); // return buildOutputError(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), // ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); //we do assume that it should be a common string jeo = new JsonPrimitive(eno.getValue()); } op.add(eno.getKey(), jeo); } String output = null; try { output = buildOutputParams(op, isEncrypt); } catch (CrossException ce) { return buildOutputByCrossException(ce); } CrossLog.logger.debug("callProxy: " + method + "(out: " + output + ")"); return output; }
From source file:co.aurasphere.botmill.rasa.service.RasaService.java
License:Open Source License
/** * Send train file request./*from www . ja va 2s . c o m*/ * * @param jsonFile the json file * @return the training response */ public static TrainingResponse sendTrainFileRequest(File jsonFile) { JsonParser parser = new JsonParser(); Object obj; try { obj = parser.parse(new FileReader(jsonFile)); JsonObject jsonObject = (JsonObject) obj; TrainingResponse resp = RasaService.sendTrainRequest(jsonObject.toString()); return resp; } catch (Exception e) { e.printStackTrace(); } return null; }