Skip to content

Add support for @JsonMerge to allow "deep update"  #1399

Closed

Description

@cowtowncoder

Currently existing values of properties are not taken into consideration, with the rare case of "setterless-getters" where this is done for Collection types if no setter exists.
But it would be useful to allow this for Maps and POJOs as well.

Proposed mechanism is to allow use of:

public class POJO {
    @JsonSetter(merge=OptBoolean.TRUE)
    public List<String> names = new ArrayList<>();
    {
         names.add("first");
    }

In addition to per-property annotation, it should also be possible to specify "config overrides" using:

objectMapper.configOverrides(MyPOJO.class)
     .setter(JsonSetter.Value.from(.....));

which would specify default merge/no-merge settings for properties of specified type.
It would probably also make sense to allow setting fully global default, similar to ability to set "serializationInclusion" global defaults (with JsonInclude.Value).

Activity

  1. added this to the 2.9.0 milestone on Oct 27, 2016
  2. added a commit that references this issue on Oct 27, 2016
  3. cowtowncoder commented on Oct 27, 2016

    @cowtowncoder
    MemberAuthor

    Initial implementation now exists, supporting POJOs, Collections, Maps, reference types.

  4. heruan commented on Jan 3, 2017

    @heruan

    Any docs on how to test this?

  5. cowtowncoder commented on Jan 6, 2017

    @cowtowncoder
    MemberAuthor

    @heruan at this point the best way I think is to search for unit tests with @JsonSetter(merge=.

  6. changed the title [-]Add support for `@JsonSetter(merge=OptBoolean.TRUE`) to allow "deep update" [/-] [+]Add support for `@JsonMerge` to allow "deep update" [/+] on Feb 1, 2017
  7. Tetradeus commented on Apr 7, 2017

    @Tetradeus

    Wouldn't it be nice to have an option to enable setting null values at merge path ? Ref #1437

        // Also: nulls better not override
        public void testBeanMergingWithNull() throws Exception
        {
            Config config = MAPPER.readerForUpdating(new Config(5, 7))
                    .readValue(aposToQuotes("{'loc':null}"));
            assertNotNull(config.loc);
            assertEquals(5, config.loc.a);
            assertEquals(7, config.loc.b);
        }

    What if I really want to be able to set null for loc ? assertNull(config.loc);

    Thanks for your reply.

  8. cowtowncoder commented on Apr 7, 2017

    @cowtowncoder
    MemberAuthor

    @Tetradeus Please file a new issue; this one is closed. We can discuss details as part of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions