1
- package com .github .xiaour .utils ;
2
-
3
-
4
- import com .google .gson .*;
5
- import com .google .gson .reflect .TypeToken ;
6
-
7
- import java .lang .reflect .Type ;
8
- import java .util .ArrayList ;
9
- import java .util .HashMap ;
10
- import java .util .List ;
11
- import java .util .Map ;
12
-
13
- public class JsonUtil {
14
-
15
-
16
- private static Gson gson = null ;
17
-
18
- static {
19
- gson = new Gson ();
20
- }
21
-
22
- public static synchronized Gson newInstance (){
23
- if (gson == null ){
24
- gson = new Gson ();
25
- }
26
- return gson ;
27
- }
28
-
29
- public static String getJsonString (Object obj ){
30
- return gson .toJson (obj );
31
- }
32
-
33
- public static <T > T toBean (String json ,Class <T > clz ){
34
-
35
- return gson .fromJson (json , clz );
36
- }
37
-
38
- public static <T > Map <String , T > readJson2MapObj (String json ,Class <T > clz ){
39
- Map <String , JsonObject > map = gson .fromJson (json , new TypeToken <Map <String ,JsonObject >>(){}.getType ());
40
- Map <String , T > result = new HashMap <>();
41
- for (String key :map .keySet ()){
42
- result .put (key ,gson .fromJson (map .get (key ),clz ) );
43
- }
44
- return result ;
45
- }
46
-
47
- public static <T > T json2Obj (String json ,Class <T > clz ){
48
- return gson .fromJson (json ,clz );
49
- }
50
-
51
- public static Map <String , Object > toMap (String json ){
52
- Map <String , Object > map = gson .fromJson (json , new TypeToken <Map <String ,Object >>(){}.getType ());
53
- return map ;
54
- }
55
-
56
- public static Map <String ,String > readJsonStrMap (String json ) {
57
- Map <String , JsonObject > map = gson .fromJson (json , new TypeToken <Map <String ,JsonObject >>(){}.getType ());
58
- Map <String ,String > result = new HashMap <>();
59
- for (String key :map .keySet ()){
60
- result .put (key ,gson .fromJson (map .get (key ),String .class ) );
61
- }
62
- return result ;
63
- }
64
-
65
- public static Map <byte [], byte []> readJsonByteMap (String json ) {
66
- Map <String , JsonPrimitive > map = gson .fromJson (json , new TypeToken <Map <String ,JsonPrimitive >>(){}.getType ());
67
- Map <byte [], byte []> vmap = new HashMap <>();
68
- for (String key :map .keySet ()){
69
- vmap .put (key .getBytes (),gson .fromJson (map .get (key ),String .class ).getBytes () );
70
- }
71
- return vmap ;
72
-
73
- }
74
-
75
-
76
- public static <T > List <T > readJson2Array (String json , Class <T > clz ){
77
- JsonArray array = new JsonParser ().parse (json ).getAsJsonArray ();
78
- List <T > list = new ArrayList <>();
79
- for (final JsonElement elem : array ){
80
- list .add (gson .fromJson (elem , (Type )clz ));
81
- }
82
- return list ;
83
- }
84
-
1
+ package com .github .xiaour .utils ;
2
+
3
+ import com .google .gson .*;
4
+ import com .google .gson .reflect .TypeToken ;
5
+
6
+ import java .lang .reflect .Type ;
7
+ import java .util .ArrayList ;
8
+ import java .util .HashMap ;
9
+ import java .util .List ;
10
+ import java .util .Map ;
11
+
12
+ public class JsonUtil {
13
+
14
+
15
+ private static Gson gson = null ;
16
+
17
+ static {
18
+ gson = new Gson ();
19
+ }
20
+
21
+ public static synchronized Gson newInstance (){
22
+ if (gson == null ){
23
+ gson = new Gson ();
24
+ }
25
+ return gson ;
26
+ }
27
+
28
+ public static String getJsonString (Object obj ){
29
+ return gson .toJson (obj );
30
+ }
31
+
32
+ public static <T > T toBean (String json ,Class <T > clz ){
33
+
34
+ return gson .fromJson (json , clz );
35
+ }
36
+
37
+ public static <T > Map <String , T > readJson2MapObj (String json ,Class <T > clz ){
38
+ Map <String , JsonObject > map = gson .fromJson (json , new TypeToken <Map <String ,JsonObject >>(){}.getType ());
39
+ Map <String , T > result = new HashMap <>();
40
+ for (String key :map .keySet ()){
41
+ result .put (key ,gson .fromJson (map .get (key ),clz ) );
42
+ }
43
+ return result ;
44
+ }
45
+
46
+ public static <T > T json2Obj (String json ,Class <T > clz ){
47
+ return gson .fromJson (json ,clz );
48
+ }
49
+
50
+ public static Map <String , Object > toMap (String json ){
51
+ Map <String , Object > map = gson .fromJson (json , new TypeToken <Map <String ,Object >>(){}.getType ());
52
+ return map ;
53
+ }
54
+
55
+ public static Map <String ,String > readJsonStrMap (String json ) {
56
+ Map <String , JsonObject > map = gson .fromJson (json , new TypeToken <Map <String ,JsonObject >>(){}.getType ());
57
+ Map <String ,String > result = new HashMap <>();
58
+ for (String key :map .keySet ()){
59
+ result .put (key ,gson .fromJson (map .get (key ),String .class ) );
60
+ }
61
+ return result ;
62
+ }
63
+
64
+ public static Map <byte [], byte []> readJsonByteMap (String json ) {
65
+ Map <String , JsonPrimitive > map = gson .fromJson (json , new TypeToken <Map <String ,JsonPrimitive >>(){}.getType ());
66
+ Map <byte [], byte []> vmap = new HashMap <>();
67
+ for (String key :map .keySet ()){
68
+ vmap .put (key .getBytes (),gson .fromJson (map .get (key ),String .class ).getBytes () );
69
+ }
70
+ return vmap ;
71
+
72
+ }
73
+
74
+
75
+ public static <T > List <T > readJson2Array (String json , Class <T > clz ){
76
+ JsonArray array = new JsonParser ().parse (json ).getAsJsonArray ();
77
+ List <T > list = new ArrayList <>();
78
+ for (final JsonElement elem : array ){
79
+ list .add (gson .fromJson (elem , (Type )clz ));
80
+ }
81
+ return list ;
82
+ }
83
+
85
84
}
0 commit comments