I'm facing an issue when generating a JSON dynamically.I've created different inner class to represent the JSON:
public class OrderActions {
public OrderActions(String type, Object addproduct, Object removeProduct) {
SM003_Order.TriggerDate triggerdateCE = new SM003_Order.TriggerDate('ContractEffective');
SM003_Order.TriggerDate triggerdateSA = new SM003_Order.TriggerDate('ServiceActivation');
SM003_Order.TriggerDate triggerdateCA = new SM003_Order.TriggerDate('CustomerAcceptance');
List<SM003_Order.TriggerDate> triggerDates = new List<SM003_Order.TriggerDate> ();
triggerDates.add(triggerdateCA);
triggerDates.add(triggerdateCE);
triggerDates.add(triggerdateSA);
this.triggerDates = triggerDates;
this.type = type;
//it's either add or remove, can't be both
if (addproduct != null) {
this.Action = addproduct;
}
if (removeProduct != null) {
this.Action = removeProduct;
}
}
public List<TriggerDate> triggerDates { get; set; }
public String type { get; set; }
public Object Action { get; set; }
}
Depending on if i want to add a product or remove a product the Action Object can be instantiated in two differents types: Add Product:
public class AddProduct {
public AddProduct() {
}
public AddProduct(String productRatePlanId, List<ChargeOverrides> chargeOverrides) {
this.chargeOverrides = chargeOverrides;
this.productRatePlanId = productRatePlanId;
}
public String productRatePlanId { get; set; }
public List<ChargeOverrides> chargeOverrides { get; set; }
}
or Remove Product:
public class RemoveProduct {
public RemoveProduct(String ratePlanId) {
this.ratePlanId = ratePlanId;
}
public String ratePlanId { get; set; }
}
The problem is that when i serialize the the whole object the JSON is generated like this (add product):
"orderActions": [
{
"type": "AddProduct",
"triggerDates": [
{
"name": "ContractEffective",
"triggerDate": "2020-03-27"
},
{
"name": "ServiceActivation",
"triggerDate": "2020-03-27"
},
{
"name": "CustomerAcceptance",
"triggerDate": "2020-03-27"
}
],
"Action": {
"productRatePlanId": "8adc9dee710b7521017116c56aa95670",
"chargeOverrides": [
{
"productRatePlanChargeId": "8adc8f99710b7523017116c5cae87a13",
"pricing": {
"recurringFlatFee": {
"listPrice": 17
}
}
}
]
}
}
]
or like this (Remove product):
"orderActions": [
{
"type": "RemoveProduct",
"triggerDates": [
{
"name": "ContractEffective",
"triggerDate": "2020-03-25"
},
{
"name": "ServiceActivation",
"triggerDate": "2020-03-25"
},
{
"name": "CustomerAcceptance",
"triggerDate": "2020-03-25"
}
],
"Action": {
"ratePlanId": "8adce4216b9de86d016bb3c15fbb0a2f"
}
}
]
Which is fine except for The key "action" that has to be named either 'addProduct' or 'removeProduct' to respect a specific format. But because of my generic constructor it doesn't generate the good value for this key... Do you guys know how could i change the value of this key?
Cheers!
addProduct
andremoveProduct
and set them based the if condition, 3. instead of Action, name the variable as something unique likeACTION_REPLACEMENT
and after deserializing replaceACTION_REPLACEMENT
withaddProduct
orremoveProduct
– manjit5190 Mar 27 '20 at 16:33