-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponseParser.java
139 lines (127 loc) · 4.53 KB
/
ResponseParser.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
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
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import java.io.IOException;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* Created by Carlos on 1/28/2017.
*/
public class ResponseParser {
private static String searchKey;
private static boolean getUsers;
protected static Queue parseJSON(JsonReader reader) {
Queue result = new PriorityQueue<>();
try {
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
boolean bool = reader.nextBoolean();
String nextName = reader.nextName();
match(reader, result);
}
reader.endObject();
} catch (IOException ie) {
System.err.println("An I/O error occurred while beginning the parse\n" + ie.getMessage());
return null;
}
setSearchKey("");
setgetUsers(false);
return result;
}
protected static void parseJSONArray(JsonReader reader, Queue result) {
try {
reader.beginArray();
while (reader.hasNext()) {
match(reader, result);
}
reader.endArray();
} catch (IOException ie) {
System.err.println("An I/O error occurred while parsing the JSONArray\n" + ie.getMessage());
return;
}
}
protected static void parseJSONObject(JsonReader reader, Queue result) {
try {
reader.beginObject();
while (reader.hasNext()) {
parseJSONKey(reader, result);
}
reader.endObject();
} catch (IOException ie) {
System.err.println("An I/O error occurred while parsing the JSONObject\n" + ie.getMessage());
}
}
protected static void parseJSONKey(JsonReader reader, Queue result) {
try {
if (reader.peek() != JsonToken.NAME)
return;
String key = reader.nextName();
if (key.equals("user") && !getUsers) {
String user = reader.nextString();
result.add(user);
match(reader, result);
} else if (key.equals("id")) {
String channel_id = findName(reader);
if (channel_id == null) {
parseJSONKey(reader, result);
} else {
result.add(channel_id);
}
} else {
match(reader, result);
}
} catch (IOException ie) {
System.err.println("An I/O error occurred while parsing the JSONKeys\n" + ie.getMessage());
}
}
protected static void match(JsonReader reader, Queue result) {
try {
JsonToken next = reader.peek();
if (next == JsonToken.BEGIN_ARRAY) {
parseJSONArray(reader, result);
} else if (next == JsonToken.BEGIN_OBJECT) {
parseJSONObject(reader, result);
} else if (next == JsonToken.BOOLEAN) {
reader.nextBoolean();
} else if (next == JsonToken.NAME) {
parseJSONKey(reader, result);
} else if (next == JsonToken.NULL) {
reader.nextNull();
} else if (next == JsonToken.NUMBER) {
reader.nextLong();
} else if (next == JsonToken.STRING) {
reader.nextString();
} else if (next == JsonToken.END_OBJECT) {
return;
}
if (reader.hasNext())
match(reader, result);
} catch (IOException ie) {
System.err.println("An I/O error occurred while matching\n" + ie.getMessage());
}
}
protected static String findName(JsonReader reader) {
try {
String channelId = reader.nextString();
if (reader.peek() == JsonToken.END_OBJECT)
return null;
String channelKeyName = reader.nextName();
String channelName = reader.nextString();
if (channelName.equals(searchKey)) {
return channelId;
}
} catch (IOException ie) {
System.err.println("An I/O error occurred while searching for Name\n" + ie.getMessage());
}
return null;
}
/**
* Sets the key we want to find in the JSON Object.
*/
protected static void setSearchKey(String key) {
searchKey = key;
}
protected static void setgetUsers(boolean yes) {
getUsers = yes;
}
}