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

com.arextest.schedule.common.JsonUtils Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package com.arextest.schedule.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author wildeslam.
 * @create 2023/11/23 16:20
 */
@Slf4j
public class JsonUtils {
  private static final ObjectMapper objectMapper = new ObjectMapper();

  public static   T byteToObject(byte[] bytes, Class tClazz) {
    try {
      return objectMapper.readValue(bytes, tClazz);
    } catch (IOException e) {
      LOGGER.error("byteToObject error:{}", e.getMessage(), e);
    }
    return null;
  }

  public static   T jsonStringToObject(String string, Class tClazz) {
    try {
      return objectMapper.readValue(string, tClazz);
    } catch (IOException e) {
      LOGGER.error("jsonStringToObject error:{}", e.getMessage(), e);
    }
    return null;
  }

  public static String objectToJsonString(Object value) {
    try {
      return objectMapper.writeValueAsString(value);
    } catch (IOException e) {
      LOGGER.error("objectToJsonString error:{}", e.getMessage(), e);
    }
    return StringUtils.EMPTY;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy