private static Event getState(JsonValue value) { switch (value.getValueType()) { case ARRAY: return Event.START_ARRAY; case OBJECT: return Event.START_OBJECT; case STRING: return Event.VALUE_STRING; case NUMBER: return Event.VALUE_NUMBER; case TRUE: return Event.VALUE_TRUE; case FALSE: return Event.VALUE_FALSE; case NULL: return Event.VALUE_NULL; default: throw new JsonException(JsonMessages.PARSER_STATE_ERR(value.getValueType())); } }
private void diff(String path, JsonValue source, JsonValue target) { if (source.equals(target)) { return; } ValueType s = source.getValueType(); ValueType t = target.getValueType(); if (s == ValueType.OBJECT && t == ValueType.OBJECT) { diffObject(path, (JsonObject) source, (JsonObject) target); } else if (s == ValueType.ARRAY && t == ValueType.ARRAY) { diffArray(path, (JsonArray) source, (JsonArray) target); } else { builder.replace(path, target); } }
@Override public JsonStructure add(JsonValue value) { switch (value.getValueType() ) { case OBJECT: case ARRAY: this.root = (JsonStructure) value; break; default: throw new JsonException(JsonMessages.NODEREF_VALUE_ADD_ERR()); } return root; }
/** * Compares the specified object with this {@link JsonValue} * object for equality. Returns {@code true} if and only if the * specified object is also a JsonValue, and their * {@link #getValueType()} objects are <i>equal</i>. * * @param obj the object to be compared for equality with this JsonValue * @return {@code true} if the specified object is equal to this * JsonValue */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof JsonValue) { return getValueType().equals(((JsonValue) obj).getValueType()); } return false; }
private static String asString(JsonValue value) { if (JsonValue.ValueType.STRING.equals(value.getValueType())) { return ((JsonString) value).getString(); } return value.toString(); } }
/** * Applies the patch operations to the specified {@code target}. * The target is not modified by the patch. * * @param target the target to apply the patch operations * @return the transformed target after the patch * @throws JsonException if the supplied JSON Patch is malformed or if * it contains references to non-existing members */ @Override public JsonStructure apply(JsonStructure target) { JsonStructure result = target; for (JsonValue operation: patch) { if (operation.getValueType() != ValueType.OBJECT) { throw new JsonException(JsonMessages.PATCH_MUST_BE_ARRAY()); } result = apply(result, (JsonObject) operation); } return result; }
/** * Returns a {@link Attributes} instance based on the given {@link JsonObject}. * * @param claims a json object with the claims to extract * @return an {@link Attributes} instance with attributes from the given json object */ public static Attributes toAttributes(JsonObject claims) { return claims.entrySet().stream().reduce(new MapAttributes(), (mapAttributes, entry) -> { String claimName = entry.getKey(); JsonValue claimValue = entry.getValue(); if (JsonValue.ValueType.ARRAY.equals(claimValue.getValueType())) { JsonArray jsonArray = claims.getJsonArray(claimName); jsonArray.forEach(arrayValue -> mapAttributes.addLast(claimName, asString(arrayValue))); } else { mapAttributes.addLast(claimName, asString(claimValue)); } return mapAttributes; }, (mapAttributes, mapAttributes2) -> mapAttributes); }
if (source.getValueType() != JsonValue.ValueType.OBJECT || target.getValueType() != JsonValue.ValueType.OBJECT) { return target;
/** * Processes a part of package.json (as defined by JsonObject) and update * the specified dependency with relevant info. * * @param engine the dependency-check engine * @param dependency the Dependency to update * @param jsonObject the jsonObject to parse * @param depType the dependency type */ protected void processPackage(Engine engine, Dependency dependency, JsonObject jsonObject, String depType) { for (int i = 0; i < jsonObject.size(); i++) { for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) { final String name = entry.getKey(); String version = ""; if (entry.getValue() != null && entry.getValue().getValueType() == JsonValue.ValueType.STRING) { version = ((JsonString) entry.getValue()).getString(); } final Dependency existing = findDependency(engine, name, version); if (existing == null) { final Dependency nodeModule = createDependency(dependency, name, version, depType); engine.addDependency(nodeModule); } else { existing.addProjectReference(dependency.getName() + ": " + depType); } } } }
if (patch.getValueType() != JsonValue.ValueType.OBJECT) { return patch; if (target.getValueType() != JsonValue.ValueType.OBJECT) { target = JsonValue.EMPTY_JSON_OBJECT;
if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) { final JsonObjectBuilder requiresBuilder = Json.createObjectBuilder(); final JsonObject dependencies = packageJson.getJsonObject("dependencies");
private boolean hasValidAudience(JsonObject claims) throws RealmUnavailableException { if (this.audiences.isEmpty()) return true; JsonValue audience = claims.get("aud"); if (audience == null) { log.debug("Token does not contain an audience claim"); return false; } JsonArray audClaimArray; if (JsonValue.ValueType.STRING.equals(audience.getValueType())) { audClaimArray = Json.createArrayBuilder().add(audience).build(); } else { audClaimArray = (JsonArray) audience; } boolean valid = audClaimArray.stream() .map(jsonValue -> (JsonString) jsonValue) .anyMatch(audience1 -> audiences.contains(audience1.getString())); if (!valid) { log.debugf("Audience check failed. Provided [%s] but was expected [%s].", audClaimArray.toArray(), this.audiences); } return valid; }
@Override public void write(JsonValue value) { switch (value.getValueType()) { case OBJECT: writeObject((JsonObject) value); return; case ARRAY: writeArray((JsonArray) value); return; default: if (writeDone) { throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED()); } writeDone = true; generator.write(value); generator.flushBuffer(); if (os != null) { generator.flush(); } } }
/** * Transform from json to object. * * @param jsonValue json value * @return object most correct for the type, or string value if not understood (e.g. json object) */ public static Object toObject(JsonValue jsonValue) { switch (jsonValue.getValueType()) { case ARRAY: return jsonValue.toString(); case OBJECT: return jsonValue.toString(); case STRING: return ((JsonString) jsonValue).getString(); case NUMBER: return ((JsonNumber) jsonValue).numberValue(); case TRUE: return true; case FALSE: return false; case NULL: return null; default: return jsonValue.toString(); } }
for (int i = 1; i < s; i++) { switch (value.getValueType()) { case OBJECT: JsonObject object = (JsonObject) value; throw new JsonException(JsonMessages.POINTER_REFERENCE_INVALID(value.getValueType()));
addComma = true; if (ValueType.STRING == array.get(x).getValueType()) { sb.append(array.getString(x)); } else {
checkContextForValue(); switch (value.getValueType()) { case ARRAY: JsonArray array = (JsonArray)value;
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); switch (value.getValueType()) { case ARRAY: JsonArray array = (JsonArray)value;
private static JsonObject getJsonObject(JsonObject obj, String prop) throws ChaincodeCollectionConfigurationException { JsonValue ret = obj.get(prop); if (ret == null) { throw new ChaincodeCollectionConfigurationException(format("property %s missing", prop)); } if (ret.getValueType() != JsonValue.ValueType.OBJECT) { throw new ChaincodeCollectionConfigurationException(format("property %s wrong type expected object got %s", prop, ret.getValueType().name())); } return ret.asJsonObject(); }
private static String getJsonString(JsonObject obj, String prop) throws ChaincodeCollectionConfigurationException { JsonValue ret = obj.get(prop); if (ret == null) { throw new ChaincodeCollectionConfigurationException(format("property %s missing", prop)); } if (ret.getValueType() != JsonValue.ValueType.STRING) { throw new ChaincodeCollectionConfigurationException(format("property %s wrong type expected string got %s", prop, ret.getValueType().name())); } return obj.getString(prop); }