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

Support for default values #23

Open
fabriziocucci opened this issue Nov 19, 2016 · 11 comments
Open

Support for default values #23

fabriziocucci opened this issue Nov 19, 2016 · 11 comments
Labels

Comments

@fabriziocucci
Copy link

fabriziocucci commented Nov 19, 2016

Besides this stackoverflow question, I couldn't really find any information related to this topic but it would be nice to extend the support of default values in Jackson, particularly in the context of the MrBean module.

One option could be using annotations:

interface Person {
  @DefaultValue("default first name")
  String getFirstName();
  @DefaultValue("default last name")
  String getLastName();
}

Another option could be using Java 8 default methods:

interface Person {
  default String getFirstName() { return "default first name"; }
  default String getLastName() { return "default last name"; }
}

Currently, there are two main limitation I see for MrBean:

  1. No possibility to specify default values
  2. No possibility to use Java 8 Optional
@cowtowncoder
Copy link
Member

This could work easily with Strings, esp. since @JsonProperty already has property.
But it can quickly become challenging with other types, since they can't really easily use somewhat obvious (conceptually) way of using from-string functionality of deserializers (why not? due to code paths resolution almost certainly not accessible from place where default value would need to resolve).

Also note that Jackson can not yet rely on Jackson 8. This will change with Jackson 3.0, which is in relatively near future, so it's just a short-term obstacle for default methods.

Aside from these I like the idea.

@cowtowncoder cowtowncoder changed the title (mrbean) Support for default values Support for default values Jul 22, 2017
@cowtowncoder
Copy link
Member

Come to think of this, there's now

     @JsonProperty(defaultValue="default value as string")

which could be used for a few types; and primitives for sure (and Strings obviously).

@fabriziocucci
Copy link
Author

@cowtowncoder yes, it would be nice to use that.

I can see from the comment of JsonProperty#defaultValue that there was already a half thought of using that:

It is possible that in future this annotation could be used for value
defaulting, and especially for default values of Creator properties,
since they support {@link #required()} in 2.6 and above.

@cowtowncoder
Copy link
Member

Right, wiring exists such that this value is easily accessible to (de)serializers via createContextual, I think. And probably also through introspection path mrbean uses.

I don't think I have time to work on this before 2.9.0, but then again adding support would not be breaking change and could perhaps go in 2.9.x if anyone has time to work on this.

@jroper
Copy link
Member

jroper commented Dec 5, 2019

Some languages provide much richer options for providing default values than Java annotations use (eg, Scala default parameter values). Also, I wouldn't be surprised if a JEP appears that adds default value parameters to Java - it seems Java is adopting Scala features at an accelerating pace these days. I think it would be great if Jackson could be prepared for this.

One thing though, the current AnnotationIntrospector.findPropertyDefaultValue method wouldn't work, because it can only return String. A new method that returns Object would need to be defined.

@cowtowncoder
Copy link
Member

True, although it may not be necessary to use this mechanism for figuring out default values that come from other metadata. JsonDeserializer has (for example) getNullValue() and getEmptyValue() so it might be possible to use alternate approaches and leave annotation introspector focused on just that source.
Or perhaps it would make sense to consider ValueInstantiator to be the extension point.
Kotlin module does handle default value injection this way, I think, since typically all values are passed via constructor.

@cowtowncoder
Copy link
Member

Also: this issue probably belongs to jackson-databind more than Mr Bean module.
Leaving here for now, may be re-filed in databind if there are plans to actually work on support (and if there isn't one on databind, I have a feeling there might actually be an issue).
More likely would be implemented for 3.0, not very likely for 2.x.

@Ghilteras
Copy link

Why was this issue closed? It seems it's still not possible to assign a default value when deserializing with Jackson latest version, not even when using defaultValue

@cowtowncoder
Copy link
Member

@Ghilteras it was mostly closed because actual support for using annotated default values would be in databind:

FasterXML/jackson-databind#618

and since no one has had time or interest to work on other parts here. If you feel like you would like to work on something, feel free to reopen.

@Ghilteras
Copy link

@cowtowncoder I'm not sure I understand the reference to another closed issue in a different repo which was closed 3 years ago. The issue should be opened somewhere as a feature request if nobody ended up addressing it, either here or on jackson-databind, wherever you guys feel more comfortable tracking it.

@cowtowncoder
Copy link
Member

cowtowncoder commented May 14, 2020

@Ghilteras what closed issue? Now I am confused.

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

No branches or pull requests

4 participants