void writeTo(JSONStringer stringer) throws JSONException { stringer.object(); for (Map.Entry<String, Object> entry : nameValuePairs.entrySet()) { stringer.key(entry.getKey()).value(entry.getValue()); } stringer.endObject(); }
void writeTo(JSONStringer stringer) throws JSONException { stringer.object(); for (Map.Entry<String, Object> entry : nameValuePairs.entrySet()) { stringer.key(entry.getKey()).value(entry.getValue()); } stringer.endObject(); }
void writeTo(JSONStringer stringer) throws JSONException { stringer.array(); for (Object value : values) { stringer.value(value); } stringer.endArray(); }
void writeTo(JSONStringer stringer) throws JSONException { stringer.array(); for (Object value : values) { stringer.value(value); } stringer.endArray(); }
@NonNull @Override public String toFormattedString(@NonNull CrashReportData data, @NonNull ImmutableSet<ReportField> order, @NonNull String mainJoiner, @NonNull String subJoiner, boolean urlEncode) throws JSONException { final Map<String, Object> map = data.toMap(); final JSONStringer stringer = new JSONStringer().object(); for (ReportField field : order) { stringer.key(field.toString()).value(map.remove(field.toString())); } for (Map.Entry<String, Object> entry : map.entrySet()) { stringer.key(entry.getKey()).value(entry.getValue()); } return stringer.endObject().toString(); } },
/** * Returns a new string by alternating this array's values with {@code * separator}. This array's string values are quoted and have their special * characters escaped. For example, the array containing the strings '12" * pizza', 'taco' and 'soda' joined on '+' returns this: * <pre>"12\" pizza"+"taco"+"soda"</pre> */ public String join(String separator) throws JSONException { JSONStringer stringer = new JSONStringer(); stringer.open(JSONStringer.Scope.NULL, ""); for (int i = 0, size = values.size(); i < size; i++) { if (i > 0) { stringer.out.append(separator); } stringer.value(values.get(i)); } stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, ""); return stringer.out.toString(); }
/** * Returns a new string by alternating this array's values with {@code * separator}. This array's string values are quoted and have their special characters escaped. * For example, the array containing the strings '12" pizza', 'taco' and 'soda' joined on '+' * returns this: * * <pre> * "12\" pizza" + "taco" + "soda" * </pre> * * @param separator The string used to separate the returned values. * @return the conjoined values. * @throws JSONException Only if there is a coding error. */ public String join(String separator) throws JSONException { JSONStringer stringer = new JSONStringer(); stringer.open(JSONStringer.Scope.NULL, ""); for (int i = 0, size = values.size(); i < size; i++) { if (i > 0) { stringer.out.append(separator); } stringer.value(values.get(i)); } stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, ""); return stringer.out.toString(); }
/** * Encodes {@code data} as a JSON string. This applies quotes and any * necessary character escaping. * * @param data the string to encode. Null will be interpreted as an empty * string. */ public static String quote(String data) { if (data == null) { return "\"\""; } try { JSONStringer stringer = new JSONStringer(); stringer.open(JSONStringer.Scope.NULL, ""); stringer.value(data); stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, ""); return stringer.toString(); } catch (JSONException e) { throw new AssertionError(); } }
/** * Encodes {@code data} as a JSON string. This applies quotes and any necessary character * escaping. * * @param data the string to encode. Null will be interpreted as an empty string. * @return the quoted string. */ public static String quote(String data) { if (data == null) { return "\"\""; } try { JSONStringer stringer = new JSONStringer(); stringer.open(JSONStringer.Scope.NULL, ""); stringer.value(data); stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, ""); return stringer.toString(); } catch (JSONException e) { throw new AssertionError(); } }
private static void addToStringer(JSONStringer stringer, Object o) throws JSONException { if (o instanceof JSONObject) { stringer.object(); JSONObject object = (JSONObject) o; Iterator<String> keyIterator = object.keys(); ArrayList<String> keys = new ArrayList<>(); while (keyIterator.hasNext()) { keys.add(keyIterator.next()); } Collections.sort(keys); for (String key : keys) { stringer.key(key); addToStringer(stringer, object.opt(key)); } stringer.endObject(); return; } if (o instanceof JSONArray) { JSONArray array = (JSONArray) o; stringer.array(); for (int i = 0; i < array.length(); ++i) { addToStringer(stringer, array.get(i)); } stringer.endArray(); return; } stringer.value(o); }
void writeTo(JSONStringer stringer) throws JSONException { stringer.object(); for (Map.Entry<String, Object> entry : nameValuePairs.entrySet()) { stringer.key(entry.getKey()).value(entry.getValue()); } stringer.endObject(); }
void writeTo(JSONStringer stringer) throws JSONException { stringer.array(); for (Object value : values) { stringer.value(value); } stringer.endArray(); }
void writeTo(JSONStringer stringer) throws JSONException { stringer.array(); for (Object value : values) { stringer.value(value); } stringer.endArray(); }
void writeTo(JSONStringer stringer) throws JSONException { stringer.array(); for (Object value : values) { stringer.value(value); } stringer.endArray(); }
@Override public void write(JSONStringer writer) throws JSONException { super.write(writer); writer.key(VALUE).value(getValue()); }
public static void writeStringArray(JSONStringer writer, String key, List<String> values) throws JSONException { if (values != null) { writer.key(key).array(); for (String value : values) { writer.value(value); } writer.endArray(); } } }
@Override public void write(JSONStringer writer) throws JSONException { super.write(writer); writer.key(VALUE).value(getValue()); }
@Override public void write(JSONStringer writer) throws JSONException { super.write(writer); writer.key(VALUE).value(JSONDateUtils.toString(getValue())); }
@Test public void writeNullDeviceTest() throws JSONException { JSONStringer mockJsonStringer = mock(JSONStringer.class); when(mockJsonStringer.key(anyString())).thenReturn(mockJsonStringer); when(mockJsonStringer.value(anyString())).thenReturn(mockJsonStringer); AbstractLog mockLog = new MockLog(); mockLog.setTimestamp(new Date()); mockLog.write(mockJsonStringer); verify(mockJsonStringer, never()).key(AbstractLog.DEVICE); }