On Wed, Dec 14, 2022 at 5:35 PM Senthil Nathan <senthil...@gmail.com> wrote:
>
> Hi,
>
> Rather updating all the properties, I want to update the values for only null 
> fields using readerForUpdating.
>
> Class:
> @Getter
> @Setter
> public class Record {
>      public String source;
>      public String resource;
> }
>
> Record record = new Record();
> record.setSource("sourceFromObject");
> record.setResource(null);
>
> Json:
>
> String json = {"source": "sourceFromJson", "resource":"resourceFromJson"}
>
> When I do,
>   ObjectMapper objectMapper = new ObjectMapper();
>   objectMapper.readerForUpdating(record).readValue(json);
>
> am getting the result as  [Record: source = " sourceFromJson ", resource = 
> "resourceFromJson" ] but
>  I want the result to be  [Record: source = "sourceFromObject", resource = 
> "resourceFromJson" ]
>
> Could anyone please help on this.

That cannot be done with Records in general: Records are immutable so
`readerForUpdating()` won't be able to use setters (although might try
to force changes via fields).

Otherwise I would suggest overriding `setSource(...)` and other
methods to implement logic you want: you could assign only if there is
no non-null value.
But that won't really work with Records: a new instance will be
created from just the data passed.

-+ 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/CAL4a10hRC8LQ2rRSvP2f9ZLdLHmdDUtLKSxW1_a-HngrEd4ePg%40mail.gmail.com.

Reply via email to