@Test public void shouldAllowNodeTypeChange() throws Exception { JsonNode node = mapper.readValue("{\"key\": {\"sub\": \"value\"}}", JsonNode.class); JsonPointer pointer = JsonPointer.compile("/key/sub"); JsonPointerEditor editor = new JsonPointerEditor(node, pointer); editor.setValue(factory.objectNode().set("subsub", factory.textNode("nested"))); assertThat("nested", equalTo(node.at("/key/sub/subsub").asText())); }
public JsonNode setValue( JsonNode value ) { JsonPointer currentPointer = pointer; JsonNode currentNode = root; JsonNode parentNode = null; String matchingProperty = null; int matchingIndex = -1; while( !"".equals(currentPointer.getMatchingProperty()) ) { parentNode = currentNode; matchingProperty = currentPointer.getMatchingProperty(); matchingIndex = currentPointer.getMatchingIndex(); if( currentNode instanceof ArrayNode ) { currentNode = currentNode.path(matchingIndex); } else { currentNode = currentNode.path(matchingProperty); } currentPointer = currentPointer.tail(); } if( parentNode == null ) { root = value; } else if( parentNode instanceof ObjectNode ) { ((ObjectNode)parentNode).set(matchingProperty, value); } else if( parentNode instanceof ArrayNode ) { ((ArrayNode) parentNode).set(matchingIndex, value); } return value; }
JsonPointerEditor { public JsonNode setValue( JsonNode value ) { JsonPointer currentPointer = pointer; JsonNode currentNode = root; JsonNode parentNode = null; String matchingProperty = null; int matchingIndex = -1; while( !"".equals(currentPointer.getMatchingProperty()) ) { parentNode = currentNode; matchingProperty = currentPointer.getMatchingProperty(); matchingIndex = currentPointer.getMatchingIndex(); if( currentNode instanceof ArrayNode ) { currentNode = currentNode.path(matchingIndex); } else { currentNode = currentNode.path(matchingProperty); } currentPointer = currentPointer.tail(); } if( parentNode == null ) { root = value; } else if( parentNode instanceof ObjectNode ) { ((ObjectNode)parentNode).set(matchingProperty, value); } else if( parentNode instanceof ArrayNode ) { ((ArrayNode) parentNode).set(matchingIndex, value); } return value; } }
JsonPointerEditor { public JsonNode setValue( JsonNode value ) { JsonPointer currentPointer = pointer; JsonNode currentNode = root; JsonNode parentNode = null; String matchingProperty = null; int matchingIndex = -1; while( !"".equals(currentPointer.getMatchingProperty()) ) { parentNode = currentNode; matchingProperty = currentPointer.getMatchingProperty(); matchingIndex = currentPointer.getMatchingIndex(); if( currentNode instanceof ArrayNode ) { currentNode = currentNode.path(matchingIndex); } else { currentNode = currentNode.path(matchingProperty); } currentPointer = currentPointer.tail(); } if( parentNode == null ) { root = value; } else if( parentNode instanceof ObjectNode ) { ((ObjectNode)parentNode).set(matchingProperty, value); } else if( parentNode instanceof ArrayNode ) { ((ArrayNode) parentNode).set(matchingIndex, value); } return value; } JsonPointerEditor( JsonNode root, JsonPointer pointer ); }
JsonPointerEditor { public JsonNode setValue( JsonNode value ) { JsonPointer currentPointer = pointer; JsonNode currentNode = root; JsonNode parentNode = null; String matchingProperty = null; int matchingIndex = -1; while( !"".equals(currentPointer.getMatchingProperty()) ) { parentNode = currentNode; matchingProperty = currentPointer.getMatchingProperty(); matchingIndex = currentPointer.getMatchingIndex(); if( currentNode instanceof ArrayNode ) { currentNode = currentNode.path(matchingIndex); } else { currentNode = currentNode.path(matchingProperty); } currentPointer = currentPointer.tail(); } if( parentNode == null ) { root = value; } else if( parentNode instanceof ObjectNode ) { ((ObjectNode)parentNode).set(matchingProperty, value); } else if( parentNode instanceof ArrayNode ) { ((ArrayNode) parentNode).set(matchingIndex, value); } return value; } JsonPointerEditor( JsonNode root, JsonPointer pointer ); JsonNode getRoot(); JsonNode getValue(); JsonNode setValue( JsonNode value ); }
JsonPointerEditor { public JsonNode setValue( JsonNode value ) { JsonPointer currentPointer = pointer; JsonNode currentNode = root; JsonNode parentNode = null; String matchingProperty = null; int matchingIndex = -1; while( !"".equals(currentPointer.getMatchingProperty()) ) { parentNode = currentNode; matchingProperty = currentPointer.getMatchingProperty(); matchingIndex = currentPointer.getMatchingIndex(); if( currentNode instanceof ArrayNode ) { currentNode = currentNode.path(matchingIndex); } else { currentNode = currentNode.path(matchingProperty); } currentPointer = currentPointer.tail(); } if( parentNode == null ) { root = value; } else if( parentNode instanceof ObjectNode ) { ((ObjectNode)parentNode).set(matchingProperty, value); } else if( parentNode instanceof ArrayNode ) { ((ArrayNode) parentNode).set(matchingIndex, value); } return value; } JsonPointerEditor( JsonNode root, JsonPointer pointer ); JsonNode getRoot(); JsonNode getValue(); JsonNode setValue( JsonNode value );  }