@@ -1673,13 +1673,13 @@ public static String getValueFromJson(String pointer, String json) {
16731673 * @param updater The function to apply to the value at the pointer.
16741674 * @return The updated object, or the original if any error occurs.
16751675 */
1676- public static <T > T updateValueByPointer (T value , String pointer , UnaryOperator <Object > updater ) {
1676+ public static <T > T updateValueByPointer (T value , String pointer ,
1677+ UnaryOperator <Object > updater ) {
16771678 if (value == null || pointer == null || pointer .isEmpty () || updater == null ) {
16781679 return value ;
16791680 }
16801681
16811682 try {
1682- ObjectMapper mapper = new ObjectMapper ();
16831683 JsonNode root = mapper .valueToTree (value );
16841684
16851685 // Split pointer into parent path and last segment
@@ -1694,80 +1694,97 @@ public static <T> T updateValueByPointer(T value, String pointer, UnaryOperator<
16941694
16951695 boolean updated = false ;
16961696 if (parentNode .isObject ()) {
1697- updated = updateObjectField ((ObjectNode ) parentNode , fieldName , updater , mapper );
1697+ updated = updateObjectField ((ObjectNode ) parentNode , fieldName , updater );
16981698 } else if (parentNode .isArray ()) {
1699- updated = updateArrayIndex ((ArrayNode ) parentNode , fieldName , updater , mapper );
1699+ updated = updateArrayIndex ((ArrayNode ) parentNode , fieldName , updater );
17001700 }
17011701
17021702 return updated ? mapper .convertValue (root , new TypeReference <T >() {}) : value ;
17031703 } catch (Exception e ) {
17041704 return value ;
17051705 }
17061706 }
1707-
1708- private static boolean updateObjectField (
1709- ObjectNode objectNode ,
1710- String fieldName ,
1711- UnaryOperator <Object > updater ,
1712- ObjectMapper mapper
1713- ) {
1707+
1708+ private static boolean updateObjectField ( ObjectNode objectNode , String fieldName ,
1709+ UnaryOperator <Object > updater ) {
17141710 JsonNode oldNode = objectNode .get (fieldName );
17151711 Object oldValue = toObject (oldNode );
17161712 Object newValueRaw = updater .apply (oldValue );
17171713
1718- if (newValueRaw == null ) return false ;
1719- JsonNode newJsonNode = toJsonNode (newValueRaw , mapper );
1720- if (newJsonNode == null ) return false ;
1714+ if (newValueRaw == null ) {
1715+ return false ;
1716+ }
1717+ JsonNode newJsonNode = toJsonNode (newValueRaw );
1718+ if (newJsonNode == null ) {
1719+ return false ;
1720+ }
17211721
17221722 objectNode .set (fieldName , newJsonNode );
17231723 return true ;
17241724 }
17251725
1726- private static boolean updateArrayIndex (
1727- ArrayNode arrayNode ,
1728- String indexStr ,
1729- UnaryOperator <Object > updater ,
1730- ObjectMapper mapper
1731- ) {
1726+ private static boolean updateArrayIndex (ArrayNode arrayNode , String indexStr ,
1727+ UnaryOperator <Object > updater ) {
1728+ int index = -1 ;
1729+
17321730 try {
1733- int index = Integer .parseInt (indexStr );
1734- if ( index < 0 || index >= arrayNode . size () ) {
1735- return false ; // invalid index
1736- }
1731+ index = Integer .parseInt (indexStr );
1732+ } catch ( NumberFormatException e ) {
1733+ return false ;
1734+ }
17371735
1738- JsonNode oldNode = arrayNode .get ( index );
1739- Object oldValue = toObject ( oldNode );
1740- Object newValueRaw = updater . apply ( oldValue );
1736+ if ( index < 0 || index > = arrayNode .size ()) {
1737+ return false ; // invalid index
1738+ }
17411739
1742- if ( newValueRaw == null ) return false ;
1743- JsonNode newJsonNode = toJsonNode ( newValueRaw , mapper );
1744- if ( newJsonNode == null ) return false ;
1740+ JsonNode oldNode = arrayNode . get ( index ) ;
1741+ Object oldValue = toObject ( oldNode );
1742+ Object newValueRaw = updater . apply ( oldValue ) ;
17451743
1746- arrayNode .set (index , newJsonNode );
1747- return true ;
1748- } catch (NumberFormatException e ) {
1744+ if (newValueRaw == null ) {
17491745 return false ;
17501746 }
1747+
1748+ JsonNode newJsonNode = toJsonNode (newValueRaw );
1749+ if (newJsonNode == null ) {
1750+ return false ;
1751+ }
1752+
1753+ arrayNode .set (index , newJsonNode );
1754+ return true ;
17511755 }
17521756
17531757 private static Object toObject (JsonNode node ) {
1754- if (node == null || node .isNull ()) return null ;
1755- if (node .isTextual ()) return node .asText ();
1756- if (node .isNumber ()) return node .numberValue ();
1757- if (node .isBoolean ()) return node .booleanValue ();
1758+ if (node == null || node .isNull ()) {
1759+ return null ;
1760+ }
1761+
1762+ if (node .isTextual ()) {
1763+ return node .asText ();
1764+ }
1765+
1766+ if (node .isNumber ()) {
1767+ return node .numberValue ();
1768+ }
1769+
1770+ if (node .isBoolean ()) {
1771+ return node .booleanValue ();
1772+ }
17581773
17591774 return null ;
17601775 }
17611776
1762- private static JsonNode toJsonNode (Object obj , ObjectMapper mapper ) {
1763- if (obj == null ) return NullNode .getInstance ();
1777+ private static JsonNode toJsonNode (Object obj ) {
1778+ if (obj == null ) {
1779+ return NullNode .getInstance ();
1780+ }
17641781
1765- if (obj instanceof String ||
1766- obj instanceof Integer ||
1767- obj instanceof Long ||
1768- obj instanceof Double ||
1769- obj instanceof Float ||
1770- obj instanceof Boolean ) {
1782+ if (obj instanceof String
1783+ || obj instanceof Integer
1784+ || obj instanceof Long
1785+ || obj instanceof Double
1786+ || obj instanceof Float
1787+ || obj instanceof Boolean ) {
17711788 return mapper .valueToTree (obj );
17721789 }
17731790
0 commit comments