Repository navigation
Expand file tree
/
Copy pathMapperManager.java
More file actions
275 lines (237 loc) · 9.48 KB
/
Copy pathMapperManager.java
File metadata and controls
275 lines (237 loc) · 9.48 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.pubnub.api.managers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.pubnub.api.PubNubException;
import com.pubnub.api.builder.PubNubErrorBuilder;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import retrofit2.Converter;
import retrofit2.converter.gson.GsonConverterFactory;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Map;
public class MapperManager {
@Getter
private final Gson objectMapper;
@Getter
private final Converter.Factory converterFactory;
private final ObjectMapper jacksonObjectMapper = new ObjectMapper();
public MapperManager() {
TypeAdapter<Boolean> booleanAsIntAdapter = getBooleanTypeAdapter();
this.objectMapper = new GsonBuilder()
.registerTypeAdapter(Boolean.class, booleanAsIntAdapter)
.registerTypeAdapter(boolean.class, booleanAsIntAdapter)
.registerTypeAdapter(JSONObject.class, new JSONObjectAdapter())
.registerTypeAdapter(JSONArray.class, new JSONArrayAdapter())
.create();
this.converterFactory = GsonConverterFactory.create(this.getObjectMapper());
}
public boolean hasField(JsonElement element, String field) {
return element.getAsJsonObject().has(field);
}
public JsonElement getField(JsonElement element, String field) {
return element.getAsJsonObject().get(field);
}
public Iterator<JsonElement> getArrayIterator(JsonElement element) {
return element.getAsJsonArray().iterator();
}
public Iterator<JsonElement> getArrayIterator(JsonElement element, String field) {
return element.getAsJsonObject().get(field).getAsJsonArray().iterator();
}
public Iterator<Map.Entry<String, JsonElement>> getObjectIterator(JsonElement element) {
return element.getAsJsonObject().entrySet().iterator();
}
public Iterator<Map.Entry<String, JsonElement>> getObjectIterator(JsonElement element, String field) {
return element.getAsJsonObject().get(field).getAsJsonObject().entrySet().iterator();
}
public String elementToString(JsonElement element) {
return element.getAsString();
}
public String elementToString(JsonElement element, String field) {
return element.getAsJsonObject().get(field).getAsString();
}
public int elementToInt(JsonElement element, String field) {
return element.getAsJsonObject().get(field).getAsInt();
}
public boolean isJsonObject(JsonElement element) {
return element.isJsonObject();
}
public JsonObject getAsObject(JsonElement element) {
return element.getAsJsonObject();
}
public boolean getAsBoolean(JsonElement element, String field) {
return element.getAsJsonObject().get(field).getAsBoolean();
}
public void putOnObject(JsonObject element, String key, JsonElement value) {
element.add(key, value);
}
public JsonElement getArrayElement(JsonElement element, int index) {
return element.getAsJsonArray().get(index);
}
public Long elementToLong(JsonElement element) {
return element.getAsLong();
}
public Long elementToLong(JsonElement element, String field) {
return element.getAsJsonObject().get(field).getAsLong();
}
public JsonArray getAsArray(JsonElement element) {
return element.getAsJsonArray();
}
@SuppressWarnings("unchecked")
public <T> T fromJson(String input, Class<T> clazz) throws PubNubException {
try {
return this.objectMapper.fromJson(input, clazz);
} catch (JsonParseException e) {
throw PubNubException.builder()
.pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR)
.errormsg(e.getMessage())
.cause(e)
.build();
}
}
@SuppressWarnings("unchecked")
public <T> T convertValue(JsonElement input, Class clazz) {
return (T) this.objectMapper.fromJson(input, clazz);
}
@SuppressWarnings("unchecked")
public <T> T convertValue(Object object, Class clazz) throws PubNubException {
return (T) fromJson(toJson(object), clazz);
}
public String toJson(Object input) throws PubNubException {
try {
return this.objectMapper.toJson(input);
} catch (JsonParseException e) {
throw PubNubException.builder()
.pubnubError(PubNubErrorBuilder.PNERROBJ_JSON_ERROR)
.errormsg(e.getMessage())
.cause(e)
.build();
}
}
public String toJsonUsinJackson(Object input) throws PubNubException {
try {
return this.jacksonObjectMapper.writeValueAsString(input);
} catch (JsonProcessingException e) {
throw PubNubException.builder()
.pubnubError(PubNubErrorBuilder.PNERROBJ_JSON_ERROR)
.errormsg(e.getMessage())
.cause(e)
.build();
}
}
public void isValidJsonObject(Object object) throws PubNubException {
String json = toJson(object);
JsonElement jsonElement = new JsonParser().parse(json);
boolean isValid = isJsonObject(jsonElement);
if (!isValid) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_INVALID_JSON).build();
}
}
@NotNull
private TypeAdapter<Boolean> getBooleanTypeAdapter() {
return new TypeAdapter<Boolean>() {
@Override
public void write(JsonWriter out, Boolean value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(value);
}
}
@Override
public Boolean read(JsonReader in) throws IOException {
JsonToken peek = in.peek();
switch (peek) {
case BOOLEAN:
return in.nextBoolean();
//case NULL:
// in.nextNull();
// return null;
case NUMBER:
return in.nextInt() != 0;
case STRING:
return Boolean.parseBoolean(in.nextString());
default:
throw new IllegalStateException("Expected BOOLEAN or NUMBER but was " + peek);
}
}
};
}
private static class JSONObjectAdapter implements JsonSerializer<JSONObject>, JsonDeserializer<JSONObject> {
@Override
public JsonElement serialize(JSONObject src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) {
return null;
}
JsonObject jsonObject = new JsonObject();
Iterator<String> keys = src.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = src.opt(key);
JsonElement jsonElement = context.serialize(value, value.getClass());
jsonObject.add(key, jsonElement);
}
return jsonObject;
}
@Override
public JSONObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
if (json == null) {
return null;
}
try {
return new JSONObject(json.toString());
} catch (JSONException e) {
e.printStackTrace();
throw new JsonParseException(e);
}
}
}
private static class JSONArrayAdapter implements JsonSerializer<JSONArray>, JsonDeserializer<JSONArray> {
@Override
public JsonElement serialize(JSONArray src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) {
return null;
}
JsonArray jsonArray = new JsonArray();
for (int i = 0; i < src.length(); i++) {
Object object = src.opt(i);
JsonElement jsonElement = context.serialize(object, object.getClass());
jsonArray.add(jsonElement);
}
return jsonArray;
}
@Override
public JSONArray deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
if (json == null) {
return null;
}
try {
return new JSONArray(json.toString());
} catch (JSONException e) {
e.printStackTrace();
throw new JsonParseException(e);
}
}
}
}