如何在HashMap中存储继承类的对象作为键(无法保留子类属性)

时间:2018-02-07 15:51:37

标签: json spring-boot inheritance hashmap google-dfp

我试图存储一个类的对象" OrderForCalendar"继承自"Order" class of DFP。但是生成的JSON不包含继承类的自定义属性。

继承类:OrderForCalendar

import com.google.api.ads.dfp.axis.v201708.Order;

public class OrderForCalendar extends Order
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String campaignCalendarStatus;
    /**
     * @return the campaignCalendarStatus
     */
    public String getCampaignCalendarStatus()
    {
        return campaignCalendarStatus;
    }
    /**
     * @param campaignCalendarStatus the campaignCalendarStatus to set
     */
    public void setCampaignCalendarStatus(String campaignCalendarStatus)
    {
        this.campaignCalendarStatus = campaignCalendarStatus;
    }
}

如您所见," campaignCalendarStatus"是我在继承类中添加的唯一属性。 现在,我是一个HashMap,

HashMap <OrderForCalendar, ArrayList<LineItemForCalendar>> ordLineItemsMap = new HashMap<>();, 哪些商店&#34; OrderForCalendar&#34;作为&#34; LineItemForCalendar&#34;的键和ArrayList;作为价值。

if (orderPage.getResults() != null)
                for (Order order : orderPage.getResults())
                {
                    OrderForCalendar orderForCalendar = new OrderForCalendar();
                    BeanUtils.copyProperties(order, orderForCalendar);
                    orderForCalendar.setCampaignCalendarStatus(queryKey);
                    ordLineItemsMap.put(orderForCalendar, ordIdLineItemsMap.get(orderForCalendar.getId()));
                }
            jElem = gObj.toJsonTree(ordLineItemsMap);

说明:

orderPage: DFP广告管理系统"Order"类型的订单数组。

BeanUtils.copyProperties(order,orderForCalendar); :我将父对象复制到子对象。

orderForCalendar.setCampaignCalendarStatus(queryKey); :我将一些字符串设置为OrderForCalendar的本地属性。

ordLineItemsMap.put(orderForCalendar,ordIdLineItemsMap.get(orderForCalendar.getId())); :我正在存储&#34; orderForCalendar&#34;对象及其相应的ArrayList of&#34; LineItemForCalendar&#34;在HashMap中。

jElem = gObj.toJsonTree(ordLineItemsMap); :最后我将该HashMap对象转换为Gson的JsonElement。

当我返回jElem.toString()时,生成的JSON不包含我的自定义属性&#34; campaignCalendarStatus &#34;在OrderForCalendar对象中。

"OrderForCalendar{advertiserId=40595, creatorId=2954556, currencyCode=USD, externalOrderId=0, id=278756, isArchived=false, isProgrammatic=false, lastModifiedByApp=DFP, lastModifiedDateTime=DateTime{date=Date{day=18, month=10, year=2017}, hour=0, minute=19, second=39, timeZoneID=PST8PDT}, name=Livy Active, notes=Ative lines will deliver with unlimited end date., startDateTime=DateTime{date=Date{day=19, month=9, year=2017}, hour=18, minute=11, second=0, timeZoneID=Asia/Dubai}, status=APPROVED, totalBudget=Money{currencyCode=USD, microAmount=0}, totalClicksDelivered=6782, totalImpressionsDelivered=314646, traffickerId=3962, unlimitedEndDateTime=true}"

完成JSON结果:

{
            "OrderForCalendar{advertiserId=40595, creatorId=2954556, currencyCode=USD, externalOrderId=0, id=278756, isArchived=false, isProgrammatic=false, lastModifiedByApp=DFP, lastModifiedDateTime=DateTime{date=Date{day=18, month=10, year=2017}, hour=0, minute=19, second=39, timeZoneID=PST8PDT}, name=Livy Active, notes=Ative lines will deliver with unlimited end date., startDateTime=DateTime{date=Date{day=19, month=9, year=2017}, hour=18, minute=11, second=0, timeZoneID=Asia/Dubai}, status=APPROVED, totalBudget=Money{currencyCode=USD, microAmount=0}, totalClicksDelivered=6782, totalImpressionsDelivered=314646, traffickerId=3962, unlimitedEndDateTime=true}": [
                {
                    "id": 3455301609,
                    "start": "2017-09-19T18:15:00",
                    "title": "KJH Active",
                    "status": "needs_creative",
                    "description": "DELIVERING",
                    "advertiser_name": "Givenchy",
                    "flag": true
                },
                {
                    "id": 3455822625,
                    "start": "2017-10-16T18:20:00",
                    "title": "KAY Joir",
                    "status": "needs_creative",
                    "description": "PAUSED_INVENTORY_RELEASED",
                    "advertiser_name": "IIlyu",
                    "flag": true
                }
            ]
}

似乎存储了原始DFP订单的唯一值,但未存储子类的自定义属性。

如果需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

toString()属性实际上就在那里。

您所使用的是将复杂对象用作地图中的关键字。根据JSON规范,此映射没有有效的JSON表示,因为JSON键必须是字符串。

因此,您使用的JSON库将您的地图回退序列化为Order返回的内容并将其用作关键字。当然,toString()类正在覆盖Order方法,当然,campaignCalendarStatus类对OrderForCalendar子类的新this.getClass().getSimpleName() + {field1= + field1Value + ...}属性一无所知。它只返回field1..N之类的内容,其中Order对应toString()类的字段。

如果您希望包含新属性,则必须覆盖OrderForCalendar类中的campaignCalendarStatus方法,并包含其{{1}}字段的值。但是,这不是IMO的好方法,您应该考虑使用字符串作为地图的关键。