类 JsonUtils

java.lang.Object
io.github.lishangbu.orange.json.util.JsonUtils
所有已实现的接口:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.DisposableBean, org.springframework.context.ApplicationContextAware

public class JsonUtils extends Object implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.DisposableBean
JSON 工具类,提供对象与 JSON 的序列化与反序列化能力

所有方法直接抛出 JacksonException,调用方无需捕获

从以下版本开始:
2025/4/8
作者:
lishangbu
  • 构造器概要

    构造器
    构造器
    说明
     
  • 方法概要

    修饰符和类型
    方法
    说明
    void
     
    static tools.jackson.databind.json.JsonMapper
    获取 JsonMapper 单例实例
    static tools.jackson.databind.JsonNode
    readTree(byte[] content)
    字节数组转 JsonNode
    static tools.jackson.databind.JsonNode
    InputStream 转 JsonNode
    static tools.jackson.databind.JsonNode
    Reader 转 JsonNode
    static tools.jackson.databind.JsonNode
    readTree(String content)
    JSON 字符串转 JsonNode
    static <T> @Nullable T
    readValue(@Nullable byte[] src, Class<T> valueType)
    字节数组转对象
    static <T> @Nullable T
    readValue(@Nullable byte[] src, tools.jackson.core.type.TypeReference<T> valueTypeRef)
    字节数组转泛型对象
    static <T> @Nullable T
    readValue(@Nullable byte[] src, tools.jackson.databind.JavaType javaType)
    字节数组转指定类型对象
    static <T> @Nullable T
    readValue(InputStream src, Class<T> valueType)
    InputStream 转对象
    static <T> @Nullable T
    readValue(InputStream src, tools.jackson.core.type.TypeReference<T> valueTypeRef)
    InputStream 转泛型对象
    static <T> @Nullable T
    readValue(InputStream src, tools.jackson.databind.JavaType valueType)
    InputStream 转指定类型对象
    static <T> @Nullable T
    readValue(Reader src, Class<T> valueType)
    Reader 转对象
    static <T> @Nullable T
    readValue(Reader src, tools.jackson.core.type.TypeReference<T> valueTypeRef)
    Reader 转泛型对象
    static <T> @Nullable T
    readValue(Reader src, tools.jackson.databind.JavaType valueType)
    Reader 转指定类型对象
    static <T> @Nullable T
    readValue(@Nullable String content, Class<T> valueType)
    JSON 字符串转对象
    static <T> @Nullable T
    readValue(@Nullable String content, tools.jackson.core.type.TypeReference<T> valueTypeRef)
    JSON 字符串转泛型对象
    void
    setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
    注入 JsonMapper Bean
    static @Nullable String
    toJson(@Nullable Object value)
    对象转 JSON 字符串
    static byte[]
    toJsonAsBytes(@Nullable Object value)
    对象转 JSON 字节数组
    static @Nullable String
    toPrettyJson(@Nullable Object value)
    对象转格式化 JSON 字符串
  • 构造器详细资料

    • JsonUtils

      public JsonUtils()
  • 方法详细资料

    • getInstance

      public static tools.jackson.databind.json.JsonMapper getInstance()
      获取 JsonMapper 单例实例
    • toJson

      public static @Nullable String toJson(@Nullable Object value)
      对象转 JSON 字符串
      参数:
      value - 待序列化对象
      返回:
      JSON 字符串,空对象返回 null
    • toPrettyJson

      public static @Nullable String toPrettyJson(@Nullable Object value)
      对象转格式化 JSON 字符串
      参数:
      value - 待序列化对象
      返回:
      格式化 JSON 字符串,空对象返回 null
    • toJsonAsBytes

      public static byte[] toJsonAsBytes(@Nullable Object value)
      对象转 JSON 字节数组
      参数:
      value - 待序列化对象
      返回:
      JSON 字节数组,空对象返回 null
    • readTree

      public static tools.jackson.databind.JsonNode readTree(String content)
      JSON 字符串转 JsonNode
    • readTree

      public static tools.jackson.databind.JsonNode readTree(InputStream in)
      InputStream 转 JsonNode
    • readTree

      public static tools.jackson.databind.JsonNode readTree(Reader r)
      Reader 转 JsonNode
    • readTree

      public static tools.jackson.databind.JsonNode readTree(byte[] content)
      字节数组转 JsonNode
    • readValue

      public static <T> @Nullable T readValue(@Nullable String content, Class<T> valueType)
      JSON 字符串转对象
      参数:
      content - JSON 字符串
      valueType - 目标类型
      返回:
      目标对象,空字符串返回 null
    • readValue

      public static <T> @Nullable T readValue(@Nullable String content, tools.jackson.core.type.TypeReference<T> valueTypeRef)
      JSON 字符串转泛型对象
      参数:
      content - JSON 字符串
      valueTypeRef - 类型引用
      返回:
      泛型对象,空字符串返回 null
    • readValue

      public static <T> @Nullable T readValue(@Nullable byte[] src, Class<T> valueType)
      字节数组转对象
      参数:
      src - JSON 字节数组
      valueType - 目标类型
      返回:
      目标对象,空数组返回 null
    • readValue

      public static <T> @Nullable T readValue(@Nullable byte[] src, tools.jackson.core.type.TypeReference<T> valueTypeRef)
      字节数组转泛型对象
    • readValue

      public static <T> @Nullable T readValue(@Nullable byte[] src, tools.jackson.databind.JavaType javaType)
      字节数组转指定类型对象
    • readValue

      public static <T> @Nullable T readValue(InputStream src, Class<T> valueType)
      InputStream 转对象
    • readValue

      public static <T> @Nullable T readValue(InputStream src, tools.jackson.core.type.TypeReference<T> valueTypeRef)
      InputStream 转泛型对象
    • readValue

      public static <T> @Nullable T readValue(InputStream src, tools.jackson.databind.JavaType valueType)
      InputStream 转指定类型对象
    • readValue

      public static <T> @Nullable T readValue(Reader src, Class<T> valueType)
      Reader 转对象
    • readValue

      public static <T> @Nullable T readValue(Reader src, tools.jackson.core.type.TypeReference<T> valueTypeRef)
      Reader 转泛型对象
    • readValue

      public static <T> @Nullable T readValue(Reader src, tools.jackson.databind.JavaType valueType)
      Reader 转指定类型对象
    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
      注入 JsonMapper Bean
      指定者:
      setApplicationContext 在接口中 org.springframework.context.ApplicationContextAware
      抛出:
      org.springframework.beans.BeansException
    • destroy

      public void destroy() throws Exception
      指定者:
      destroy 在接口中 org.springframework.beans.factory.DisposableBean
      抛出:
      Exception