public class JsonUtil extends Object
This class provides methods to convert Java objects to JSON strings and vice versa.
It includes a default ObjectMapper
instance for common use cases,
while also allowing custom ObjectMapper
instances for specific configurations.
Constructor and Description |
---|
JsonUtil() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
fromJson(InputStream json,
Class<T> clazz) |
static <T> T |
fromJson(InputStream json,
com.fasterxml.jackson.databind.ObjectMapper objectMapper,
Class<T> clazz)
Converts an InputStream to an object of the specified class using Jackson ObjectMapper.
|
static <T> T |
fromJson(InputStream json,
com.fasterxml.jackson.databind.ObjectMapper objectMapper,
com.fasterxml.jackson.core.type.TypeReference<T> typeReference) |
static <T> T |
fromJson(InputStream json,
com.fasterxml.jackson.core.type.TypeReference<T> typeReference) |
static String |
toJson(Object object) |
static String |
toJson(Object object,
com.fasterxml.jackson.databind.ObjectMapper mapper)
Converts an object to a JSON string using Jackson ObjectMapper.
|
public static String toJson(Object object, com.fasterxml.jackson.databind.ObjectMapper mapper)
object
- the object to be converted to JSONmapper
- the ObjectMapper instanceApiException
- if there is a JSON processing or mapping errorpublic static <T> T fromJson(InputStream json, Class<T> clazz)
public static <T> T fromJson(InputStream json, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<T> clazz)
T
- the type of the desired objectjson
- the InputStream containing JSON dataobjectMapper
- the ObjectMapper instanceclazz
- the Class object of the desired typeApiException
- if there is a JSON parsing or mapping errorApiConnectionException
- if there is an I/O errorpublic static <T> T fromJson(InputStream json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
public static <T> T fromJson(InputStream json, com.fasterxml.jackson.databind.ObjectMapper objectMapper, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
Copyright © 2024. All rights reserved.