jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Provide a way to get bound keys

Open quaff opened this issue 5 months ago • 3 comments

Is your feature request related to a problem? Please describe.

We could use HTTP PATCH for partial update, currently there is no easy way to know which keys are bound. More background https://github.com/spring-projects/spring-framework/issues/27159

Describe the solution you'd like

Provide an interface like:

	public interface BoundKeysAware {

		public void setBoundKeys(Set<String> boundKeys);

	}

	public class User implements BoundKeysAware {

		private String username;

		private String password = "******";

		private Set<String> boundKeys;

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public Set<String> getBoundKeys() {
			return boundKeys;
		}

		public void setBoundKeys(Set<String> boundKeys) {
			this.boundKeys = boundKeys;
		}

	}

	@Test
	public void test() throws Exception {
		ObjectMapper om = new ObjectMapper();
		String json = """
				{"username": "test", "password": null}
				""";
		User user = om.readValue(json, User.class);
		assertThat(user.getUsername()).isEqualTo("test");
		assertThat(user.getPassword()).isNull();

		assertThat(user.getBoundKeys()).containsExactly("username", "password");
		
		
		json = """
				{"username": "test"}
				""";
		user = om.readValue(json, User.class);
		assertThat(user.getUsername()).isEqualTo("test");
		assertThat(user.getPassword()).isEqualTo("******");

		assertThat(user.getBoundKeys()).containsExactly("username");
	}

Usage example

No response

Additional context

No response

quaff avatar Jun 27 '25 01:06 quaff

I'm not sure that this is a feature that many people need. If you want to provide a PR then it will be looked at. Anyone else who thinks this is useful could also provide a PR. It looks like something that would be hard to implement.

If this was my problem, I would write a solution using existing Jackson APIs.

  • I would use mapper.readTree to read the JSON into a node tree - see https://www.baeldung.com/jackson-json-node-tree-model
  • It is possible to check the tree for which nodes are in it (and which ones are not)

pjfanning avatar Jun 27 '25 07:06 pjfanning

This appears to be a variant on the 'Missing Node' issue. You can use your favourite Search Engine or GenAI tool to look into discussion and solutions related to Missing Nodes (basically whether a JSON node appears in the input, appears but is explicitly set to a null value or appears and has a non-null value).

pjfanning avatar Jun 27 '25 10:06 pjfanning

I don't really see the feature as suggested feasible or generally useful (also rather difficult if not impossible to implement but that aside). So basically "what @pjfanning said".

Will leave open for now but no plans to implement.

cowtowncoder avatar Oct 13 '25 00:10 cowtowncoder