@Override public void write(JsonWriter out, Object value) throws IOException { if (value == null) { out.nullValue(); return; } TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) gson.getAdapter(value.getClass()); if (typeAdapter instanceof NumberPredictObjectTypeAdapter) { out.beginObject(); out.endObject(); return; } typeAdapter.write(out, value); }
public static void saveToWriter(Collection<? extends Record> records, Writer writer) throws IOException { JsonWriter jsonWriter = new JsonWriter(writer); jsonWriter.beginArray(); for (Record record : records) { jsonWriter.beginObject(); jsonWriter.name(ALGORITHM_ID).value(record.getAlgorithmId()); jsonWriter.name(DATASET_ID).value(record.getDatasetId()); jsonWriter.name(MEASUREMENT_METHOD).value(record.getMeasurementMethod()); jsonWriter.name(MEASUREMENT_AUTHOR).value(record.getMeasurementAuthor()); jsonWriter.name(MEASUREMENT_TIME).value(record.getMeasurementTime().toString()); jsonWriter.name(CPU_MODEL_NAME).value(record.getCpuModelName()); jsonWriter.name(JAVA_RUNTIME_VERSION).value(record.getJavaRuntimeVersion()); jsonWriter.name(MEASUREMENTS).beginArray(); for (double measurement : record.getMeasurements()) { jsonWriter.value(measurement); } jsonWriter.endArray(); jsonWriter.name(COMMENT).value(record.getComment()); jsonWriter.endObject(); } jsonWriter.endArray(); }
@Override public void gatherParsers(GsonBuilder builder) { builder.registerTypeHierarchyAdapter(TrickType.class, new TypeAdapter<TrickType>() { @Override public TrickType read(JsonReader in) throws IOException { TrickType type = TrickType.byId.get(in.nextString()); if (type == null) { return TrickType.STRING; } return type; } @Override public void write(JsonWriter out, TrickType value) throws IOException { out.value(value.getId()); } }); }
private TypeAdapter<Number> doubleAdapter(boolean serializeSpecialFloatingPointValues) { if (serializeSpecialFloatingPointValues) { return TypeAdapters.DOUBLE; } return new TypeAdapter<Number>() { @Override public Double read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return in.nextDouble(); } @Override public void write(JsonWriter out, Number value) throws IOException { if (value == null) { out.nullValue(); return; } double doubleValue = value.doubleValue(); checkValidFloatingPoint(doubleValue); out.value(value); } }; }
/** * 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; }
@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(); }
@Override public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); return; } out.beginObject(); try { for (BoundField boundField : boundFields.values()) { if (boundField.serialized) { out.name(boundField.name); boundField.write(out, value); } } } catch (IllegalAccessException e) { throw new AssertionError(); } out.endObject(); }
@Override public void write(JsonWriter out, T obj) throws IOException { //log("BaseTypeAdapter_write"); out.beginObject(); for (GsonProperty prop : mProps) { Object val = SupportUtils.getValue(prop, obj); if (val == null) { continue; } //gson name out.name(prop.getRealSerializeName()); // log("simpleType = " + simpleType.getName()); TypeHandler.getTypeHandler(prop).write(out, prop, val); } out.endObject(); }
@SuppressWarnings("unchecked") @Override public void write(JsonWriter out, Object value) throws IOException { if (value == null) { out.nullValue(); return; } TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) gson.getAdapter(value.getClass()); if (typeAdapter instanceof ObjectTypeAdapter) { out.beginObject(); out.endObject(); return; } typeAdapter.write(out, value); }
@Override public void write(JsonWriter out, Calendar value) throws IOException { if (value == null) { out.nullValue(); return; } out.beginObject(); out.name(YEAR); out.value(value.get(Calendar.YEAR)); out.name(MONTH); out.value(value.get(Calendar.MONTH)); out.name(DAY_OF_MONTH); out.value(value.get(Calendar.DAY_OF_MONTH)); out.name(HOUR_OF_DAY); out.value(value.get(Calendar.HOUR_OF_DAY)); out.name(MINUTE); out.value(value.get(Calendar.MINUTE)); out.name(SECOND); out.value(value.get(Calendar.SECOND)); out.endObject(); }
/** * Writes the JSON for {@code jsonElement} to {@code writer}. * @throws JsonIOException if there was a problem writing to the writer */ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException { boolean oldLenient = writer.isLenient(); writer.setLenient(true); boolean oldHtmlSafe = writer.isHtmlSafe(); writer.setHtmlSafe(htmlSafe); boolean oldSerializeNulls = writer.getSerializeNulls(); writer.setSerializeNulls(serializeNulls); try { Streams.write(jsonElement, writer); } catch (IOException e) { throw new JsonIOException(e); } finally { writer.setLenient(oldLenient); writer.setHtmlSafe(oldHtmlSafe); writer.setSerializeNulls(oldSerializeNulls); } }
public final TypeAdapter<T> nullSafe() { return new TypeAdapter<T>() { public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); } else { TypeAdapter.this.write(out, value); } } public T read(JsonReader reader) throws IOException { if (reader.peek() != JsonToken.NULL) { return TypeAdapter.this.read(reader); } reader.nextNull(); return null; } }; }
@Override public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); try { String subclassName = baseType.getName() + "$" + label.replaceAll("\\s", ""); Class<?> subclass = Class.forName(subclassName); @SuppressWarnings("unchecked") TypeAdapter<R> delegate = (TypeAdapter<R>) gson.getDelegateAdapter( InnerClassTypeAdapterFactory.this, TypeToken.get(subclass)); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label); } return delegate.fromJsonTree(jsonElement); } catch (ClassNotFoundException e) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label); } } @Override public void write(JsonWriter out, R value) throws IOException { throw new NotImplementedException("Write not implemented for InnerClassTypeAdapter"); } }.nullSafe(); }
@Override 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 boolean SaveRecipe(Recipe recipe) { String filename = nameService.simplify(recipe.name); Path filepath = Paths.get(getRecipesFolder().getPath(), (filename + ".json")); try { JsonWriter writer = new JsonWriter(new FileWriter(filepath.toFile())); gson.toJson(recipe, Recipe.class, writer); } catch (JsonIOException | IOException e) { logger.error(e.getMessage()); e.printStackTrace(); return false; } return true; }
@Override public void write(JsonWriter out, ZonedDateTime value) throws IOException { if (value == null) { out.nullValue(); return; } out.value(value.toEpochSecond()); }
@Override public void write(JsonWriter writer, DProductType value) throws IOException { if (value == null) { writer.nullValue(); return; } writer.value(value.getTypeCode()); }
@Override public void write(JsonWriter out, SparseArray<Car3> value) throws IOException { out.beginObject(); if(value != null) { for (int size = value.size(), i = size - 1; i >= 0; i--) { out.name(value.keyAt(i) + ""); mAdapter.write(out, value.valueAt(i)); } } out.endObject(); }
@Override public void write(JsonWriter out, Id value) throws IOException { if (out instanceof BsonWriter) { OBJECT_ID_ADAPTER.write(out, value.value()); } else { out.value(value.toString()); } }
@Override public void write(JsonWriter out, Binary value) throws IOException { if (out instanceof BsonWriter) { BINARY_ADAPTER.write(out, value.value()); } else { out.value(value.toString()); } }
public synchronized void write(JsonWriter out, Date value) throws IOException { if (value == null) { out.nullValue(); } else { out.value(this.enUsFormat.format(value)); } }
/** * Returns a new JSON writer configured for this GSON and with the non-execute * prefix if that is configured. */ private JsonWriter newJsonWriter(Writer writer) throws IOException { if (generateNonExecutableJson) { writer.write(JSON_NON_EXECUTABLE_PREFIX); } JsonWriter jsonWriter = new JsonWriter(writer); if (prettyPrinting) { jsonWriter.setIndent(" "); } jsonWriter.setSerializeNulls(serializeNulls); return jsonWriter; }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); }
public JsonWriter endObject() throws IOException { if (this.stack.isEmpty() || this.pendingName != null) { throw new IllegalStateException(); } else if (peek() instanceof JsonObject) { this.stack.remove(this.stack.size() - 1); return this; } else { throw new IllegalStateException(); } }
@Override public void write(JsonWriter out, DateTime date) throws IOException { if (date == null) { out.nullValue(); } else { out.value(formatter.print(date)); } }
@Override public void serialize(JsonWriter writer) throws IOException { writer.beginObject(); writer.name("enabled"); writer.value(enabled); writer.name("keyword"); writer.value(keyword); writer.name("type"); writer.value(type.name()); writer.name("trigger"); writer.value(trigger.name()); writer.name("delay"); writer.value(getDelay()); writer.name("messages"); writer.beginArray(); for(String msg : messages) { writer.value(msg); } writer.endArray(); writer.endObject(); }
/** * Serialize the metadata to a set of bytes. * @return the serialized bytes * @throws IOException */ protected byte[] serialize() throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); JsonWriter writer = new JsonWriter( new OutputStreamWriter(buffer, Charsets.UTF_8)); try { writer.beginObject(); if (cipher != null) { writer.name(CIPHER_FIELD).value(cipher); } if (bitLength != 0) { writer.name(BIT_LENGTH_FIELD).value(bitLength); } if (created != null) { writer.name(CREATED_FIELD).value(created.getTime()); } if (description != null) { writer.name(DESCRIPTION_FIELD).value(description); } if (attributes != null && attributes.size() > 0) { writer.name(ATTRIBUTES_FIELD).beginObject(); for (Map.Entry<String, String> attribute : attributes.entrySet()) { writer.name(attribute.getKey()).value(attribute.getValue()); } writer.endObject(); } writer.name(VERSIONS_FIELD).value(versions); writer.endObject(); writer.flush(); } finally { writer.close(); } return buffer.toByteArray(); }
@Override public void writeHistoryJson(final OutputStream os, final List<Address> addresses) throws JsonWritingException { try { JsonWriter writer = new JsonWriter(new OutputStreamWriter(os, "UTF-8")); writer.setIndent(" "); writer.beginArray(); for (Address address : addresses) { gson.toJson(address, Address.class, writer); } writer.endArray(); writer.close(); } catch (Exception e) { throw new JsonWritingException(e); } }
@Override public void write(JsonWriter out, IStudent value) throws IOException { GsonTest.log("write"); out.beginObject(); out.name("name").value("heaven7"); out.name("id").value("26"); out.endObject(); }
public JsonWriter endArray() throws IOException { if (this.stack.isEmpty() || this.pendingName != null) { throw new IllegalStateException(); } else if (peek() instanceof JsonArray) { this.stack.remove(this.stack.size() - 1); return this; } else { throw new IllegalStateException(); } }
@Override public void write(JsonWriter out, Instant value) throws IOException { if (value!=null) { out.value(ISO_FORMATTER.format(value)); } else { out.nullValue(); } }
@Override public void write(JsonWriter out, Optional<E> value) throws IOException{ if(value == null || !value.isPresent()){ out.nullValue(); }else{ typeAdapter.write(out, value.get()); } }
@Override public void write(JsonWriter out, DateTime date) throws IOException { if (date == null) { out.nullValue(); } else { out.value(printFormatter.print(date)); } }
@Override final public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); } else { writeNonNull(out, value); } }
@Override public JsonWriter endObject() throws IOException { if (stack.isEmpty() || pendingName != null) { throw new IllegalStateException(); } JsonElement element = peek(); if (element instanceof JsonObject) { stack.remove(stack.size() - 1); return this; } throw new IllegalStateException(); }
@Override public void write(JsonWriter out, Zone value) throws IOException { out.beginObject(); for (Map.Entry<String, Object> entry : value.entrySet()) { out.name(entry.getKey()).value(entry.getValue().toString().toUpperCase()); } out.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(); } if (insertionData != null) { json.name("insertion").value(insertionData); } if (translationReplacements.size() > 0 && text != null && TextualComponent.isTranslatableText(text)) { json.name("with").beginArray(); for (JsonRepresentedObject obj : translationReplacements) { obj.writeJson(json); } json.endArray(); } json.endObject(); } catch (IOException e) { Bukkit.getLogger().log(Level.WARNING, "A problem occured during writing of JSON string", e); } }
@Override public void serialize(JsonWriter writer) throws IOException { writer.beginArray(); for(DimensionType dimension : dimensions) { writer.value(dimension.getName()); } writer.endArray(); }