JsonUtil.java 18.3 KB
Newer Older
如梦技术's avatar
如梦技术 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
 * <p>
 * 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
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * 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 net.dreamlu.mica.core.utils;

19
import com.fasterxml.jackson.core.JsonFactory;
如梦技术's avatar
如梦技术 已提交
20 21
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
如梦技术's avatar
如梦技术 已提交
22
import com.fasterxml.jackson.core.TreeNode;
23
import com.fasterxml.jackson.core.json.JsonReadFeature;
如梦技术's avatar
如梦技术 已提交
24
import com.fasterxml.jackson.core.type.TypeReference;
25
import com.fasterxml.jackson.databind.*;
26 27
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
如梦技术's avatar
如梦技术 已提交
28 29
import com.fasterxml.jackson.databind.type.CollectionLikeType;
import com.fasterxml.jackson.databind.type.MapType;
如梦技术's avatar
如梦技术 已提交
30
import lombok.experimental.UtilityClass;
31
import net.dreamlu.mica.core.function.CheckedConsumer;
如梦技术's avatar
如梦技术 已提交
32
import org.springframework.lang.Nullable;
如梦技术's avatar
如梦技术 已提交
33 34 35

import java.io.IOException;
import java.io.InputStream;
36
import java.text.SimpleDateFormat;
如梦技术's avatar
如梦技术 已提交
37
import java.time.ZoneId;
如梦技术's avatar
如梦技术 已提交
38
import java.util.*;
如梦技术's avatar
如梦技术 已提交
39 40 41

/**
 * json 工具类
如梦技术's avatar
如梦技术 已提交
42
 *
如梦技术's avatar
如梦技术 已提交
43 44 45 46 47 48 49 50 51 52 53
 * @author L.cm
 */
@UtilityClass
public class JsonUtil {

