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

Add test case for using JsonUnwrapped on fields of the same class

Open kariem opened this issue 4 months ago • 6 comments

See FasterXML/jackson#278 for context

I would like to include a bean into a class multiple times. For serialization, this bean should prefix its fields in a specific way so that I can reuse this bean in other classes too.

This is the JSON representation I want to use

{
  "name": "name",  // name.value
  "name_l10n": {   // name.value_l10n
    "de": "name",
    "fr": "nom"
  },
  "description": "description",  // description.value
  "description_l10n": {          // description.value_l10n
    "de": "Beschreibung",
    "fr": "description"
  }
}

The pairs name and name_l10n should be from the same bean instance. The java representation should be simple and reusable

public class MyFancyClass {
	@JsonUnwrapped
	private LocalizedField name;        // → name.value and name.value_l10n
	@JsonUnwrapped
	private LocalizedField description; // → description.value and description.value_l10n

    // ... getters and setters
}

I have provided some test cases to demonstrate the problems I have encountered so far.

  1. Without custom serialization configuration, the mapper creates invalid JSON. See testUnwrappingWithDifferentProperties
  2. Serialization works find, see testUnwrappingWithCustomSerializer
  3. Deserialization does not work, see testUnwrappingWithCustomDeserializer

kariem avatar Jun 06 '25 12:06 kariem