1
+ package com .ClassicQueue .config ;
2
+
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .core .type .TypeReference ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
+ import com .fasterxml .jackson .databind .SerializationFeature ;
7
+
8
+ import java .io .IOException ;
9
+
10
+ public class JsonUtils {
11
+ private final ObjectMapper json ;
12
+ public JsonUtils () {
13
+ json = new ObjectMapper ();
14
+ json .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
15
+ }
16
+ public static ObjectMapper json () {
17
+ return InstanceHolder .json .json ;
18
+ }
19
+ public static <T > T read (String src , Class <T > valueType ) {
20
+ try {
21
+ return json ().readValue (src , valueType );
22
+ } catch (IOException e ) {
23
+ throw new IllegalStateException (e );
24
+ }
25
+ }
26
+ public static <T > T read (String src , TypeReference <T > valueTypeRef ) {
27
+ try {
28
+ return json ().readValue (src , valueTypeRef );
29
+ } catch (IOException e ) {
30
+ throw new IllegalStateException (e );
31
+ }
32
+ }
33
+ public static <T > T read (byte [] src , Class <T > valueType ) {
34
+ try {
35
+ return json ().readValue (src , valueType );
36
+ } catch (IOException e ) {
37
+ throw new IllegalStateException (e );
38
+ }
39
+ }
40
+ public static <T > T read (byte [] src , TypeReference <T > valueTypeRef ) {
41
+ try {
42
+ return json ().readValue (src , valueTypeRef );
43
+ } catch (IOException e ) {
44
+ throw new IllegalStateException (e );
45
+ }
46
+ }
47
+ public static String writeValueAsString (Object value ) {
48
+ try {
49
+ return json ().writeValueAsString (value );
50
+ } catch (JsonProcessingException e ) {
51
+ throw new IllegalStateException (e );
52
+ }
53
+ }
54
+ public static byte [] writeValueAsBytes (Object value ) {
55
+ try {
56
+ return json ().writeValueAsBytes (value );
57
+ } catch (JsonProcessingException e ) {
58
+ throw new IllegalStateException (e );
59
+ }
60
+ }
61
+ private static class InstanceHolder {
62
+ static final JsonUtils json = new JsonUtils ();
63
+ }
64
+ }
0 commit comments