|
package com.maoudia; |
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
import com.fasterxml.jackson.databind.JavaType; |
|
import com.fasterxml.jackson.databind.JsonNode; |
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
import com.gravity9.jsonpatch.JsonPatch; |
|
import com.gravity9.jsonpatch.JsonPatchException; |
|
import com.gravity9.jsonpatch.mergepatch.JsonMergePatch; |
|
import jakarta.validation.constraints.NotNull; |
|
|
|
import java.util.Objects; |
|
|
|
/** |
|
* Utility class for applying JSON Patch and JSON Merge Patch to objects using Jackson. |
|
*/ |
|
public final class JsonPatchUtils { |
|
|
|
private JsonPatchUtils() { |
|
// Private constructor to prevent instantiation |
|
} |
|
|
|
/** |
|
* Apply a JSON Patch to an object. |
|
* |
|
* @param objectMapper Jackson ObjectMapper instance for JSON processing. |
|
* @param originalObject The original object to be patched. |
|
* @param patchJson JSON representation of the patch. |
|
* @param <T> The type of the object. |
|
* @return The patched object. |
|
* @throws RuntimeException if there is an error applying the JSON patch. |
|
*/ |
|
@NotNull |
|
public static <T> T applyJsonPatch(@NotNull ObjectMapper objectMapper, |
|
@NotNull T originalObject, |
|
@NotNull String patchJson) { |
|
JavaType targetType = objectMapper.getTypeFactory().constructType(originalObject.getClass()); |
|
return applyJsonPatch(objectMapper, originalObject, patchJson, targetType); |
|
} |
|
|
|
/** |
|
* Apply a JSON Patch to an object. |
|
* |
|
* @param objectMapper Jackson ObjectMapper instance for JSON processing. |
|
* @param originalObject The original object to be patched. |
|
* @param patchJson JSON representation of the patch. |
|
* @param targetType The target type of the patched object. |
|
* @param <T> The type of the object. |
|
* @return The patched object. |
|
* @throws RuntimeException if there is an error applying the JSON patch. |
|
*/ |
|
@NotNull |
|
public static <T> T applyJsonPatch(@NotNull ObjectMapper objectMapper, |
|
@NotNull T originalObject, |
|
@NotNull String patchJson, |
|
@NotNull JavaType targetType) { |
|
Objects.requireNonNull(objectMapper, "ObjectMapper must not be null"); |
|
Objects.requireNonNull(originalObject, "Original object must not be null"); |
|
Objects.requireNonNull(patchJson, "JSON patch must not be null"); |
|
Objects.requireNonNull(targetType, "Target type must not be null"); |
|
try { |
|
JsonNode originalNode = objectMapper.convertValue(originalObject, JsonNode.class); |
|
JsonPatch jsonPatch = objectMapper.readValue(patchJson, JsonPatch.class); |
|
JsonNode patchedNode = jsonPatch.apply(originalNode); |
|
return objectMapper.treeToValue(patchedNode, targetType); |
|
} catch (JsonProcessingException | JsonPatchException e) { |
|
throw new RuntimeException(e); |
|
} |
|
} |
|
|
|
/** |
|
* Apply a JSON Merge Patch to an object. |
|
* |
|
* @param objectMapper Jackson ObjectMapper instance for JSON processing. |
|
* @param originalObject The original object to be patched. |
|
* @param mergePatchJson JSON representation of the merge patch. |
|
* @param <T> The type of the object. |
|
* @return The patched object. |
|
* @throws RuntimeException if there is an error applying the JSON merge patch. |
|
*/ |
|
@NotNull |
|
public static <T> T applyJsonMergePatch(@NotNull ObjectMapper objectMapper, |
|
@NotNull T originalObject, |
|
@NotNull String mergePatchJson) { |
|
JavaType targetType = objectMapper.getTypeFactory().constructType(originalObject.getClass()); |
|
return applyJsonMergePatch(objectMapper, originalObject, mergePatchJson, targetType); |
|
} |
|
|
|
/** |
|
* Apply a JSON Merge Patch to an object. |
|
* |
|
* @param objectMapper Jackson ObjectMapper instance for JSON processing. |
|
* @param originalObject The original object to be patched. |
|
* @param mergePatchJson JSON representation of the merge patch. |
|
* @param targetType The target type of the patched object. |
|
* @param <T> The type of the object. |
|
* @return The patched object. |
|
* @throws RuntimeException if there is an error applying the JSON merge patch. |
|
*/ |
|
@NotNull |
|
public static <T> T applyJsonMergePatch(@NotNull ObjectMapper objectMapper, |
|
@NotNull T originalObject, |
|
@NotNull String mergePatchJson, |
|
@NotNull JavaType targetType) { |
|
Objects.requireNonNull(objectMapper, "ObjectMapper must not be null"); |
|
Objects.requireNonNull(originalObject, "Original object must not be null"); |
|
Objects.requireNonNull(mergePatchJson, "JSON merge patch must not be null"); |
|
Objects.requireNonNull(targetType, "Target type must not be null"); |
|
try { |
|
JsonNode originalNode = objectMapper.convertValue(originalObject, JsonNode.class); |
|
JsonMergePatch jsonMergePatch = objectMapper.readValue(mergePatchJson, JsonMergePatch.class); |
|
JsonNode patchedNode = jsonMergePatch.apply(originalNode); |
|
return objectMapper.treeToValue(patchedNode, targetType); |
|
} catch (JsonProcessingException | JsonPatchException e) { |
|
throw new RuntimeException(e); |
|
} |
|
} |
|
} |