2
2
3
3
import cn .fxbin .bubble .core .exception .UtilException ;
4
4
import cn .fxbin .bubble .core .module .JacksonHolder ;
5
+ import cn .hutool .json .JSONUtil ;
5
6
import com .fasterxml .jackson .core .JsonProcessingException ;
6
- import com .fasterxml .jackson .databind .ObjectMapper ;
7
7
import lombok .experimental .UtilityClass ;
8
8
9
9
import java .io .IOException ;
16
16
* @since 2020/3/20 17:28
17
17
*/
18
18
@ UtilityClass
19
- public class JsonUtils {
19
+ public class JsonUtils extends JSONUtil {
20
20
21
21
22
22
/**
@@ -28,7 +28,13 @@ public class JsonUtils {
28
28
*/
29
29
public String toJson (Object object ) {
30
30
try {
31
- return getInstance ().writeValueAsString (object );
31
+ if (ObjectUtils .isEmpty (object )) {
32
+ return null ;
33
+ }
34
+ if (object instanceof CharSequence ) {
35
+ return StringUtils .utf8Str (object );
36
+ }
37
+ return JacksonHolder .INSTANCE .writeValueAsString (object );
32
38
} catch (JsonProcessingException e ) {
33
39
throw new UtilException (e );
34
40
}
@@ -44,7 +50,7 @@ public String toJson(Object object) {
44
50
*/
45
51
public boolean isJsonString (String jsonString ) {
46
52
try {
47
- getInstance () .readTree (jsonString );
53
+ JacksonHolder . INSTANCE .readTree (jsonString );
48
54
return true ;
49
55
} catch (IOException e ) {
50
56
return false ;
@@ -61,7 +67,7 @@ public boolean isJsonString(String jsonString) {
61
67
*/
62
68
public boolean isJsonSerialize (Object object ) {
63
69
try {
64
- getInstance () .writeValueAsBytes (object );
70
+ JacksonHolder . INSTANCE .writeValueAsBytes (object );
65
71
return true ;
66
72
} catch (JsonProcessingException e ) {
67
73
return false ;
@@ -79,22 +85,10 @@ public boolean isJsonSerialize(Object object) {
79
85
*/
80
86
public <T > T parse (String jsonString , Class <T > requiredType ) {
81
87
try {
82
- return getInstance () .readValue (jsonString , requiredType );
88
+ return JacksonHolder . INSTANCE .readValue (jsonString , requiredType );
83
89
} catch (IOException e ) {
84
90
throw new UtilException (e );
85
91
}
86
92
}
87
93
88
-
89
- /**
90
- * getInstance 获取jackson实例
91
- *
92
- * @since 2020/3/20 17:30
93
- * @return com.fasterxml.jackson.databind.ObjectMapper
94
- */
95
- private ObjectMapper getInstance () {
96
- return JacksonHolder .INSTANCE ;
97
- }
98
-
99
-
100
94
}
0 commit comments