public Object parse(String s) { return JSON.parse(s); }
/** * Parse the json string and returns the the array. */ public static Object[] decodeArray(String json){ return parse(json).asArray(); }
/** * Parse the json. */ default void parseJson(String json) { parseJson(JSON.parse(json)); }
/** * Returns the content of response body as {@link JsonValue}. */ default JsonValue getJson() { return JSON.parse(getContent()); }
/** * Parse the json string and returns the the array. */ public static Map<String,Object> decodeMap(String json){ return parse(json).asMap(); }
/** * Parse the json string and returns the the array. */ public static Object[] decodeArray(Reader json){ return parse(json).asArray(); }
/** * Parse the json string and returns the the array of the given type. */ public static <T> T[] decodeArray(Reader json, Class<T> componentType){ T[] a = (T[])Array.newInstance(componentType, 0); return (T[])Converts.convert(parse(json).asArray(), a.getClass(), null, convertContext); }
/** * Parse the json string and returns the the array of the given type. */ public static <T> T[] decodeArray(String json, Class<T> componentType){ T[] a = (T[])Array.newInstance(componentType, 0); return (T[])Converts.convert(parse(json).asArray(), a.getClass(), null, convertContext); }
protected void setSwagger(ApiImpl api) { Try.throwUnchecked(() -> { StringBuilder s = new StringBuilder(); swaggerJsonWriter.write(api.getMetadata(), s); JsonObject swagger = JSON.parse(s.toString()).asJsonObject(); api.setSwagger(swagger); }); }
@Override public Object read(Type type, Class contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { if(type instanceof Class<?>) { return readInternal((Class)type, inputMessage); }else { ParameterizedType parameterizedType = (ParameterizedType)type; Class typeArgument = (Class)parameterizedType.getActualTypeArguments()[0]; Collection c = newCollection((Class)parameterizedType.getRawType()); try(InputStream is = inputMessage.getBody()) { try(InputStreamReader reader = new InputStreamReader(is, getCharset(inputMessage))) { try { JsonArray a = JSON.parse(reader).asJsonArray(); a.forEach(item -> { if(null == item) { c.add(null); }else { Object o = Reflection.newInstance(typeArgument); ((JsonParsable)o).parseJson(item); c.add(o); } }); }catch (Exception e) { throw new HttpMessageNotReadableException(e.getMessage(), e); } } } return c; } }
@Override protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try(InputStream is = inputMessage.getBody()) { if(clazz.isArray()) { try(InputStreamReader reader = new InputStreamReader(is, getCharset(inputMessage))) { try { JsonArray a = JSON.parse(reader).asJsonArray(); Object o = Array.newInstance(clazz.getComponentType(), a.length()); for(int i=0;i<a.length();i++) { Object item = Reflection.newInstance(clazz.getComponentType()); ((JsonParsable)item).parseJson(a.getObject(i)); Array.set(o, i, item); } return o; }catch (Exception e) { throw new HttpMessageNotReadableException(e.getMessage(), e); } } }else { Object o = Reflection.newInstance(clazz); try { ((JsonParsable)o).parseJson(IO.readString(is, getCharset(inputMessage))); return o; }catch (Exception e) { throw new HttpMessageNotReadableException(e.getMessage(), e); } } } }
protected Map<String, Object> verify(String content, String payload, String signature) { if (!verifySignature(content, signature)) { throw new TokenVerifyException(ErrorCode.INVALID_SIGNATURE, "Signature verification failed"); } JsonValue json; try { json = JSON.parse(JWT.base64UrlDeocodeToString(payload)); } catch (Exception e) { throw new TokenVerifyException(ErrorCode.INVALID_PAYLOAD, "Parse payload as json object failed, " + e.getMessage()); } if (!json.isMap()) { throw new TokenVerifyException(ErrorCode.INVALID_PAYLOAD, "The payload must be json object '{..}'"); } //get claims Map<String, Object> claims = json.asMap(); //verify expiration verifyExpiration(claims); return claims; }
public MetaModel read(Resource file) { String content = file.getContent().trim(); if (content.length() == 0) { return null; } String name = Strings.upperFirst(Paths.getFileNameWithoutExtension(file.getFilename())); JsonObject o = JSON.parse(file.getContent()).asJsonObject(); if(o.asMap().isEmpty()) { return null; } MetaModel model = JSON.convert(o.asMap(), MetaModel.class); if (!Strings.isEmpty(model.getClassName())) { Class<?> c = Classes.tryForName(model.getClassName()); if (null == c) { throw new IllegalStateException("Model class '" + model.getClassName() + "' not found!"); } MType type = MTypes.getMType(c); if (!type.isComplexType()) { throw new IllegalStateException("The model class '" + model.getClassName() + "' must be complex type!"); } MetaUtils.tryCopyModel(type.asComplexType(), model); } MApiModelBuilder am = nonValidateSwaggerReader.readModel(name, o.asMap(), new SwaggerTypeEx()); MetaUtils.tryCopyModel(am, model); validator.validate("Model(" + model.getName() + ")", model); return model; }
protected AccessToken fetchAccessToken(HttpRequest request) { if(null != config.getClientId()){ request.addHeader(Headers.AUTHORIZATION, "Basic " + Base64.encode(config.getClientId()+":"+config.getClientSecret())); } HttpResponse response = request.send(); if(ContentTypes.APPLICATION_JSON_TYPE.isCompatible(response.getContentType())){ String content = response.getString(); log.debug("Received response : {}", content); JsonValue json = JSON.parse(content); if(!json.isMap()) { throw new OAuth2InternalServerException("Invalid response from auth server : not a json map"); }else{ Map<String, Object> map = json.asMap(); String error = (String)map.get("error"); if(Strings.isEmpty(error)) { return createAccessToken(map); }else{ throw new OAuth2InternalServerException("Auth server response error '" + error + "' : " + map.get("error_description")); } } }else{ throw new OAuth2InternalServerException("Invalid response from auth server"); } }
JsonValue json = JSON.parse(content);
JsonValue json = JSON.parse(content);
JsonValue json = JSON.parse(content);