-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsonUtils.java
executable file
·114 lines (96 loc) · 3.22 KB
/
JsonUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.hwx.ranger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.reflect.TypeToken;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
public class JsonUtils {
protected static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
protected Response parseJSON(String jsonContent) {
Response inpObject=null;
if ((jsonContent == null) || jsonContent.isEmpty()) {
return (Response) Collections.emptyMap();
} else {
try {
inpObject = new Gson().fromJson(jsonContent, Response.class);
}
catch (JsonSyntaxException e) {
logger.error(e.getMessage());
}
return inpObject;
}
}
protected RangerPolicyResponse parseRangerPolicy(String jsonContent) {
RangerPolicyResponse inpObject=null;
if ((jsonContent == null) || jsonContent.isEmpty()) {
return (RangerPolicyResponse) Collections.emptyMap();
} else {
try {
inpObject = new Gson().fromJson(jsonContent, RangerPolicyResponse.class);
}
catch (JsonSyntaxException e) {
logger.error(e.getMessage());
}
return inpObject;
}
}
protected List<RangerPolicyResponse> parseRangerPolicies(String jsonContent) {
List<RangerPolicyResponse> inpObject=null;
if ((jsonContent == null) || jsonContent.isEmpty()) {
return null;
} else {
try {
Type listType = new TypeToken<ArrayList<RangerPolicyResponse>>(){}.getType();
inpObject = new Gson().fromJson(jsonContent, listType);
}
catch (JsonSyntaxException e) {
logger.error(e.getMessage());
}
return inpObject;
}
}
protected HDFSCheckList parseHDFSCheckList(String jsonContent) {
HDFSCheckList inpObject=null;
if ((jsonContent == null) || jsonContent.isEmpty()) {
return null;
} else {
try {
inpObject = new Gson().fromJson(jsonContent, HDFSCheckList.class);
}
catch (JsonSyntaxException e) {
logger.error(e.getMessage());
}
return inpObject;
}
}
protected String prettyPrint(String jsonContent) {
String prettyJsonString=null;
String data=null;
if ((jsonContent == null) || jsonContent.isEmpty()) {
return "EmptyJson";
} else {
try {
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(jsonContent);
if (je.isJsonObject())
{
JsonObject response = je.getAsJsonObject();
data = response.get("data").getAsString();
}
//prettyJsonString = gson.toJson(data);
prettyJsonString = data;
}
catch (JsonSyntaxException e) {
logger.error(e.getMessage());
}
return prettyJsonString;
}
}
}