/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
*
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/lgpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springblade.core.tool.jackson;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.type.CollectionLikeType;
import com.fasterxml.jackson.databind.type.MapType;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.*;
import org.springframework.lang.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.*;
/**
* Jackson工具类
*
* @author Chill
*/
@Slf4j
public class JsonUtil {
/**
* 将对象序列化成json字符串
*
* @param value javaBean
* @param T 泛型标记
* @return jsonString json字符串
*/
public static String toJson(T value) {
try {
return getInstance().writeValueAsString(value);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* 将对象序列化成 json byte 数组
*
* @param object javaBean
* @return jsonString json字符串
*/
public static byte[] toJsonAsBytes(Object object) {
try {
return getInstance().writeValueAsBytes(object);
} catch (JsonProcessingException e) {
throw Exceptions.unchecked(e);
}
}
/**
* 将json反序列化成对象
*
* @param content content
* @param valueType class
* @param T 泛型标记
* @return Bean
*/
public static T parse(String content, Class valueType) {
try {
return getInstance().readValue(content, valueType);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* 将json反序列化成对象
*
* @param content content
* @param typeReference 泛型类型
* @param T 泛型标记
* @return Bean
*/
public static T parse(String content, TypeReference typeReference) {
try {
return getInstance().readValue(content, typeReference);
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
}
/**
* 将json byte 数组反序列化成对象
*
* @param bytes json bytes
* @param valueType class
* @param T 泛型标记
* @return Bean
*/
public static T parse(byte[] bytes, Class valueType) {
try {
return getInstance().readValue(bytes, valueType);
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
}
/**
* 将json反序列化成对象
*
* @param bytes bytes
* @param typeReference 泛型类型
* @param T 泛型标记
* @return Bean
*/
public static T parse(byte[] bytes, TypeReference typeReference) {
try {
return getInstance().readValue(bytes, typeReference);
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
}
/**
* 将json反序列化成对象
*
* @param in InputStream
* @param valueType class
* @param T 泛型标记
* @return Bean
*/
public static T parse(InputStream in, Class valueType) {
try {
return getInstance().readValue(in, valueType);
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
}
/**
* 将json反序列化成对象
*
* @param in InputStream
* @param typeReference 泛型类型
* @param T 泛型标记
* @return Bean
*/
public static T parse(InputStream in, TypeReference typeReference) {
try {
return getInstance().readValue(in, typeReference);
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
}
/**
* 将json反序列化成List对象
* @param content content
* @param valueTypeRef class
* @param T 泛型标记
* @return List
*/
public static List parseArray(String content, Class valueTypeRef) {
try {
if (!StringUtil.startsWithIgnoreCase(content, StringPool.LEFT_SQ_BRACKET)) {
content = StringPool.LEFT_SQ_BRACKET + content + StringPool.RIGHT_SQ_BRACKET;
}
List