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

cloud.eppo.rac.dto.EppoAttributes Maven / Gradle / Ivy

There is a newer version: 3.3.2
Show newest version
package cloud.eppo.rac.dto;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.HashMap;
import java.util.Map;

/** Subject Attributes Class */
public class EppoAttributes extends HashMap {

  public EppoAttributes() {
    super();
  }

  public EppoAttributes(Map initialValues) {
    super(initialValues);
  }

  public String serializeToJSONString() {
    return EppoAttributes.serializeAttributesToJSONString(this);
  }

  public static String serializeAttributesToJSONString(Map attributes) {
    return EppoAttributes.serializeAttributesToJSONString(attributes, false);
  }

  public static String serializeNonNullAttributesToJSONString(Map attributes) {
    return EppoAttributes.serializeAttributesToJSONString(attributes, true);
  }

  private static String serializeAttributesToJSONString(
      Map attributes, boolean omitNulls) {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode result = mapper.createObjectNode();

    for (Map.Entry entry : attributes.entrySet()) {
      String attributeName = entry.getKey();
      Object attributeValue = entry.getValue();

      if (attributeValue instanceof EppoValue) {
        EppoValue eppoValue = (EppoValue) attributeValue;
        if (eppoValue.isNull()) {
          if (!omitNulls) {
            result.putNull(attributeName);
          }
          continue;
        }
        if (eppoValue.isNumeric()) {
          result.put(attributeName, eppoValue.doubleValue());
          continue;
        }
        if (eppoValue.isBoolean()) {
          result.put(attributeName, eppoValue.boolValue());
          continue;
        }
        // fall back put treating any other eppo values as a string
        result.put(attributeName, eppoValue.toString());
      } else if (attributeValue instanceof Double) {
        Double doubleValue = (Double) attributeValue;
        result.put(attributeName, doubleValue);
      } else if (attributeValue == null) {
        if (!omitNulls) {
          result.putNull(attributeName);
        }
      } else {
        // treat everything else as a string
        result.put(attributeName, attributeValue.toString());
      }
    }

    try {
      return mapper.writeValueAsString(result);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy