1
1
package synapticloop .b2 .response ;
2
2
3
+ import java .util .Iterator ;
4
+
3
5
/*
4
6
* Copyright (c) 2016 synapticloop.
5
7
*
21
23
import org .json .JSONObject ;
22
24
import org .slf4j .Logger ;
23
25
import org .slf4j .LoggerFactory ;
24
- import synapticloop .b2 .exception .B2ApiException ;
25
26
26
- import java . util . Iterator ;
27
+ import synapticloop . b2 . exception . B2ApiException ;
27
28
28
29
public abstract class BaseB2Response {
29
30
private static final Logger LOGGER = LoggerFactory .getLogger (BaseB2Response .class );
@@ -69,12 +70,24 @@ private static JSONObject parse(String json) throws B2ApiException {
69
70
}
70
71
71
72
/**
72
- * Read and remove object with key from JSON
73
+ * Read and remove String with key from JSON
74
+ *
75
+ * @param key the key to read as a string and remove
76
+ *
77
+ * @return the read key (or null if it doesn't exist)
73
78
*/
74
79
protected String readString (String key ) {
75
80
return this .readString (response , key );
76
81
}
77
82
83
+ /**
84
+ * Read and remove String with key from JSON object
85
+ *
86
+ * @param response The JSON object to read from
87
+ * @param key the key to read as a string and remove
88
+ *
89
+ * @return the read key (or null if it doesn't exist)
90
+ */
78
91
protected String readString (JSONObject response , String key ) {
79
92
final Object value = response .remove (key );
80
93
if (null == value || JSONObject .NULL == value ) {
@@ -85,7 +98,11 @@ protected String readString(JSONObject response, String key) {
85
98
}
86
99
87
100
/**
88
- * Read and remove object with key from JSON
101
+ * Read and remove int with key from JSON object
102
+ *
103
+ * @param key the key to read as an int and remove
104
+ *
105
+ * @return the read key (or -1 if it doesn't exist)
89
106
*/
90
107
protected int readInt (String key ) {
91
108
final Object value = response .remove (key );
@@ -97,7 +114,11 @@ protected int readInt(String key) {
97
114
}
98
115
99
116
/**
100
- * Read and remove object with key from JSON
117
+ * Read and remove long with key from JSON object
118
+ *
119
+ * @param key the key to read as a long and remove
120
+ *
121
+ * @return the read key (or -1L if it doesn't exist)
101
122
*/
102
123
protected long readLong (String key ) {
103
124
final Object value = response .remove (key );
@@ -108,10 +129,25 @@ protected long readLong(String key) {
108
129
return value instanceof Number ? ((Number ) value ).longValue () : Long .parseLong (value .toString ());
109
130
}
110
131
132
+ /**
133
+ * Read and remove JSONObject with key from JSON object
134
+ *
135
+ * @param key the key to read as a JSONObject and remove
136
+ *
137
+ * @return the read key (or null if it doesn't exist)
138
+ */
111
139
protected JSONObject readObject (String key ) {
112
140
return this .readObject (response , key );
113
141
}
114
142
143
+ /**
144
+ * Read and remove JSONObject with key from JSON object
145
+ *
146
+ * @param response The JSON object to read from
147
+ * @param key the key to read as a JSONObject and remove
148
+ *
149
+ * @return the read key (or null if it doesn't exist)
150
+ */
115
151
protected JSONObject readObject (JSONObject response , String key ) {
116
152
final Object value = response .remove (key );
117
153
if (null == value || JSONObject .NULL == value ) {
@@ -122,7 +158,11 @@ protected JSONObject readObject(JSONObject response, String key) {
122
158
}
123
159
124
160
/**
125
- * Read and remove object with key from JSON
161
+ * Read and remove JSONArray with key from JSON object
162
+ *
163
+ * @param key the key to read as a JSONArray and remove
164
+ *
165
+ * @return the read key (or null if it doesn't exist)
126
166
*/
127
167
protected JSONArray readObjects (String key ) {
128
168
final Object value = response .remove (key );
@@ -139,7 +179,15 @@ protected JSONArray readObjects(String key) {
139
179
* the response will not be mapped. This will loop through the JSON object
140
180
* and any key left in the object will generate a 'WARN' message. The
141
181
* response class __MUST__ remove the object (i.e. jsonObject.remove(KEY_NAME))
142
- * after getting the value
182
+ * after getting the value, or use the utility methods in this class. This
183
+ * is used more as a testing tool/sanity test than anything else as there
184
+ * are some instances in where keys are returned, however are not listed in
185
+ * the documentation.
186
+ *
187
+ * {@link BaseB2Response#readInt(String)}
188
+ * {@link BaseB2Response#readLong(String)}
189
+ * {@link BaseB2Response#readString(String)}
190
+ * {@link BaseB2Response#readObject(String)}
143
191
*
144
192
* @param LOGGER The logger to use
145
193
*/
0 commit comments