Search in sources :

Example 1 with UserEntryModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.email.UserEntryModel in project ma-modules-public by infiniteautomation.

the class EmailRecipientModelDeserializer method deserialize.

/* (non-Javadoc)
		 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
		 */
@Override
public EmailRecipientModel<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode tree = jp.readValueAsTree();
    String type = tree.get("type").asText();
    int code = EmailRecipient.TYPE_CODES.getId(type);
    switch(code) {
        case EmailRecipient.TYPE_ADDRESS:
            return (AddressEntryModel) mapper.treeToValue(tree, AddressEntryModel.class);
        case EmailRecipient.TYPE_MAILING_LIST:
            return (MailingListModel) mapper.treeToValue(tree, MailingListModel.class);
        case EmailRecipient.TYPE_USER:
            return (UserEntryModel) mapper.treeToValue(tree, UserEntryModel.class);
        default:
            throw new ModelNotFoundException(type);
    }
}
Also used : AddressEntryModel(com.serotonin.m2m2.web.mvc.rest.v1.model.email.AddressEntryModel) ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) UserEntryModel(com.serotonin.m2m2.web.mvc.rest.v1.model.email.UserEntryModel) JsonNode(com.fasterxml.jackson.databind.JsonNode) MailingListModel(com.serotonin.m2m2.web.mvc.rest.v1.model.email.MailingListModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)1 AddressEntryModel (com.serotonin.m2m2.web.mvc.rest.v1.model.email.AddressEntryModel)1 MailingListModel (com.serotonin.m2m2.web.mvc.rest.v1.model.email.MailingListModel)1 UserEntryModel (com.serotonin.m2m2.web.mvc.rest.v1.model.email.UserEntryModel)1