lundi 13 octobre 2014

[Jackson]why TextNode doesn't have update value(set) method?


Vote count:

0




I want to modify the value of TextNode using jackson.

But there is no such method in API.

Then I try to use reflection to break the limitation:



public class TestModify {

public static void main(final String[] args) throws JsonProcessingException, IOException,
NoSuchFieldException, SecurityException, IllegalArgumentException,
IllegalAccessException {
final String json = "[{},\"123123\",\"12456\"]";
final ObjectMapper mapper = new ObjectMapper();
final JsonNode node = mapper.readTree(json);
final Iterator<JsonNode> nodes = node.elements();
while (nodes.hasNext()) {
final JsonNode n = nodes.next();
if (n instanceof TextNode) {
final Field f = TextNode.class.getDeclaredField("_value");
f.setAccessible(true);
f.set(n, "updated");
}
System.out.println(n.getClass());
}
System.out.println(node);
}


}


The code seems work fine and println:



class com.fasterxml.jackson.databind.node.ObjectNode
class com.fasterxml.jackson.databind.node.TextNode
class com.fasterxml.jackson.databind.node.TextNode
[{},"updated","updated"]


so why there is not update methods in original API?



asked 9 secs ago







[Jackson]why TextNode doesn't have update value(set) method?

Aucun commentaire:

Enregistrer un commentaire