Skip to content

Error in @JsonAnySetter @JsonPOJOBuilder #822

@jeffklassen

Description

@jeffklassen

I'm getting a JsonMappingException when deserializing json to BatchMetricsImpl using the Builder pattern:

com.fasterxml.jackson.databind.JsonMappingException: Problem deserializing "any" property 'docStoreRefsMs' of class  dao.impl.BatchMetricImpl$Builder 
(expected type: [simple type, class java.lang.Object]; actual type: java.lang.Integer), 
problem: object is not an instance of declaring class (through reference chain: dao.impl.Builder["docStoreRefsMs"])

Integer does inherit from Object, so I'm not sure why I'm getting this error. Can you help?

I've included the relevant JSON and BatchMetricImpl class below.

JSON:

{  
   "batchId":"testId",
   "comments":[  
      {  
         "comment":"testcomment1",
         "timestamp":"1434034631829"
      },
      {  
         "comment":"testcomment2",
         "timestamp":"1434034631868"
      }
   ],
   "errors":[  
      {  
         "error":"testerror1",
         "timestamp":"1434034631868"
      }
   ],
   "docStoreRefsMs":124,
   "batchType":"sampleType"
}

My annotations around BatchMetricImpl:

@JsonDeserialize(builder = BatchMetricImpl.Builder.class)
public class BatchMetricImpl implements BatchMetric {

    private String batchId;
    private Map<MetricProperty, Object> metricsProperties;
    private List<MetricsComment> comments;
    private List<MetricsError> errors;

    public BatchMetricImpl() {
    }

    private BatchMetricImpl(String batchId,
            Map<MetricProperty, Object> metricsProperties,
            List<MetricsComment> comments, List<MetricsError> errors) {
        this.batchId = batchId;
        this.metricsProperties = metricsProperties;
        this.comments = comments;
        this.errors = errors;
    }

    @Override
    public List<MetricsComment> getComments() {
        return this.comments;
    }

    @Override
    public List<MetricsError> getErrors() {
        return this.errors;
    }

    @Override
    @JsonAnyGetter
    public Map<MetricProperty, Object> getAllProperties() {
        return this.metricsProperties;
    }

    @Override
    public String getBatchId() {
        return this.batchId;
    }

    @Override
    public Object getProperty(MetricProperty metricsProperty) {
        if (this.metricsProperties.containsKey(metricsProperty)) {
            return this.metricsProperties.get(metricsProperty);
        } else {
            return null;
        }
    }

    @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "add")
    public static class Builder {

        private String batchId;
        private Map<MetricProperty, Object> metricProperties;
        private List<MetricsComment> comments;
        private List<MetricsError> errors;

        // default constructure to make jackson happy
        public Builder() {
        }

        public Builder(@JsonProperty("batchId") String batchId) {
            this.batchId = batchId;
            this.metricProperties = new HashMap<MetricProperty, Object>();
            this.errors = new ArrayList<MetricsError>();
            this.comments = new ArrayList<MetricsComment>();
        }

        public Builder(BatchMetric batchMetric) {
            this.batchId = batchMetric.getBatchId();
            this.metricProperties = batchMetric.getAllProperties();
            this.comments = batchMetric.getComments();
            this.errors = batchMetric.getErrors();
        }

        @JsonAnySetter
        public Builder addSingleProperty(String key, Object value)
                throws IllegalArgumentException {

            MetricProperty metricProperty = MetricProperty.get(key);

            if (!metricProperty.validateValue(value)) {
                throw new IllegalArgumentException(value
                        + " was not of the type required by " + key + ". "
                        + metricProperty.getExpectedClass() + " expected.");
            }

            this.metricProperties.put(metricProperty, metricProperty
                    .getExpectedClass().cast(value));
            return this;
        }

        public Builder addProperties(
                Map<MetricProperty, Object> metricsProperties) {
            if (metricsProperties != null) {
                this.metricProperties.putAll(metricsProperties);
            }
            return this;
        }

        public Builder addComments(List<MetricsComment> comments) {
            if (comments != null) {
                this.comments.addAll(comments);
            }
            return this;
        }

        public Builder addErrors(List<MetricsError> errors) {
            if (errors != null) {
                this.errors.addAll(errors);
            }
            return this;
        }

        public BatchMetricImpl build() {
            return new BatchMetricImpl(this.batchId, this.metricProperties,
                    this.comments, this.errors);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions