All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.contrastsecurity.utils.MetadataDeserializer Maven / Gradle / Ivy

There is a newer version: 3.4.2
Show newest version
package com.contrastsecurity.utils;

import com.contrastsecurity.models.FreeformMetadata;
import com.contrastsecurity.models.MetadataEntity;
import com.contrastsecurity.models.NumericMetadata;
import com.contrastsecurity.models.PointOfContactMetadata;
import com.google.gson.*;

import java.lang.reflect.Type;

public class MetadataDeserializer implements JsonDeserializer {
    @Override
    public MetadataEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonObject jsonObject = json.getAsJsonObject();
        JsonElement type = jsonObject.get("type");
        if (type != null) {
            switch (type.getAsString()) {
                case "STRING":
                    return context.deserialize(jsonObject,
                            FreeformMetadata.class);
                case "NUMERIC":
                    return context.deserialize(jsonObject,
                            NumericMetadata.class);
                case "PERSON_OF_CONTACT":
                    PointOfContactMetadata poiMetadata = new PointOfContactMetadata();
                    poiMetadata.setFieldName(jsonObject.get("fieldName").getAsString());
                    JsonArray subFields = jsonObject.getAsJsonArray("subfields");
                    for (JsonElement subfieldElement: subFields) {
                        JsonObject subField = subfieldElement.getAsJsonObject();
                        JsonElement subfieldType = subField.get("type");
                        switch (subfieldType.getAsString()) {
                            case "CONTACT_NAME":
                                poiMetadata.setContactName(subField.get("fieldValue").getAsString());
                                break;
                            case "EMAIL":
                                poiMetadata.setEmail(subField.get("fieldValue").getAsString());
                                break;
                            case "PHONE":
                                poiMetadata.setPhoneNo(subField.get("fieldValue").getAsString());
                                break;
                        }
                    }
                    return poiMetadata;
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy