-
-
Notifications
You must be signed in to change notification settings - Fork 343
Add @JsonWrapped annotation #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sri-adarsh-kumar
wants to merge
4
commits into
FasterXML:2.x
Choose a base branch
from
sri-adarsh-kumar:feature/512-json-wrapped-annotation
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−0
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
61cf530
Add @JsonWrapped annotation (moved from jackson-databind)
sri-adarsh-kumar 1ca9412
feat(JsonWrapped): add `enabled` flag and simplify tests
sri-adarsh-kumar 6538d70
Fix issue ref (must refer to in-repo issue or PR)
cowtowncoder d263202
Merge branch '2.x' into feature/512-json-wrapped-annotation
sri-adarsh-kumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/com/fasterxml/jackson/annotation/JsonWrapped.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package com.fasterxml.jackson.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Annotation that groups one or more bean properties into a synthetic | ||
| * nested JSON object during serialization, and extracts them back during | ||
| * deserialization. This is the inverse of {@link JsonUnwrapped}. | ||
| * | ||
| * <p>Multiple fields annotated with the same {@code value()} are grouped into | ||
| * a single wrapper object. Inner property names follow Jackson's standard naming | ||
| * ({@code @JsonProperty} or default). | ||
| * | ||
| * <p>Example: given a POJO such as: | ||
| * <pre> | ||
| * public class Gene { | ||
| * public String name; | ||
| * | ||
| * @JsonWrapped("chr") | ||
| * public String chromosome; | ||
| * | ||
| * @JsonWrapped("chr") | ||
| * public int position; | ||
| * } | ||
| * </pre> | ||
| * serialization produces: | ||
| * <pre> | ||
| * { | ||
| * "name" : "BRCA1", | ||
| * "chr" : { | ||
| * "chromosome" : "17", | ||
| * "position" : 43044295 | ||
| * } | ||
| * } | ||
| * </pre> | ||
| * | ||
| * <p>Constraints: | ||
| * <ul> | ||
| * <li>Non-scalar field types (POJOs, collections, maps, arrays) are supported for | ||
| * baseline serialization and deserialization. Each inner field serializes under | ||
| * its own name within the wrapper object. Note: existing interaction limitations | ||
| * around {@code @JsonView}, {@code @JsonFilter}, and {@code @JsonInclude} on | ||
| * inner wrapped fields still apply — see the remaining bullets below.</li> | ||
| * <li>The wrapper name ({@code value()}) must be non-empty, unless explicitly disabling | ||
| * wrapping: an empty {@code value()} ({@code @JsonWrapped("")}) disables wrapping — | ||
| * useful in mix-ins to suppress wrapping defined in a supertype.</li> | ||
| * <li>The wrapper name must not conflict with an existing non-wrapped property on the same bean.</li> | ||
| * <li>Not supported on {@code @JsonCreator} constructor or factory-method parameters.</li> | ||
| * <li>MVP limitation: {@code @JsonView} on inner wrapped fields is ignored — the wrapper | ||
| * is always emitted and all inner fields are always included regardless of active view.</li> | ||
| * <li>MVP limitation: class-level {@code @JsonFilter} still applies to the wrapper property | ||
| * by its wrapper name (the whole wrapper can be suppressed if the filter excludes it), | ||
| * but inner fields are not individually filtered.</li> | ||
| * <li>MVP limitation: class-level {@code @JsonInclude} (e.g. {@code NON_NULL}) still applies | ||
| * to inner wrapped fields during serialization.</li> | ||
| * </ul> | ||
| * | ||
| * @see JsonUnwrapped | ||
| * @since 2.22 | ||
| */ | ||
| @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @JacksonAnnotation | ||
| public @interface JsonWrapped { | ||
| /** | ||
| * Single-level wrapper object name (e.g. "chr"). | ||
| * An empty string disables wrapping (useful in mix-ins to suppress | ||
| * wrapping defined in a supertype). | ||
| */ | ||
| String value(); | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
src/test/java/com/fasterxml/jackson/annotation/JsonWrappedTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.fasterxml.jackson.annotation; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class JsonWrappedTest | ||
| extends AnnotationTestUtil | ||
| { | ||
| private static class TestClass { | ||
| @JsonWrapped("chr") | ||
| public String chromosome; | ||
|
|
||
| @JsonWrapped("chr") | ||
| public int position; | ||
| } | ||
|
|
||
| private static class TestClassWithGetter { | ||
| private String data; | ||
|
|
||
| @JsonWrapped("wrapper") | ||
| public String getData() { | ||
| return data; | ||
| } | ||
| } | ||
|
|
||
| private static class TestClassEmptyWrapper { | ||
| @JsonWrapped("") | ||
| public String field; | ||
| } | ||
|
|
||
| @Test | ||
| public void testAnnotationRetentionAtRuntime() throws Exception { | ||
| // Verify annotation is retained at runtime | ||
| JsonWrapped ann = TestClass.class.getField("chromosome").getAnnotation(JsonWrapped.class); | ||
| assertNotNull(ann, "Annotation should be retained at runtime"); | ||
| assertEquals("chr", ann.value()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAnnotationValue() throws Exception { | ||
| JsonWrapped ann = TestClass.class.getField("chromosome").getAnnotation(JsonWrapped.class); | ||
| assertEquals("chr", ann.value()); | ||
|
|
||
| JsonWrapped ann2 = TestClass.class.getField("position").getAnnotation(JsonWrapped.class); | ||
| assertEquals("chr", ann2.value()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyStringValue() throws Exception { | ||
| JsonWrapped ann = TestClassEmptyWrapper.class.getField("field").getAnnotation(JsonWrapped.class); | ||
| assertEquals("", ann.value()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAnnotationOnMethod() throws Exception { | ||
| JsonWrapped ann = TestClassWithGetter.class.getMethod("getData").getAnnotation(JsonWrapped.class); | ||
| assertNotNull(ann, "Annotation should be applicable to methods"); | ||
| assertEquals("wrapper", ann.value()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.