forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapperManager.java
More file actions
155 lines (127 loc) · 5.21 KB
/
Copy pathMapperManager.java
File metadata and controls
155 lines (127 loc) · 5.21 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
package com.pubnub.api.managers;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
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 retrofit2.Converter;
import retrofit2.converter.gson.GsonConverterFactory;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
public class MapperManager {
@Getter
private Gson objectMapper;
@Getter
private Converter.Factory converterFactory;
public MapperManager() {
TypeAdapter<Boolean> booleanAsIntAdapter = 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);
}
}
};
this.objectMapper = new GsonBuilder()
.registerTypeAdapter(Boolean.class, booleanAsIntAdapter)
.registerTypeAdapter(boolean.class, booleanAsIntAdapter)
.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 int elementToInt(JsonElement element, String field) {
return element.getAsJsonObject().get(field).getAsInt();
}
public String elementToString(JsonElement element, String field) {
return element.getAsJsonObject().get(field).getAsString();
}
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()).build();
}
}
@SuppressWarnings("unchecked")
public <T> T convertValue(JsonElement input, Class clazz) {
return (T) this.objectMapper.fromJson(input, 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()).build();
}
}
}