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

uk.gov.service.payments.commons.api.json.JsonMapper Maven / Gradle / Ivy

There is a newer version: 1.0.20241216100655
Show newest version
package uk.gov.service.payments.commons.api.json;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.apache.commons.lang3.StringUtils.isEmpty;

public class JsonMapper {
    private ObjectMapper objectMapper;
    
    public JsonMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    public Map getAsMap(JsonNode jsonNode) {
        if (jsonNode != null) {
            if ((jsonNode.isTextual() && !isEmpty(jsonNode.asText())) || (!jsonNode.isNull() && jsonNode.isObject())) {
                try {
                    return objectMapper.readValue(jsonNode.traverse(), new TypeReference>() {});
                } catch (IOException e) {
                    throw new RuntimeException("Malformed JSON object in value", e);
                }
            }
        }
        return null;
    }    
    
    public List getAsListOfString(JsonNode jsonNode) {
        if (jsonNode != null) {
            if (jsonNode.isArray()) {
                try {
                    return objectMapper.readValue(jsonNode.traverse(), new TypeReference>() {});
                } catch (IOException e) {
                    throw new RuntimeException("Malformed JSON object in value", e);
                }
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy