Thanks for the Inputs mate. On Sun, Jan 10, 2021 at 6:38 AM Tatu Saloranta <t...@fasterxml.com> wrote:
> On Sat, Jan 9, 2021 at 10:09 AM Govinda Sakhare > <govindasakhar...@gmail.com> wrote: > > > > Hi All, > > > > I have written a custom de-serializer to ensure backward compatibility > between models and applied it on a field as below. I want the de-serializer > to kick-in if the JsonNode is array otherwise I want the default > deserializer to deserialize the field. I tried the below approach using the > ObjectMapper object but it is not working. > > > > class Person { > > > > private int id; > > > > @JsonDeserialize(using = CustomDeserializer.class) > > @JsonAlias("addresses") > > private AddressDetails addressDetails; > > // .Getter setters > > } > > > > but in else part jsonNode.asText() is returning empty string, but > debugger is showing child nodes. am I doing something wrong here? > > Yes, the problem is that "Jsonnode.asText()" will just return String > value of scalar-valued node, and that is not going to work as input > in most cases (it might sort of work for Boolean and Number values but > nothing else): for Objects and Arrays it simply returns "". > In future you may want to read Javadocs of the methods you call to see > what they are supposed to do, that helps a lot in figuring out how to > do things. > > There are couple of ways to "read" from JsonNode (like it was a json > document), however, and the simplest is: > > return ((ObjectMapper) parser.getCodec()).treeToValue(jsonNode, > AddressDetails.class); > > (note: you usually do not want to do this > > ObjectMapper objectMapper = new ObjectMapper(); > > as constructing a new mapper is very expensive and does not have > configuration settings). > > Hope this helps, > > -+ Tatu +- > > -- > 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 jackson-user+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/jackson-user/CAL4a10jsYtehD97n%3DfsL1Xv4yoGH9DKDaJBGF_Yr%3DmsxCZXC5A%40mail.gmail.com > . > -- 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 jackson-user+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAF_LtyvZsprmP9wPSbDU%2Bjx3sYNb_PuNacdhHMYHyh9ZPJR0iA%40mail.gmail.com.