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

generator.server.springboot.cucumber.rest.CucumberJson.mustache Maven / Gradle / Ivy

There is a newer version: 1.18.1
Show newest version
package {{packageName}}.cucumber.rest;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

final class CucumberJson {

  private static final ObjectMapper jsonMapper = jsonMapper();

  private CucumberJson() {}

  public static ObjectMapper jsonMapper() {
    return JsonMapper.builder()
      .serializationInclusion(JsonInclude.Include.NON_NULL)
      .addModule(new JavaTimeModule())
      .addModules(new Jdk8Module())
      .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
      .disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)
      .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
      .build();
  }

  public static String pretty(String json) {
    if (StringUtils.isBlank(json)) {
      return json;
    }

    try {
      return jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonMapper.readValue(json, Object.class));
    } catch (IOException e) {
      return json;
    }
  }

  public static String toCamelCase(String value) {
    if (value == null) {
      return null;
    }

    return Arrays.stream(value.split("\\.")).map(CucumberJson::camelCaseField).collect(Collectors.joining("."));
  }

  private static String camelCaseField(String value) {
    return StringUtils.uncapitalize(Arrays.stream(value.split(" ")).map(StringUtils::capitalize).collect(Collectors.joining()));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy