Skip to content
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

Add @Json.Creator #189

Merged
merged 10 commits into from
Nov 30, 2023
Merged

Add @Json.Creator #189

merged 10 commits into from
Nov 30, 2023

Conversation

SentryMan
Copy link
Collaborator

@SentryMan SentryMan commented Nov 29, 2023

We can annotate a constructor/factory method with @Json.Creator to get more control on how an object gets deserialized. (or use mix-ins to switch things up entirely)

resolves #187

@rbygrave rbygrave added this to the 1.9 milestone Nov 30, 2023
@rbygrave rbygrave enabled auto-merge (squash) November 30, 2023 09:58
@rbygrave rbygrave merged commit 14c60f7 into avaje:main Nov 30, 2023
2 checks passed
rbygrave added a commit that referenced this pull request Nov 30, 2023
rbygrave added a commit that referenced this pull request Nov 30, 2023
Copy link
Contributor

@rbygrave rbygrave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note on changes I've made.

@@ -139,6 +139,16 @@ public boolean hasJsonAnnotation() {

@Override
public void read() {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format

@@ -470,6 +481,8 @@ private void writeJsonBuildResult(Append writer, String varName) {
// assuming name matches field here?
writer.append(constructorParamName(name + (frequency == 0 ? "" : frequency.toString())));
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invert if

return element;
}

public VariableElement getElement() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused

jsonCreator =
jsonCreator.or(
() ->
baseType.getEnclosedElements().stream()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refractor extract method

jsonCreator
.map(
ex -> {
var mods = ex.getModifiers();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor extract method

TypeReader(TypeElement baseType, TypeElement mixInType, NamingConvention namingConvention, String typePropertyKey) {
private final boolean hasJsonCreator;

TypeReader(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format

* @Json
* public record Product( ... ) {
*
* public static Product factory(@Json.Alias("alias") String name){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing annotation in example javadoc

@rbygrave rbygrave modified the milestones: 1.9, 1.10 Dec 16, 2023
@SentryMan SentryMan deleted the creator branch February 12, 2024 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a functionality similar to Jackson's @JsonCreator
2 participants