The problem with your code is that you are for some reason assuming
that `user` would be dropped for some reason: it is not. JSON
structure that would be equivalent is rather:

{ "user" : {
     "name" : "Joe",
     "age" : 28
  }
}

So basically there is no 'age' property in main level object: there is
just 'user'. This is by design; while there is no guarantee Tree model
api works (it is not officially supported by XML backend), it will try
to represent XML content as closely as possible.
The only way to do that is to retain root element name as well.

-+ Tatu +-


On Mon, Mar 6, 2017 at 1:34 PM, Diego Oliveira <[email protected]> wrote:
> Hello all,
>
>     I'm using the Jackson json library to work with some Json/Xml content,
> the content type is dynamic. My feature needs to open the payload, remove
> some data and generate a new version. The problem I'm getting is the way
> Jackson is handling the root element name for xml content. Follows one
> example that works and one that don't.
>
> Prints different, but I wanted it to be equal
>
>             ObjectMapper xmlMapper = new XmlMapper();
>
>         String original = "<user><name>jhon</name><age>28</age></user>";
>         String expected = "<user><name>jhon</name></user>";
>
>         ObjectNode obj = xmlMapper.readValue(original, ObjectNode.class);
>         obj.remove("age");
>
>         String serialized = xmlMapper.writeValueAsString(obj);
>
>         if (expected.equals(serialized)) {
>             System.out.println("Equals");
>         } else {
>             System.out.println("Different");
>         }
>
> Print equals as expected
>
>         ObjectMapper jsonMapper = new ObjectMapper();
>
>         String original = "{\"name\":\"Joe\",\"age\":28}";
>         String expected = "{\"name\":\"Joe\"}";
>
>         ObjectNode obj = jsonMapper.readValue(original, ObjectNode.class);
>         obj.remove("age");
>         String serialized = jsonMapper.writeValueAsString(obj);
>
>         if (expected.equals(serialized)) {
>             System.out.println("Equals");
>         } else {
>             System.out.println("Different");
>         }
>
>     The gist contains the full example code
> https://gist.github.com/diegooliveira/34252ae174cfee4e52628ecfc962aaf1. Are
> there any way that this code might work properly?
>
> Thanks in advance,
> D.
>
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to