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

se.vandmo.dependencylock.maven.JsonUtils Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package se.vandmo.dependencylock.maven;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Optional;

public final class JsonUtils {
  private JsonUtils() {}

  public static String getStringValue(JsonNode json, String fieldName) {
    String value = json.get(fieldName).textValue();
    if (isBlank(value)) {
      throw new IllegalArgumentException("Missing value for "+fieldName);
    }
    return value;
  }

  public static Optional possiblyGetStringValue(JsonNode json, String fieldName) {
    if (!json.has(fieldName)) {
      return Optional.empty();
    }
    return Optional.of(getStringValue(json, fieldName));
  }

  public static JsonNode readJson(File file) {
    try {
      return new ObjectMapper().readTree(file);
    } catch (IOException ex) {
      throw new UncheckedIOException(ex);
    }
  }

  public static void writeJson(File file, JsonNode json) {
    try {
      new ObjectMapper()
          .writerWithDefaultPrettyPrinter()
          .writeValue(file, json);
    } catch (IOException ex) {
      throw new UncheckedIOException(ex);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy