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

com.teststeps.thekla4j.utils.yaml.YAML Maven / Gradle / Ivy

package com.teststeps.thekla4j.utils.yaml;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.teststeps.thekla4j.utils.json.exception.JsonParseException;
import com.teststeps.thekla4j.utils.json.exception.JsonStringifyException;
import com.teststeps.thekla4j.utils.json.serialization.LocalDateTimeDeserializer;
import com.teststeps.thekla4j.utils.json.serialization.LocalDateTimeSerializer;
import io.vavr.Function1;
import io.vavr.control.Try;

import java.time.LocalDateTime;

public class YAML {
  private static final ObjectMapper yamlNonNullMapper =
    new YAMLMapper()
      .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
      .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
      .setSerializationInclusion(JsonInclude.Include.NON_NULL)
      .registerModule(new JavaTimeModule())
      .registerModule(new SimpleModule().addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()))
      .registerModule(new SimpleModule().addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()));


  public static  String jStringify(T obj) {
    try {
      return yamlNonNullMapper.writeValueAsString(obj);
    } catch (Exception e) {
      throw JsonStringifyException.withMessageAndCause(e.getMessage(), e);
    }
  }

  public static  Try jParse(String json, Class clazz) {
    return Try.of(() -> yamlNonNullMapper.readValue(json, clazz));
  }

  public static  Function1> jParse(Class clazz) {
    return json -> Try.of(() -> yamlNonNullMapper.readValue(json, clazz));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy