content: /*
   * Copyright 2023-2025 Licensed under the Dual Licensing
   * website: https://aizuda.com
   */
  package com.aizuda.bpm.spring.adaptive;
  
  import com.aizuda.bpm.engine.assist.Assert;
  import com.aizuda.bpm.engine.handler.FlowJsonHandler;
  import com.fasterxml.jackson.annotation.JsonInclude;
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.DeserializationFeature;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  
  /**
   * Jackson JSON 解析处理器接口
   *
   * <p>
   * <a href="https://aizuda.com">官网</a>尊重知识产权，不允许非法使用，后果自负
   * </p>
   *
   * @author hubin
   * @since 1.0
   */
  public class FlowJacksonHandler implements FlowJsonHandler {
      private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  
      static {
          OBJECT_MAPPER.registerModule(new JavaTimeModule());
          OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
          OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
      }
  
      @Override
      public String toJson(Object object) {
          if (null == object) {
              return null;
          }
          try {
              return OBJECT_MAPPER.writeValueAsString(object);
          } catch (JsonProcessingException e) {
              throw Assert.throwable(e);
          }
      }
  
      @Override
      public <T> T fromJson(String jsonString, Class<T> clazz) {
          if (null == jsonString || null == clazz) {
              return null;
          }
          try {
              return OBJECT_MAPPER.readValue(jsonString, clazz);
          } catch (JsonProcessingException e) {
              throw Assert.throwable(e);
          }
      }
  }
  