Vladlen Gladis
Vladlen Gladis

Reputation: 1787

How to ignore field depending on method?

How to ignore filed field depending on method?

I have a question about Spring Jackson annotations.

I have a model class. Code:

public class Tenant{
@JsonIgnore
private String id;

@JsonProperty("id")
@JsonDeserialize(using = StringValueDeserializer.class)
private String idSIMC;

@JsonProperty("name")
private String displayName;

@JsonProperty("description")
private String description;

@JsonProperty("default")
private boolean def;

@JsonProperty("geoLoc")
@JsonDeserialize(using = GeoLocationIdNameDeserializer.class)
private GeoLocation geoLoc;

@JsonProperty("asnId")
private String asnId;

@JsonProperty("devices")
@JsonDeserialize(using = StringArrayIdDeserializer.class)
private List<String> tempDevice = Collections.synchronizedList(new ArrayList<>());


@JsonIgnore 
@JsonProperty("devices") // <-- I need to add this
private List<Device> devices = Collections.synchronizedList(new ArrayList());

//getters and setters...
}

Now my question. I have method#1 that instance above class and write in tempDevice IDs of devices. method#2 get all devices from servers and filter them by tempDevice (I need to do it) can I annotate ( or something else ) my fields to be ignored as Json Objects depending on method is called?

method#1

public List<Tenant> getTenantsFromServer() {
    SSLVerification.disableSslVerification();
    ArrayList<Tenant> tenants = new ArrayList<>(0);

    String[] ids = getTenantIds();


    for(int i=0; i<ids.length; i++){
        ResponseEntity<ReturnUnique<Tenant>> result = getRestTemplate().exchange(getUrl() + SIEMCommands.ZONE_GET_ZONE + "?id=" + ids[i],
                HttpMethod.GET, new HttpEntity(getHeader()), new ParameterizedTypeReference<ReturnUnique<Tenant>>(){});
        Tenant newTenant = result.getBody().getValue();
        newTenant.setParentNode(this);

        newTenant.generateId();
        tenants.add(newTenant);
    }
    return tenants;
}

In this method i have a key "devices" in that is stored id. In method#2 another json that also have "devices" key but with another dates.(name,ip, etc.) And when I execute this method I should to store all in devices list.

JSON used in first method#1

{"return": {
"asnId": 0,
"default": false,
"description": "",
"devices": [
    {"id": 144121785900597248},
    {"id": 144121785917374464},
    {"id": 144121785934151680}
],
"geoLoc": {
    "id": {"value": 0},
    "name": ""
},
"id": {"value": 1},
"name": "HA_Zone"
}}

devices values are written in tempDevice;

method#2 use this JSON

"devices": [{
                "CRuleRight": true,
                "FTestRight": true,
                "adRight": true,
                "addDeleteRight": false,
                "children": [],
                "clientCount": 0,
                "clientStatus": "0",
                "clientVipsInSync": false,
                "deviceActionRight": true,
                "deviceDisabled": false,
                "elmFile": false,
                "elmHasSAN": false,
                "eventRight": true,
                "hostname": "",
                "ipAddress": "172.28.60.17",
                "ipsID": "144121785950928896",
                "ipsRight": true,
                "name": "ASA Admin CTX Site 2",
                "parent": false,
                "polRight": true,
                "protocol": "gsyslog",
                "reportRight": true,
                "status": "6",
                "statusAck": "0",
                "tpcCollector": "syslog",
                "tpcType": "278",
                "type": "VIPS",
                "viewRight": true,
                "vipsEnabled": true,
                "vipsID": "49",
                "vipsInSync": false
            },
            {
                "CRuleRight": true,
                "FTestRight": true,
                "adRight": true,
                "addDeleteRight": false,
                "children": [],
                "clientCount": 0,
                "clientStatus": "0",
                "clientVipsInSync": false,
                "deviceActionRight": true,
                "deviceDisabled": false,
                "elmFile": false,
                "elmHasSAN": false,
                "eventRight": true,
                "hostname": "",
                "ipAddress": "172.28.13.10",
                "ipsID": "144121785179176960",
                "ipsRight": true,
                "name": "ASA-VPN-DC1",
                "parent": false,
                "polRight": true,
                "protocol": "gsyslog",
                "reportRight": true,
                "status": "0",
                "statusAck": "0",
                "tpcCollector": "syslog",
                "tpcType": "278",
                "type": "VIPS",
                "viewRight": true,
                "vipsEnabled": true,
                "vipsID": "3",
                "vipsInSync": false
            }
        }]

this dates i have to write in devices

Upvotes: 0

Views: 802

Answers (1)

Wilson
Wilson

Reputation: 11619

If I understand correctly, you are looking for a way to deserialize 2 different json datatype having same property name into a object in different situation. If that is the case, the suggestion of using JacksonMixInAnnotations from @Thomas should work. JacksonMixInAnnotations can provide a kind of way to add annotation from another class (called mix-in class) to the target class during run time.

In your case, you can left tempDevice and devices without Jackson annotation like follows:

public class Tenant {

    private List<String> tempDevice;

    private List<Device> devices;

}

and declare 2 mix-in classes:

abstract class TempDeviceMixIn {
    @JsonProperty("devices")
    private List<String> tempDevice;
}

abstract class DeviceMixIn {
    @JsonProperty("devices")
    private List<Device> devices;
}

When you need to deserialize a json string with a string property of devices, you can add mix-in class annotation likes:

ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(Tenant.class, TempDeviceMixIn.class);
Tenant tenant = mapper.readValue(json, Tenant.class);

Deserialize a json string with a object property of devices is similar.

As you are using RestTemplate, you will need a MappingJacksonHttpMessageConverter with your ObjectMapper.

Upvotes: 1

Related Questions