/** * Serialize this fancy message, converting it into syntactically-valid JSON using a {@link JsonWriter}. * This JSON should be compatible with vanilla formatter commands such as {@code /tellraw}. * @return The JSON string representing this object. */ public String toJSONString() { if (!dirty && jsonString != null) { return jsonString; } StringWriter string = new StringWriter(); JsonWriter json = new JsonWriter(string); try { writeJson(json); json.close(); } catch (Exception e) { throw new RuntimeException("invalid message"); } jsonString = string.toString(); dirty = false; return jsonString; }
/** * Serialize this fancy message, converting it into syntactically-valid JSON using a {@link JsonWriter}. * This JSON should be compatible with vanilla formatter commands such as {@code /tellraw}. * @return The JSON string representing this object. */ public String toJSONString() { if (!dirty && jsonString != null) { return jsonString; } StringWriter string = new StringWriter(); JsonWriter json = new JsonWriter(string); try { writeJson(json); json.close(); } catch (IOException e) { throw new RuntimeException("invalid message"); } jsonString = string.toString(); dirty = false; return jsonString; }
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; }
private String makeMultilineTooltip(final String[] lines) { StringWriter string = new StringWriter(); JsonWriter json = new JsonWriter(string); try { json.beginObject().name("id").value(1); json.name("tag").beginObject().name("display").beginObject(); json.name("Name").value("\\u00A7f" + lines[0].replace("\"", "\\\"")); json.name("Lore").beginArray(); for (int i = 1; i < lines.length; i++) { final String line = lines[i]; json.value(line.isEmpty() ? " " : line.replace("\"", "\\\"")); } json.endArray().endObject().endObject().endObject(); json.close(); } catch (Exception e) { throw new RuntimeException("invalid tooltip"); } return string.toString(); }
public void writeJson(JsonWriter json) { try { json.beginObject(); text.writeJson(json); json.name("color").value(color.name().toLowerCase()); for (final ChatColor style : styles) { json.name(stylesToNames.get(style)).value(true); } if (clickActionName != null && clickActionData != null) { json.name("clickEvent") .beginObject() .name("action").value(clickActionName) .name("value").value(clickActionData) .endObject(); } if (hoverActionName != null && hoverActionData != null) { json.name("hoverEvent") .beginObject() .name("action").value(hoverActionName) .name("value"); hoverActionData.writeJson(json); json.endObject(); } json.endObject(); } catch(Exception e){ e.printStackTrace(); } }
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(); } }
@Override public void writeJson(JsonWriter writer) throws IOException { writer.name(getKey()); writer.beginObject(); for(Map.Entry<String, String> jsonPair : _value.entrySet()){ writer.name(jsonPair.getKey()).value(jsonPair.getValue()); } writer.endObject(); }
public void writeJson(JsonWriter json) { try { json.beginObject(); text.writeJson(json); json.name("color").value(color.name().toLowerCase()); for (final ChatColor style : styles) { json.name(stylesToNames.get(style)).value(true); } if (clickActionName != null && clickActionData != null) { json.name("clickEvent") .beginObject() .name("action").value(clickActionName) .name("value").value(clickActionData) .endObject(); } if (hoverActionName != null && hoverActionData != null) { json.name("hoverEvent") .beginObject() .name("action").value(hoverActionName) .name("value"); hoverActionData.writeJson(json); json.endObject(); } json.endObject(); } catch(IOException e){ Bukkit.getLogger().log(Level.WARNING, "A problem occured during writing of JSON string", e); } }
/** * Save to the {@link com.relicum.ipsum.Configuration.PersistentPlayer} object to disk in JSON format. * * @param persistentPlayer the {@link com.relicum.ipsum.Configuration.PersistentPlayer} that will be serialized to JSON and saved to disk */ public void saveToDisk(PersistentPlayer persistentPlayer) { try { JsonWriter jsonWriter = GsonIO.writer(Paths.get(stringPath, persistentPlayer.getUuid() + ".json")); jsonWriter.setIndent(" "); gson.toJson(persistentPlayer, type, jsonWriter); jsonWriter.flush(); jsonWriter.close(); } catch (IOException e) { e.printStackTrace(); } }
public static void writeToFile(Path path, Object src, Type typeOfSrc) throws IOException { JsonWriter writer = writer(path); gson.toJson(src, typeOfSrc, writer); writer.flush(); writer.close(); }
public static void writeToFile(Path path, Object src, Type typeOfSrc) throws IOException { JsonWriter writer = writer(path); writer.setIndent(" "); gson.toJson(src, typeOfSrc, writer); writer.flush(); writer.close(); }
JsonWriter writeJson(JsonWriter json) { try { json.beginObject().name("text").value(text); if (color != null) { json.name("color").value(color.name().toLowerCase()); } if (styles != null) { for (final ChatColor style : styles) { json.name(style.name().toLowerCase()).value(true); } } if (clickActionName != null && clickActionData != null) { json.name("clickEvent") .beginObject() .name("action").value(clickActionName) .name("value").value(clickActionData) .endObject(); } if (hoverActionName != null && hoverActionData != null) { json.name("hoverEvent") .beginObject() .name("action").value(hoverActionName) .name("value").value(hoverActionData) .endObject(); } return json.endObject(); } catch (Exception e) { e.printStackTrace(); return json; } }
public void writeJson(JsonWriter writer) throws IOException { writer.value(getValue()); }
@Override public void writeJson(JsonWriter writer) throws IOException { writer.name(getKey()).value(getValue()); }
/** * Writes the JSON representation of this object to the specified writer. * @param writer The JSON writer which will receive the object. * @throws IOException If an error occurs writing to the stream. */ public void writeJson(JsonWriter writer) throws IOException;
/** * Writes the text data represented by this textual component to the specified JSON writer object. * A new object within the writer is not started. * @param writer The object to which to write the JSON data. * @throws IOException If an error occurs while writing to the stream. */ public abstract void writeJson(JsonWriter writer) throws IOException;
/** * Return a new {@link com.google.gson.stream.JsonWriter} at the specified {@link java.nio.file.Path} location * * @param path the location on disk of the file to write to * @return the {@link com.google.gson.stream.JsonWriter} * @throws java.io.IOException if there was an exception while trying to create the writer */ public static JsonWriter writer(Path path) throws IOException { return new JsonWriter(Files.newBufferedWriter(path, Charset.defaultCharset())); }
/** * Return a new {@link com.google.gson.stream.JsonWriter} at the specified {@link java.nio.file.Path} location * * @param path the location on disk of the file to write to * @return the {@link com.google.gson.stream.JsonWriter} * @throws IOException if there was an exception while trying to create the writer */ public static JsonWriter writer(Path path) throws IOException { return new JsonWriter(Files.newBufferedWriter(path, Charset.defaultCharset())); }