/** * Returns a new JSON Patch created from the given string. * * @param jsonString the string representation of the JSON Patch object to be created. * @return the new JSON Patch. * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws IllegalArgumentException if {@code jsonString} is empty or if {@code jsonString} did contain an * unknown operation. * @throws JsonParseException if {@code jsonString} does not contain a valid JSON Patch JSON object. * @throws JsonMissingFieldException if {@code jsonString} did not contain {@link JsonFields#OPERATION} or * {@link JsonFields#PATH}. */ public static ImmutableJsonPatch fromJson(final String jsonString) { requireNonNull(jsonString, "The string representation of the JSON Patch to be created must not be null!"); if (jsonString.isEmpty()) { throw new IllegalArgumentException( "The string representation of the JSON Patch to be created must not be empty!"); } final JsonObject jsonObject = JsonFactory.newObject(jsonString); final String operationName = jsonObject.getValueOrThrow(JsonFields.OPERATION); final Operation operation = Operation.fromString(operationName).orElseThrow( () -> new IllegalArgumentException(MessageFormat.format("Operation <{0}> is unknown!", operationName)) ); final JsonPointer path = JsonFactory.newPointer(jsonObject.getValueOrThrow(JsonFields.PATH)); final JsonValue value = jsonObject.getValue(JsonFields.VALUE).orElse(null); return new ImmutableJsonPatch(operation, path, value); }
/** * Returns a new JSON Patch created from the given string. * * @param jsonString the string representation of the JSON Patch object to be created. * @return the new JSON Patch. * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws IllegalArgumentException if {@code jsonString} is empty or if {@code jsonString} did contain an * unknown operation. * @throws JsonParseException if {@code jsonString} does not contain a valid JSON Patch JSON object. * @throws JsonMissingFieldException if {@code jsonString} did not contain {@link JsonFields#OPERATION} or * {@link JsonFields#PATH}. */ public static ImmutableJsonPatch fromJson(final String jsonString) { requireNonNull(jsonString, "The string representation of the JSON Patch to be created must not be null!"); if (jsonString.isEmpty()) { throw new IllegalArgumentException( "The string representation of the JSON Patch to be created must not be empty!"); } final JsonObject jsonObject = JsonFactory.newObject(jsonString); final String operationName = jsonObject.getValueOrThrow(JsonFields.OPERATION); final Operation operation = Operation.fromString(operationName).orElseThrow( () -> new IllegalArgumentException(MessageFormat.format("Operation <{0}> is unknown!", operationName)) ); final JsonPointer path = JsonFactory.newPointer(jsonObject.getValueOrThrow(JsonFields.PATH)); final JsonValue value = jsonObject.getValue(JsonFields.VALUE).orElse(null); return new ImmutableJsonPatch(operation, path, value); }