11package synapticloop .b2 .response ;
22
3+ import java .util .Iterator ;
4+
35/*
46 * Copyright (c) 2016 synapticloop.
57 *
2123import org .json .JSONObject ;
2224import org .slf4j .Logger ;
2325import org .slf4j .LoggerFactory ;
24- import synapticloop .b2 .exception .B2ApiException ;
2526
26- import java . util . Iterator ;
27+ import synapticloop . b2 . exception . B2ApiException ;
2728
2829public abstract class BaseB2Response {
2930 private static final Logger LOGGER = LoggerFactory .getLogger (BaseB2Response .class );
@@ -69,12 +70,24 @@ private static JSONObject parse(String json) throws B2ApiException {
6970 }
7071
7172 /**
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)
7378 */
7479 protected String readString (String key ) {
7580 return this .readString (response , key );
7681 }
7782
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+ */
7891 protected String readString (JSONObject response , String key ) {
7992 final Object value = response .remove (key );
8093 if (null == value || JSONObject .NULL == value ) {
@@ -85,7 +98,11 @@ protected String readString(JSONObject response, String key) {
8598 }
8699
87100 /**
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)
89106 */
90107 protected int readInt (String key ) {
91108 final Object value = response .remove (key );
@@ -97,7 +114,11 @@ protected int readInt(String key) {
97114 }
98115
99116 /**
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)
101122 */
102123 protected long readLong (String key ) {
103124 final Object value = response .remove (key );
@@ -108,10 +129,25 @@ protected long readLong(String key) {
108129 return value instanceof Number ? ((Number ) value ).longValue () : Long .parseLong (value .toString ());
109130 }
110131
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+ */
111139 protected JSONObject readObject (String key ) {
112140 return this .readObject (response , key );
113141 }
114142
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+ */
115151 protected JSONObject readObject (JSONObject response , String key ) {
116152 final Object value = response .remove (key );
117153 if (null == value || JSONObject .NULL == value ) {
@@ -122,7 +158,11 @@ protected JSONObject readObject(JSONObject response, String key) {
122158 }
123159
124160 /**
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)
126166 */
127167 protected JSONArray readObjects (String key ) {
128168 final Object value = response .remove (key );
@@ -139,7 +179,15 @@ protected JSONArray readObjects(String key) {
139179 * the response will not be mapped. This will loop through the JSON object
140180 * and any key left in the object will generate a 'WARN' message. The
141181 * 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)}
143191 *
144192 * @param LOGGER The logger to use
145193 */
0 commit comments