@Override public S isNotArray() { isNotNull(); assertThat(actual.isArray()) .overridingErrorMessage("Expected JSON value <%s> not to be an array but it was.", actual) .isFalse(); return myself; }
/** * Creates a new JSON array from the given string. * * @param jsonString the string that represents the JSON array. * @return the JSON array that has been created from the string. * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws IllegalArgumentException if {@code jsonString} is empty. * @throws JsonParseException if {@code jsonString} does not contain a valid JSON array. */ public static JsonArray newArray(final String jsonString) { requireNonNull(jsonString, "The JSON string to create JSON array from must not be null!"); if (jsonString.isEmpty()) { throw new IllegalArgumentException("The JSON string to create a JSON array from must not be empty!"); } if (isJsonNullLiteralString(jsonString)) { return nullArray(); } else { final JsonValue jsonValue = JsonValueParser.fromString().apply(jsonString); if (!jsonValue.isArray()) { final String msgPattern = "<{0}> is not a valid JSON array!"; throw JsonParseException.newBuilder().message(MessageFormat.format(msgPattern, jsonString)).build(); } return jsonValue.asArray(); } }
@Override public S isArray() { isNotNull(); assertThat(actual.isArray()) .overridingErrorMessage("Expected JSON value <%s> to be an array but it was not.", actual) .isTrue(); return myself; }
@Nullable private JsonValue getViewForJsonValueOrNull(final JsonValue jsonValue, final Set<String> subjectIds, final Permissions permissions) { final JsonValue result; if (jsonValue.isObject()) { result = getViewForJsonObjectOrNull(jsonValue.asObject(), subjectIds, permissions); } else if (jsonValue.isArray()) { result = getViewForJsonArrayOrNull(jsonValue.asArray(), subjectIds, permissions); } else if (grantRevokeIndex.hasPermissions(subjectIds, permissions)) { result = jsonValue; } else { result = null; } return result; }
private static boolean isJsonValueIllegal(final JsonValue entity) { final boolean result; if (entity.isArray()) { result = entity.asArray().stream().anyMatch(CommandWithOptionalEntityValidator::isJsonValueIllegal); } else if (entity.isObject()) { result = entity.asObject().stream().anyMatch(CommandWithOptionalEntityValidator::isJsonFieldIllegal); } else if (entity.isString()) { result = isStringIllegal(entity.asString()); } else { result = false; } return result; }
private static Object toJavaObject(final JsonValue jsonValue) { final Object result; if (null == jsonValue) { result = null; } else if (jsonValue.isNull()) { result = null; } else if (jsonValue.isString()) { result = jsonValue.asString(); } else if (jsonValue.isBoolean()) { result = jsonValue.asBoolean(); } else if (jsonValue.isNumber()) { final Double doubleValue = jsonValue.asDouble(); if (doubleValue.intValue() == doubleValue) { result = doubleValue.intValue(); } else if (doubleValue.longValue() == doubleValue) { result = doubleValue.longValue(); } else { result = doubleValue; } } else if (jsonValue.isObject()) { result = toJavaMap(jsonValue.asObject()); } else if (jsonValue.isArray()) { result = toJavaList(jsonValue.asArray()); } else { throw new IllegalStateException( MessageFormat.format("Failed to convert {0} to a Java object", jsonValue)); } return result; }
} else if (jsonValue.isArray()) { result = Json.parse(jsonValue.toString()); } else {
} else if (v.isString()) { return v.asString(); } else if (v.isArray()) { JsonArray a = v.asArray(); return a.stream().map(w -> getJavaValue(w)).toArray();
/** * Maps the specified JsonValue to a Java Object which can be stored in MongoDB. * * @param jsonValue the value to be mapped. * @return {@code jsonValue} as Java Object which can be stored in MongoDB or {@code null}. * @throws NullPointerException if {@code jsonValue} is {@code null}. */ @Nullable private Object mapJsonValueToJavaObject(final JsonValue jsonValue) { checkNotNull(jsonValue, "JSON value to be mapped"); final Object result; if (jsonValue.isNull()) { result = null; } else if (jsonValue.isString()) { result = jsonValue.asString(); } else if (jsonValue.isNumber()) { result = mapJsonNumberToJavaNumber(jsonValue); } else if (jsonValue.isObject()) { result = mapJsonObjectToBasicDBObject(jsonValue.asObject()); } else if (jsonValue.isArray()) { result = mapJsonArrayToBasicDBList(jsonValue.asArray()); } else if (jsonValue.isBoolean()) { result = jsonValue.asBoolean(); } else { result = null; } return result; }
/** * Maps the specified JsonValue to a Java Object which can be stored in MongoDB. * * @param jsonValue the value to be mapped. * @return {@code jsonValue} as Java Object which can be stored in MongoDB or {@code null}. * @throws NullPointerException if {@code jsonValue} is {@code null}. */ @Nullable private Object mapJsonValueToJavaObject(final JsonValue jsonValue) { checkNotNull(jsonValue, "JSON value to be mapped"); final Object result; if (jsonValue.isNull()) { result = null; } else if (jsonValue.isString()) { result = jsonValue.asString(); } else if (jsonValue.isNumber()) { result = mapJsonNumberToJavaNumber(jsonValue); } else if (jsonValue.isObject()) { result = mapJsonObjectToBsonDocument(jsonValue.asObject()); } else if (jsonValue.isArray()) { result = mapJsonArrayToBsonArray(jsonValue.asArray()); } else if (jsonValue.isBoolean()) { result = jsonValue.asBoolean(); } else { result = null; } return result; }
private static Object toJavaObject(final JsonValue jsonValue) { final Object result; if (null == jsonValue) { result = null; } else if (jsonValue.isNull()) { result = null; } else if (jsonValue.isString()) { result = jsonValue.asString(); } else if (jsonValue.isBoolean()) { result = jsonValue.asBoolean(); } else if (jsonValue.isNumber()) { if (jsonValue.isInt()) { result = jsonValue.asInt(); } else if (jsonValue.isLong()) { result = jsonValue.asLong(); } else { result = jsonValue.asDouble(); } } else if (jsonValue.isObject()) { result = toJavaMap(jsonValue.asObject()); } else if (jsonValue.isArray()) { result = toJavaList(jsonValue.asArray()); } else { throw new IllegalStateException( MessageFormat.format("Failed to convert {0} to a Java object", jsonValue)); } return result; }
private static Object mapJsonValueToJava(final JsonValue jsonValue) { final Object result; if (jsonValue.isString()) { result = jsonValue.asString(); } else if (jsonValue.isBoolean()) { result = jsonValue.asBoolean(); } else if (jsonValue.isNull()) { result = null; } else if (jsonValue.isNumber()) { if (jsonValue.isLong()) { result = jsonValue.asLong(); } else { result = jsonValue.asDouble(); } } else if (jsonValue.isArray()) { result = null; // filtering arrays is not supported } else if (jsonValue.isObject()) { result = null; // filtering objects is not supported } else { result = null; } return result; }
/** * Maps the specified JsonValue to a Java Object which can be stored in MongoDB. * * @param jsonValue the value to be mapped. * @return {@code jsonValue} as Java Object which can be stored in MongoDB or {@code null}. * @throws NullPointerException if {@code jsonValue} is {@code null}. */ @Nullable private BsonValue mapJsonValueToBsonValue(final JsonValue jsonValue) { checkNotNull(jsonValue, "JSON value to be mapped"); final BsonValue result; if (jsonValue.isNull()) { result = BsonNull.VALUE; } else if (jsonValue.isString()) { result = new BsonString(jsonValue.asString()); } else if (jsonValue.isNumber()) { result = mapJsonNumberToBsonNumber(jsonValue); } else if (jsonValue.isObject()) { result = mapJsonObjectToBsonDocument(jsonValue.asObject()); } else if (jsonValue.isArray()) { result = mapJsonArrayToBsonArray(jsonValue.asArray()); } else if (jsonValue.isBoolean()) { result = BsonBoolean.valueOf(jsonValue.asBoolean()); } else { result = null; } return result; }