	/**
	 * 将对象序列化成json字符串
	 *
	 * @param object javaBean
	 * @return jsonString json字符串
	 */
如梦技术's avatar
如梦技术 已提交
54 55 56 57 58
	@Nullable
	public static String toJson(@Nullable Object object) {
		if (object == null) {
			return null;
		}
如梦技术's avatar
如梦技术 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71
		try {
			return getInstance().writeValueAsString(object);
		} catch (JsonProcessingException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将对象序列化成 json byte 数组
	 *
	 * @param object javaBean
	 * @return jsonString json字符串
	 */
如梦技术's avatar
如梦技术 已提交
72 73
	public static byte[] toJsonAsBytes(@Nullable Object object) {
		if (object == null) {
74
			return new byte[0];
如梦技术's avatar
如梦技术 已提交
75
		}
如梦技术's avatar
如梦技术 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89
		try {
			return getInstance().writeValueAsBytes(object);
		} catch (JsonProcessingException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json字符串转成 JsonNode
	 *
	 * @param jsonString jsonString
	 * @return jsonString json字符串
	 */
	public static JsonNode readTree(String jsonString) {
如梦技术's avatar
如梦技术 已提交
90
		Objects.requireNonNull(jsonString, "jsonString is null");
如梦技术's avatar
如梦技术 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
		try {
			return getInstance().readTree(jsonString);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json字符串转成 JsonNode
	 *
	 * @param in InputStream
	 * @return jsonString json字符串
	 */
	public static JsonNode readTree(InputStream in) {
如梦技术's avatar
如梦技术 已提交
105
		Objects.requireNonNull(in, "InputStream in is null");
如梦技术's avatar
如梦技术 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119
		try {
			return getInstance().readTree(in);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json字符串转成 JsonNode
	 *
	 * @param content content
	 * @return jsonString json字符串
	 */
	public static JsonNode readTree(byte[] content) {
如梦技术's avatar
如梦技术 已提交
120
		Objects.requireNonNull(content, "byte[] content is null");
如梦技术's avatar
如梦技术 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134
		try {
			return getInstance().readTree(content);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json字符串转成 JsonNode
	 *
	 * @param jsonParser JsonParser
	 * @return jsonString json字符串
	 */
	public static JsonNode readTree(JsonParser jsonParser) {
如梦技术's avatar
如梦技术 已提交
135
		Objects.requireNonNull(jsonParser, "jsonParser is null");
如梦技术's avatar
如梦技术 已提交
136 137 138 139 140 141 142 143 144 145
		try {
			return getInstance().readTree(jsonParser);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json byte 数组反序列化成对象
	 *
如梦技术's avatar
如梦技术 已提交
146 147 148
	 * @param content   json bytes
	 * @param valueType class
	 * @param <T>       T 泛型标记
如梦技术's avatar
如梦技术 已提交
149 150
	 * @return Bean
	 */
如梦技术's avatar
如梦技术 已提交
151 152 153 154 155
	@Nullable
	public static <T> T readValue(@Nullable byte[] content, Class<T> valueType) {
		if (ObjectUtil.isEmpty(content)) {
			return null;
		}
如梦技术's avatar
如梦技术 已提交
156
		try {
如梦技术's avatar
如梦技术 已提交
157
			return getInstance().readValue(content, valueType);
如梦技术's avatar
如梦技术 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json反序列化成对象
	 *
	 * @param jsonString jsonString
	 * @param valueType  class
	 * @param <T>        T 泛型标记
	 * @return Bean
	 */
如梦技术's avatar
如梦技术 已提交
171 172 173 174 175
	@Nullable
	public static <T> T readValue(@Nullable String jsonString, Class<T> valueType) {
		if (StringUtil.isBlank(jsonString)) {
			return null;
		}
如梦技术's avatar
如梦技术 已提交
176 177 178 179 180 181 182 183 184 185
		try {
			return getInstance().readValue(jsonString, valueType);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json反序列化成对象
	 *
如梦技术's avatar
如梦技术 已提交
186 187 188
	 * @param in        InputStream
	 * @param valueType class
	 * @param <T>       T 泛型标记
如梦技术's avatar
如梦技术 已提交
189 190
	 * @return Bean
	 */
如梦技术's avatar
如梦技术 已提交
191 192 193 194 195
	@Nullable
	public static <T> T readValue(@Nullable InputStream in, Class<T> valueType) {
		if (in == null) {
			return null;
		}
如梦技术's avatar
如梦技术 已提交
196 197 198 199 200 201 202 203 204 205
		try {
			return getInstance().readValue(in, valueType);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json反序列化成对象
	 *
如梦技术's avatar
如梦技术 已提交
206
	 * @param content       bytes
如梦技术's avatar
如梦技术 已提交
207
	 * @param typeReference 泛型类型
如梦技术's avatar
如梦技术 已提交
208
	 * @param <T>           T 泛型标记
如梦技术's avatar
如梦技术 已提交
209 210
	 * @return Bean
	 */
如梦技术's avatar
如梦技术 已提交
211
	@Nullable
如梦技术's avatar
如梦技术 已提交
212
	public static <T> T readValue(@Nullable byte[] content, TypeReference<T> typeReference) {
如梦技术's avatar
如梦技术 已提交
213 214 215
		if (ObjectUtil.isEmpty(content)) {
			return null;
		}
如梦技术's avatar
如梦技术 已提交
216
		try {
如梦技术's avatar
如梦技术 已提交
217
			return getInstance().readValue(content, typeReference);
如梦技术's avatar
如梦技术 已提交
218 219 220 221 222 223 224 225
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json反序列化成对象
	 *
如梦技术's avatar
如梦技术 已提交
226
	 * @param jsonString    jsonString
如梦技术's avatar
如梦技术 已提交
227
	 * @param typeReference 泛型类型
如梦技术's avatar
如梦技术 已提交
228
	 * @param <T>           T 泛型标记
如梦技术's avatar
如梦技术 已提交
229 230
	 * @return Bean
	 */
如梦技术's avatar
如梦技术 已提交
231
	@Nullable
如梦技术's avatar
如梦技术 已提交
232
	public static <T> T readValue(@Nullable String jsonString, TypeReference<T> typeReference) {
如梦技术's avatar
如梦技术 已提交
233 234 235
		if (StringUtil.isBlank(jsonString)) {
			return null;
		}
如梦技术's avatar
如梦技术 已提交
236 237 238 239 240 241 242 243 244 245
		try {
			return getInstance().readValue(jsonString, typeReference);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json反序列化成对象
	 *
如梦技术's avatar
如梦技术 已提交
246
	 * @param in            InputStream
如梦技术's avatar
如梦技术 已提交
247
	 * @param typeReference 泛型类型
如梦技术's avatar
如梦技术 已提交
248
	 * @param <T>           T 泛型标记
如梦技术's avatar
如梦技术 已提交
249 250
	 * @return Bean
	 */
如梦技术's avatar
如梦技术 已提交
251
	@Nullable
如梦技术's avatar
如梦技术 已提交
252
	public static <T> T readValue(@Nullable InputStream in, TypeReference<T> typeReference) {
如梦技术's avatar
如梦技术 已提交
253 254 255
		if (in == null) {
			return null;
		}
如梦技术's avatar
如梦技术 已提交
256 257 258 259 260 261 262
		try {
			return getInstance().readValue(in, typeReference);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	/**
	 * 将json反序列化成对象
	 *
	 * @param content  bytes
	 * @param javaType JavaType
	 * @param <T>      T 泛型标记
	 * @return Bean
	 */
	@Nullable
	public static <T> T readValue(@Nullable byte[] content, JavaType javaType) {
		if (ObjectUtil.isEmpty(content)) {
			return null;
		}
		try {
			return getInstance().readValue(content, javaType);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json反序列化成对象
	 *
	 * @param jsonString jsonString
	 * @param javaType   JavaType
	 * @param <T>        T 泛型标记
	 * @return Bean
	 */
	@Nullable
	public static <T> T readValue(@Nullable String jsonString, JavaType javaType) {
		if (StringUtil.isBlank(jsonString)) {
			return null;
		}
		try {
			return getInstance().readValue(jsonString, javaType);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将json反序列化成对象
	 *
	 * @param in       InputStream
	 * @param javaType JavaType
	 * @param <T>      T 泛型标记
	 * @return Bean
	 */
	@Nullable
	public static <T> T readValue(@Nullable InputStream in, JavaType javaType) {
		if (in == null) {
			return null;
		}
		try {
			return getInstance().readValue(in, javaType);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

323 324 325 326 327 328 329 330 331 332
	/**
	 * 封装 map type,keyClass String
	 *
	 * @param valueClass value 类型
	 * @return MapType
	 */
	public static MapType getMapType(Class<?> valueClass) {
		return getMapType(String.class, valueClass);
	}

如梦技术's avatar
如梦技术 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	/**
	 * 封装 map type
	 *
	 * @param keyClass   key 类型
	 * @param valueClass value 类型
	 * @return MapType
	 */
	public static MapType getMapType(Class<?> keyClass, Class<?> valueClass) {
		return getInstance().getTypeFactory().constructMapType(Map.class, keyClass, valueClass);
	}

	/**
	 * 封装 map type
	 *
	 * @param elementClass 集合值类型
	 * @return CollectionLikeType
	 */
	public static CollectionLikeType getListType(Class<?> elementClass) {
		return getInstance().getTypeFactory().constructCollectionLikeType(List.class, elementClass);
	}

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	/**
	 * 封装参数化类型
	 *
	 * <p>
	 * 例如: Map.class, String.class, String.class 对应 Map[String, String]
	 * </p>
	 *
	 * @param parametrized     泛型参数化
	 * @param parameterClasses 泛型参数类型
	 * @return JavaType
	 */
	public static JavaType getParametricType(Class<?> parametrized, Class<?>... parameterClasses) {
		return getInstance().getTypeFactory().constructParametricType(parametrized, parameterClasses);
	}

如梦技术's avatar
如梦技术 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	/**
	 * 读取集合
	 *
	 * @param content      bytes
	 * @param elementClass elementClass
	 * @param <T>          泛型
	 * @return 集合
	 */
	public static <T> List<T> readList(@Nullable byte[] content, Class<T> elementClass) {
		if (ObjectUtil.isEmpty(content)) {
			return Collections.emptyList();
		}
		try {
			return getInstance().readValue(content, getListType(elementClass));
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

如梦技术's avatar
如梦技术 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	/**
	 * 读取集合
	 *
	 * @param content      InputStream
	 * @param elementClass elementClass
	 * @param <T>          泛型
	 * @return 集合
	 */
	public static <T> List<T> readList(@Nullable InputStream content, Class<T> elementClass) {
		if (content == null) {
			return Collections.emptyList();
		}
		try {
			return getInstance().readValue(content, getListType(elementClass));
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

如梦技术's avatar
如梦技术 已提交
407 408 409 410 411 412 413 414
	/**
	 * 读取集合
	 *
	 * @param content      bytes
	 * @param elementClass elementClass
	 * @param <T>          泛型
	 * @return 集合
	 */
415
	public static <T> List<T> readList(@Nullable String content, Class<T> elementClass) {
如梦技术's avatar
如梦技术 已提交
416
		if (StringUtil.isBlank(content)) {
如梦技术's avatar
如梦技术 已提交
417 418 419 420 421 422 423 424 425
			return Collections.emptyList();
		}
		try {
			return getInstance().readValue(content, getListType(elementClass));
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	/**
	 * 读取集合
	 *
	 * @param content bytes
	 * @return 集合
	 */
	public static Map<String, Object> readMap(@Nullable byte[] content) {
		return readMap(content, Object.class);
	}

	/**
	 * 读取集合
	 *
	 * @param content InputStream
	 * @return 集合
	 */
	public static Map<String, Object> readMap(@Nullable InputStream content) {
		return readMap(content, Object.class);
	}

	/**
	 * 读取集合
	 *
	 * @param content bytes
	 * @return 集合
	 */
	public static Map<String, Object> readMap(@Nullable String content) {
		return readMap(content, Object.class);
	}

456 457 458 459 460 461 462 463
	/**
	 * 读取集合
	 *
	 * @param content    bytes
	 * @param valueClass 值类型
	 * @param <V>        泛型
	 * @return 集合
	 */
464
	public static <V> Map<String, V> readMap(@Nullable byte[] content, Class<?> valueClass) {
465 466 467 468 469 470 471 472 473 474 475
		return readMap(content, String.class, valueClass);
	}

	/**
	 * 读取集合
	 *
	 * @param content    InputStream
	 * @param valueClass 值类型
	 * @param <V>        泛型
	 * @return 集合
	 */
476
	public static <V> Map<String, V> readMap(@Nullable InputStream content, Class<?> valueClass) {
477 478 479 480 481 482 483 484 485 486 487
		return readMap(content, String.class, valueClass);
	}

	/**
	 * 读取集合
	 *
	 * @param content    bytes
	 * @param valueClass 值类型
	 * @param <V>        泛型
	 * @return 集合
	 */
488
	public static <V> Map<String, V> readMap(@Nullable String content, Class<?> valueClass) {
489 490 491
		return readMap(content, String.class, valueClass);
	}

如梦技术's avatar
如梦技术 已提交
492 493 494 495 496 497
	/**
	 * 读取集合
	 *
	 * @param content    bytes
	 * @param keyClass   key类型
	 * @param valueClass 值类型
如梦技术's avatar
如梦技术 已提交
498 499
	 * @param <K>        泛型
	 * @param <V>        泛型
如梦技术's avatar
如梦技术 已提交
500 501
	 * @return 集合
	 */
如梦技术's avatar
如梦技术 已提交
502
	public static <K, V> Map<K, V> readMap(@Nullable byte[] content, Class<?> keyClass, Class<?> valueClass) {
如梦技术's avatar
如梦技术 已提交
503
		if (ObjectUtil.isEmpty(content)) {
如梦技术's avatar
如梦技术 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
			return Collections.emptyMap();
		}
		try {
			return getInstance().readValue(content, getMapType(keyClass, valueClass));
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 读取集合
	 *
	 * @param content    InputStream
	 * @param keyClass   key类型
	 * @param valueClass 值类型
	 * @param <K>        泛型
	 * @param <V>        泛型
	 * @return 集合
	 */
	public static <K, V> Map<K, V> readMap(@Nullable InputStream content, Class<?> keyClass, Class<?> valueClass) {
如梦技术's avatar
如梦技术 已提交
524
		if (content == null) {
如梦技术's avatar
如梦技术 已提交
525
			return Collections.emptyMap();
如梦技术's avatar
如梦技术 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539
		}
		try {
			return getInstance().readValue(content, getMapType(keyClass, valueClass));
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 读取集合
	 *
	 * @param content    bytes
	 * @param keyClass   key类型
	 * @param valueClass 值类型
如梦技术's avatar
如梦技术 已提交
540 541
	 * @param <K>        泛型
	 * @param <V>        泛型
如梦技术's avatar
如梦技术 已提交
542 543
	 * @return 集合
	 */
如梦技术's avatar
如梦技术 已提交
544
	public static <K, V> Map<K, V> readMap(@Nullable String content, Class<?> keyClass, Class<?> valueClass) {
如梦技术's avatar
如梦技术 已提交
545
		if (StringUtil.isBlank(content)) {
如梦技术's avatar
如梦技术 已提交
546
			return Collections.emptyMap();
如梦技术's avatar
如梦技术 已提交
547 548 549 550 551 552 553 554
		}
		try {
			return getInstance().readValue(content, getMapType(keyClass, valueClass));
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

如梦技术's avatar
如梦技术 已提交
555 556 557 558 559 560 561 562 563 564 565 566
	/**
	 * jackson 的类型转换
	 *
	 * @param fromValue   来源对象
	 * @param toValueType 转换的类型
	 * @param <T>         泛型标记
	 * @return 转换结果
	 */
	public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
		return getInstance().convertValue(fromValue, toValueType);
	}

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	/**
	 * jackson 的类型转换
	 *
	 * @param fromValue   来源对象
	 * @param toValueType 转换的类型
	 * @param <T>         泛型标记
	 * @return 转换结果
	 */
	public static <T> T convertValue(Object fromValue, JavaType toValueType) {
		return getInstance().convertValue(fromValue, toValueType);
	}

	/**
	 * jackson 的类型转换
	 *
	 * @param fromValue      来源对象
	 * @param toValueTypeRef 泛型类型
	 * @param <T>            泛型标记
	 * @return 转换结果
	 */
	public static <T> T convertValue(Object fromValue, TypeReference<T> toValueTypeRef) {
		return getInstance().convertValue(fromValue, toValueTypeRef);
	}

如梦技术's avatar
如梦技术 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	/**
	 * tree 转对象
	 *
	 * @param treeNode  TreeNode
	 * @param valueType valueType
	 * @param <T>       泛型标记
	 * @return 转换结果
	 */
	public static <T> T treeToValue(TreeNode treeNode, Class<T> valueType) {
		try {
			return getInstance().treeToValue(treeNode, valueType);
		} catch (JsonProcessingException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
608
	 * 对象转 tree
如梦技术's avatar
如梦技术 已提交
609
	 *
610 611 612
	 * @param fromValue fromValue
	 * @param <T>       泛型标记
	 * @return 转换结果
如梦技术's avatar
如梦技术 已提交
613
	 */
614 615
	public static <T extends JsonNode> T valueToTree(@Nullable Object fromValue) {
		return getInstance().valueToTree(fromValue);
如梦技术's avatar
如梦技术 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	}

	/**
	 * 判断是否可以序列化
	 *
	 * @param value 对象
	 * @return 是否可以序列化
	 */
	public static boolean canSerialize(@Nullable Object value) {
		if (value == null) {
			return true;
		}
		return getInstance().canSerialize(value.getClass());
	}

631
	/**
632
	 * 判断是否可以反序列化
633 634
	 *
	 * @param type JavaType
635
	 * @return 是否可以反序列化
636 637 638 639 640
	 */
	public static boolean canDeserialize(JavaType type) {
		return getInstance().canDeserialize(type);
	}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
	/**
	 * 检验 json 格式
	 *
	 * @param jsonString json 字符串
	 * @return 是否成功
	 */
	public static boolean isValidJson(String jsonString) {
		return isValidJson(mapper -> mapper.readTree(jsonString));
	}

	/**
	 * 检验 json 格式
	 *
	 * @param content json byte array
	 * @return 是否成功
	 */
	public static boolean isValidJson(byte[] content) {
		return isValidJson(mapper -> mapper.readTree(content));
	}

	/**
	 * 检验 json 格式
	 *
	 * @param input json input stream
	 * @return 是否成功
	 */
	public static boolean isValidJson(InputStream input) {
		return isValidJson(mapper -> mapper.readTree(input));
	}

	/**
	 * 检验 json 格式
	 *
	 * @param jsonParser json parser
	 * @return 是否成功
	 */
	public static boolean isValidJson(JsonParser jsonParser) {
		return isValidJson(mapper -> mapper.readTree(jsonParser));
	}

	/**
	 * 检验 json 格式
	 *
	 * @param consumer ObjectMapper consumer
	 * @return 是否成功
	 */
	public static boolean isValidJson(CheckedConsumer<ObjectMapper> consumer) {
		ObjectMapper mapper = getInstance().copy();
		mapper.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
		mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
		try {
			consumer.accept(mapper);
			return true;
		} catch (Throwable e) {
			return false;
		}
	}

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	/**
	 * 创建 ObjectNode
	 *
	 * @return ObjectNode
	 */
	public static ObjectNode createObjectNode() {
		return getInstance().createObjectNode();
	}

	/**
	 * 创建 ArrayNode
	 *
	 * @return ArrayNode
	 */
	public static ArrayNode createArrayNode() {
		return getInstance().createArrayNode();
	}

717 718 719 720 721
	/**
	 * 获取 ObjectMapper 实例
	 *
	 * @return ObjectMapper
	 */
如梦技术's avatar
如梦技术 已提交
722 723 724 725 726
	public static ObjectMapper getInstance() {
		return JacksonHolder.INSTANCE;
	}

	private static class JacksonHolder {
727
		private static final ObjectMapper INSTANCE = new JacksonObjectMapper();
如梦技术's avatar
如梦技术 已提交
728 729 730 731 732 733 734 735
	}

	private static class JacksonObjectMapper extends ObjectMapper {
		private static final long serialVersionUID = 4288193147502386170L;

		private static final Locale CHINA = Locale.CHINA;

		JacksonObjectMapper() {
736
			super(jsonFactory());
如梦技术's avatar
如梦技术 已提交
737
			super.setLocale(CHINA);
738
			super.setDateFormat(new SimpleDateFormat(DateUtil.PATTERN_DATETIME, CHINA));
如梦技术's avatar
如梦技术 已提交
739 740 741 742 743 744 745 746 747 748
			// 单引号
			super.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
			// 忽略json字符串中不识别的属性
			super.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			// 忽略无法转换的对象
			super.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
			super.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
			super.findAndRegisterModules();
		}

749 750 751 752
		JacksonObjectMapper(ObjectMapper src) {
			super(src);
		}

753 754 755 756 757 758 759 760
		private static JsonFactory jsonFactory() {
			return JsonFactory.builder()
				// 可解析反斜杠引用的所有字符
				.configure(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true)
				// 允许JSON字符串包含非引号控制字符(值小于32的ASCII字符,包含制表符和换行符)
				.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS, true)
				.build();
		}
761 762 763 764 765

		@Override
		public ObjectMapper copy() {
			return new JacksonObjectMapper(this);
		}
如梦技术's avatar
如梦技术 已提交
766 767
	}
}