0

I have a Abstact Class say "AbstractChartConfig".

// This is abstract class where I have some common properties of a chart and some methods
@Getter
@Setter
public abstract class AbstractChartConfig {
    
    protected String name;
    protected Map<String, Object> type;
    protected Map<String, Object> xAxis;
    protected Map<String, Object> yAxis;
    
    @JsonIgnore
    protected ObjectMapper mapper = new ObjectMapper()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
            ;
    
    protected abstract void setChartType(Map<String, Object> chartType);
    
    public abstract void parseChart(ChartColDto column, HashMap<String, Object> chartData);
    
}

And a concrete class

@Getter
@Setter
public class PieChart extends AbstractChartConfig {
    //Implementing the methods here and setting the properties in abstract class
    @Override
    public HashMap<String, Object> parseChart(ChartColDto column, HashMap<String, Object> chartData) {
        HashMap<String, Object> config = column.getConfig();
        setChartType(config.getChartType());
        setXAxis(config.getXAxis());
        setYAxis(config.getYAxis());
        return mapper.convertValue(this, HashMap<String, Object>);
    }
    
    @Override
    protected void setChartType(Map<String, Object> chartType){
        if(null != chartType){
            setType(chartType);
        }
    }
    
}

And say the config is like

[
    {
        "config": {
            "name": "pie",
            "chartType": {
                "type": "pie",
                "radius": "10"
            },
            "xAxis": {
                "title": "Some Title",
                "fontSize": "16px"
            },
            "yAxis": {
                "title": "Some Title",
                "fontSize": "16px"
            },
            "zAxis": {
                "title": "Some Title",
                "fontSize": "16px"
            },
            "tooltip": {
                "format": "{point.y}"
            }
        }
    }
]

Now, The Problem is when mapper is converting the class to HashMap, the properties which are defined in the abstract class is there in the final result but not the one which is not declared i.e, in this case zAxis, tooltip

Now, I know that in order to deserialize the undeclared properties, if I declare them in concrete class it works fine. But that's what I don't want to do. Is there anyway in which mapper will include the property which is not declared?

Thanks in advance.

Rakesh Kumar
  • 61
  • 1
  • 9
  • Maybe you could use something like [this](https://stackoverflow.com/questions/24263904/deserializing-polymorphic-types-with-jackson) – Lino Jul 03 '20 at 07:53
  • @Lino-Votedon'tsayThanks Thanks, but if I understand correctly, I will still have to declare the properties in the child classes which I am not willing to do. – Rakesh Kumar Jul 03 '20 at 08:15
  • Not possible then sorry, java is a statically typed language, meaning you need to know ahead of time (when writing your code) with which fields you're working – Lino Jul 03 '20 at 08:26
  • if you are unsure which axis will come, then you should define a HashMap of axis, that way you can store all incoming axis, as well check if it exist or not. – Zedex7 Jul 03 '20 at 08:46

0 Answers0