default JsonObject asJsonObject() { return getJson().asJsonObject(); }
/** * Parse the json string and returns the the array. */ public static Map<String,Object> decodeMap(String json){ return parse(json).asMap(); }
public void read(MetaApi api, Resource resource, boolean global) { JsonValue v = JsonValue.of(JSON.decode(resource.getContent())); String path = !global ? PathDefParser.path("/app/operations/", resource) : null; if(v.isObject()) { read(api, global, path,(Map)v.raw()); }else if(v.isArray()) { for(Object map : v.asArray()) { read(api, global, path,(Map)map); } }else { throw new IllegalStateException("Invalid json format of tags, must be an array or an object"); } }
@Override public void parseJson(JsonValue json) { if(json.isObject()) { JsonObject o = json.asJsonObject(); this.user = toRule(o.get("user")); this.client = toRule(o.get("client")); return; } Object v = json.raw(); if(v instanceof String) { this.parseString((String)v); return; } throw new IllegalStateException("Can't parse json value '" + v + "'"); }
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; }
/** * Returns true if the raw value is an json array. */ default boolean isArray() { return raw() instanceof List; }
JsonValue json = JSON.parse(content); if(!json.isMap()) { throw new OAuth2InternalServerException("Invalid response from auth server : not a json map"); }else{ JsonObject o = json.asJsonObject();
@Override protected JsonValue computeNext() { if(!it.hasNext()){ return endOfData(); } return JsonValue.of(it.next()); } };
/** * Parse the json string and returns the the array. */ public static Object[] decodeArray(String json){ return parse(json).asArray(); }
default JsonArray asJsonArray() { return getJson().asJsonArray(); }
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"); } }
/** * Returns the raw value as {@link String}. */ default String asString() { return (String)raw(); }
/** * Parse the json string and returns the result as {@link JsonValue}. */ public static JsonValue parse(String json) { return JsonValue.of(decode(json)); }
/** * Parse the json string and returns the the array. */ public static Object[] decodeArray(Reader json){ return parse(json).asArray(); }
@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; } }
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); }); }
/** * Returns true if the raw value is an json object. * * <p/> * Same as {@link #isMap()} */ default boolean isObject() { return raw() instanceof Map; }
/** * Wraps the property value as {@link JsonValue}. */ public JsonValue getValue(String key){ Object o = get(key); return null == o ? null : JsonValue.of(o); }
/** * 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); }