public void writeJson(JsonWriter writer) throws IOException { if (messageParts.size() == 1) { latest().writeJson(writer); } else { writer.beginObject().name("text").value("").name("extra").beginArray(); for (final MessagePart part : this) { part.writeJson(writer); } writer.endArray().endObject(); } }
public String toJSONString() { final JSONStringer json = new JSONStringer(); try { if (messageParts.size() == 1) { latest().writeJson(json); } else { json.object().key("text").value("").key("extra").array(); for (final MessagePart part : messageParts) { part.writeJson(json); } json.endArray().endObject(); } } catch (final JSONException e) { throw new RuntimeException("invalid message"); } return json.toString(); }
public String toJSONString() { if (!dirty && jsonString != null) { return jsonString; } StringWriter string = new StringWriter(); JsonWriter json = new JsonWriter(string); try { if (messageParts.size() == 1) { latest().writeJson(json); } else { json.beginObject().name("text").value("").name("extra").beginArray(); for (final MessagePart part : messageParts) { part.writeJson(json); } json.endArray().endObject(); json.close(); } } catch (Exception e) { throw new RuntimeException("invalid message"); } jsonString = string.toString(); dirty = false; return jsonString; }