List of utility methods to do Json
JSONArray | getAsJSONArray(Object obj) This method takes an object that is JSONArray already or JSONObject and convert it into JSONArray this is useful because JSON turn an xml entry into array or object according to number of entries using this method will avoid to treat as special cases whether you have one entrie or several. JSONArray result = null; if (obj instanceof JSONArray) { result = (JSONArray) obj; } else if (obj instanceof JSONObject) { result = new JSONArray(); result.put(obj); } else { throw new ParseException("problem while interpreting " + obj, 0); ... |
T | getFromGson(String json, Class get From Gson return G.fromJson(json, clazz);
|
String | getJSONDate(long l) get JSON Date return getDateString(l, "yyyy-MM-dd'T'HH:mm:ss"); |
ObjectMapper | getJsonMapper() get Json Mapper return mapper.get();
|
boolean | hasKey(JsonObject object, String name) has Key JsonValue value = object.get(name); if ((value == null) || (value == JsonValue.NULL)) { return false; return true; |
boolean | jsonEquals(JsonArray expected, JsonArray actual, boolean strict) json Equals return compare(expected, actual, strict);
|
JsonObject | jsonObject(String key, JsonValue val) json Object return Json.createObjectBuilder().add(key, val).build(); |
JsonObjectBuilder | jsonObjectToBuilder(JsonObject jo) json Object To Builder JsonObjectBuilder job = Json.createObjectBuilder(); for (Entry<String, JsonValue> entry : jo.entrySet()) { job.add(entry.getKey(), entry.getValue()); return job; |
void | jsonPrettyPrinter(Object object) This printer only support JsonObject and JsonArray if (!(object instanceof JsonObject || object instanceof JsonArray)) { System.out.println(object.toString()); return; String newLine = System.getProperty("line.separator"); StringBuilder strb = new StringBuilder(); char[] charArr = object.toString().toCharArray(); int depth = 0; ... |
String | jsonString(JsonValue value) json String if (value == null || value.getValueType() == JsonValue.ValueType.NULL) return ""; if (value.getValueType() == JsonValue.ValueType.STRING && (value instanceof JsonString)) return ((JsonString) value).getString(); return ""; |