jackson-dataformat-csv icon indicating copy to clipboard operation
jackson-dataformat-csv copied to clipboard

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

Open jmax01 opened this issue 10 years ago • 3 comments

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;
        }

    }```

jmax01 avatar Jul 21 '15 19:07 jmax01

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).

cowtowncoder avatar Jul 21 '15 22:07 cowtowncoder

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.

damianball avatar May 18 '16 08:05 damianball

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).

cowtowncoder avatar May 18 '16 14:05 cowtowncoder