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

org.approvaltests.JsonJacksonApprovals Maven / Gradle / Ivy

There is a newer version: 24.9.0
Show newest version
package org.approvaltests;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spun.util.ObjectUtils;
import org.approvaltests.core.Options;

public class JsonJacksonApprovals
{
  public static void verifyAsJson(Object o)
  {
    verifyAsJson(o, new Options());
  }
  public static void verifyAsJson(Object o, Options options)
  {
    Approvals.verify(asJson(o), options.forFile().withExtension(".json"));
  }
  public static String asJson(Object o)
  {
    try
    {
      ObjectMapper objectMapper = new ObjectMapper();
      return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
    }
    catch (JsonProcessingException e)
    {
      throw ObjectUtils.throwAsError(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy