Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Consider removing the @JsonCreator requirement #13

Closed
lpandzic opened this issue Feb 26, 2015 · 11 comments
Closed

Consider removing the @JsonCreator requirement #13

lpandzic opened this issue Feb 26, 2015 · 11 comments

Comments

@lpandzic
Copy link
Contributor

This feature would really simplify usage and provide convience for developers.
Considering everyday development techniques I've encountered regarding this issue the most basic use case boils down to:

class AClass {

  ... mandatory field m1, ..., mn
  ... optional field o1, ..., on

  AClass(m1, ..., mn) {
  }

  ... setters for o1, .., on

  ... other methods
}

One additional argument for tackling this issue are value types which will always be immutable.

@cowtowncoder
Copy link
Member

I have an idea which would apply to jackson-databind, and might satisfy different requirements (convenience of detecting likely primary constructor; safety of not accidentally using a constructor that was not mean to be used). Given that without a no-arg constructor, @JsonCreator, or auto-detected single-argument (String, boolean, int/long, double) constructor there is no way to deserialize a value, I think it would be safe to auto-detect a multi-argument constructors if:

  1. It is the only visible constructor class has
  2. There are no @JsonCreator annotated factory methods
  3. All arguments have either a name (implicit or explicit), or are injectable

Would this make sense?

/cc @christophercurrie

@lpandzic
Copy link
Contributor Author

lpandzic commented Mar 1, 2015

Would this make sense?

Yes.
I've made a pull request with tests for this issue.

cowtowncoder added a commit that referenced this issue Mar 2, 2015
@cowtowncoder
Copy link
Member

Ok, one more related improvement:

FasterXML/jackson-databind#725

which I hope should actually further reduce the need for @JsonCreator. Fix is in master (for 2.6.0-SNAPSHOT). The basic rule is simple: if

  1. There is no other visible creator and
  2. All parameters have explicit or implicit name

a constructor will be used similar to if it was annotated. Cases that do not fall under this would be:

a. Multiple visible constructors: no attempt is made to know which one to use; no point guessing as there will always be some cases where determination impossible (same number of arguments, same visibility etc)
b. Single-argument case: due to ambiguity between delegating/properties-based variants this case does require annotation (default is to assume creator to be delegating)

@lpandzic
Copy link
Contributor Author

Nice, so after the release the tests should pass.

@lpandzic
Copy link
Contributor Author

I've tried it out locally and tests pass with jackson-databind 2.6.0-SNAPSHOT!
After release README.md should be updated to reflect those changes - min. dependency and removal of @JsonCreator in the example.

@lpandzic
Copy link
Contributor Author

This issue is solved so I'm closing it.
Maybe somewhere in the future we can come up with some smarter exact algorithm for cases of multiple visible constructors.

@cowtowncoder
Copy link
Member

Agreed.

@lpandzic
Copy link
Contributor Author

Is release planned soon?

@cowtowncoder
Copy link
Member

No immediate plans, but I do hope to start release planning once JsonParser-filtering is implemented for streaming API. The other bigger thing (protobuf dataformat) is complete.
We may be able to start Release Candidate process before end of April.

@lpandzic
Copy link
Contributor Author

Does this latest release contain this feature? (jackson-databind)

@cowtowncoder
Copy link
Member

2.6.0 has not yet been released, last one was 2.5.3, so not yet.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants