/** * Defines the reverse operation of #toString method to be able to convert a string representation of an * operation to its corresponding enumeration element. * * @param name the operation name. * @return the Operation. */ public static Optional<Operation> fromString(@Nullable final String name) { return Stream.of(values()) .filter(operation -> Objects.equals(operation.name, name)) .findAny(); }
/** * 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); }
@Override public JsonObject toJson() { final JsonObjectBuilder builder = JsonFactory.newObjectBuilder() .set(JsonFields.OPERATION, operation.toString()) .set(JsonFields.PATH, path.toString()); if (null != value) { builder.set(JsonFields.VALUE, value); } return builder.build(); }
@Override public JsonObject toJson() { final JsonObjectBuilder builder = JsonFactory.newObjectBuilder() .set(JsonFields.OPERATION, operation.toString()) .set(JsonFields.PATH, path.toString()); if (null != value) { builder.set(JsonFields.VALUE, value); } return builder.build(); }
/** * Defines the reverse operation of #toString method to be able to convert a string representation of an * operation to its corresponding enumeration element. * * @param name the operation name. * @return the Operation. */ public static Optional<Operation> fromString(@Nullable final String name) { return Stream.of(values()) .filter(operation -> Objects.equals(operation.name, name)) .findAny(); }