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

Leverage java 8 Parameter Name support to allow @JsonCreator to be used for schema generation #86

Open
jmax01 opened this issue Jul 21, 2015 · 3 comments

Comments

@jmax01
Copy link

jmax01 commented Jul 21, 2015

Is it possible with Parameter Name support an @JsonCreator annotated constructor could be all that is needed to manage column order and header names?

This would save lots of duplicate and easy to mess up @JsonProperty code.

Users would be required to have their parameter names match their field/accessors names.

Current Code:

    @JsonPropertyOrder({ "E-Trade ID", "MY-Trade ID" })
    public static class EtradeAccount {

        @JsonProperty("E-Trade ID")
        private final String eTradeId;

        @JsonProperty("MY-Trade ID")
        private final String myTradeId;

        @JsonCreator
        public EtradeAccount(@JsonProperty("E-Trade ID") String eTradeId, @JsonProperty("MY-Trade ID") String myTradeId) {
            super();
            this.eTradeId = eTradeId;
            this.myTradeId = myTradeId;
        }

       //Workaround single character acronym issue
        @JsonProperty("E-Trade ID")
        public String getETradeId() {
            return this.eTradeId;
        }

        public String getMyTradeId() {
            return this.myTradeId;
        }

    }

Leveraging @JsonCreator and Parameter Names

    public static class EtradeAccount {

        private final String eTradeId;

        private final String myTradeId;

        @JsonCreator
        public EtradeAccount( @JsonProperty("E-Trade ID") String eTradeId, @JsonProperty("MY-Trade ID") String myTradeId) {
            super();
            this.eTradeId = eTradeId;
            this.myTradeId = myTradeId;
        }

        public String getETradeId() {
            return this.eTradeId;
        }

        public String getMyTradeId() {
            return this.myTradeId;
        }

    }```
@cowtowncoder
Copy link
Member

All of the code for this actually resides in jackson-databind, as ordering is handled there, and CSV module has no power over handling, at least currently.

I may be wrong, but I thought that creator properties are indeed sorted before other properties (with exception of possible Type and Object Ids), unless there is explicit @JsonPropertyOrder.

However... actually, this may currently conflict with one CSV-specifity: defaulting to alphabetic ordering of properties. If so, this is what changes ordering.
And if so, it would perhaps make sense to allow feature or setting that changes behavior to use @JsonCreator indicated ordering, and only use alphabetic order for remainder of properties, if any.

This would need to go in 2.7, and not be default, to retain full backwards compatibility.
It could actually be a MapperFeature (generic to all formats) as I assume it would make sense for JSON, XML, YAML as well. It can not be SerializationFeature just because setting can not be dynamically change, as ordering is fixed once BeanSerializer is constructed (or, rather, it is fixed for a given JsonSerializer instance: new instances may be constructed, but existing one can not be modified).

@damianball
Copy link

Would something like jackson-module-parameter-names make more sense? It seems even cleaner given you can exclude the @JsonCreator and @JsonProperty fields on/in the constructor.

@cowtowncoder
Copy link
Member

Agreed, parameter name information should come through use of existing module (whether it's still stand-alone with 2.8, or embedded, which might happen).

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

3 participants