Java 类com.google.gson.stream.JsonWriter 实例源码

项目:MCLibrary    文件:NumberPredictObjectTypeAdapterFactory.java   
@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);
}
项目:non-dominated-sorting    文件:Records.java   
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();
}
项目:MCBot    文件:CommandTrick.java   
@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());
        }
    });
}
项目:lams    文件:Gson.java   
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);
    }
  };
}
项目:CaulCrafting    文件:FancyMessage.java   
/**
 * 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;
}
项目:RankVouchers    文件:TextualComponent.java   
@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();
}
项目:lams    文件:ReflectiveTypeAdapterFactory.java   
@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();
}
项目:data-mediator    文件:BaseTypeAdapter.java   
@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();
}
项目:lams    文件:ObjectTypeAdapter.java   
@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);
}
项目:odoo-work    文件:TypeAdapters.java   
@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();
}
项目:lams    文件:Gson.java   
/**
 * 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);
  }
}
项目:boohee_v5.6    文件:TypeAdapter.java   
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;
        }
    };
}
项目:synthea_java    文件:InnerClassTypeAdapterFactory.java   
@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();
}
项目:RankVouchers    文件:FancyMessage.java   
@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();
    }
}
项目:JavaFX_Tutorial    文件:RecipeLoaderService.java   
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;
}
项目:flow-platform    文件:WebConfig.java   
@Override
public void write(JsonWriter out, ZonedDateTime value) throws IOException {
    if (value == null) {
        out.nullValue();
        return;
    }
    out.value(value.toEpochSecond());
}
项目:degiro-java-client    文件:DUtils.java   
@Override
public void write(JsonWriter writer, DProductType value) throws IOException {
    if (value == null) {
        writer.nullValue();
        return;
    }

    writer.value(value.getTypeCode());
}
项目:data-mediator    文件:Car3SparseArrayTypeAdapter.java   
@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();
}
项目:GitHub    文件:TypeAdapters.java   
@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());
  }
}
项目:GitHub    文件:TypeAdapters.java   
@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());
  }
}
项目:boohee_v5.6    文件:DateTypeAdapter.java   
public synchronized void write(JsonWriter out, Date value) throws IOException {
    if (value == null) {
        out.nullValue();
    } else {
        out.value(this.enUsFormat.format(value));
    }
}
项目:lams    文件:Gson.java   
/**
 * 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;
}
项目:super-volley    文件:GsonRequestBodyConverter.java   
@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());
}
项目:letv    文件:JsonTreeWriter.java   
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();
    }
}
项目:afp-api-client    文件:JSON.java   
@Override
public void write(JsonWriter out, DateTime date) throws IOException {
    if (date == null) {
        out.nullValue();
    } else {
        out.value(formatter.print(date));
    }
}
项目:ForgeHax    文件:SpamEntry.java   
@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();
}
项目:hadoop-oss    文件:KeyProvider.java   
/**
 * 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();
}
项目:kong-java-client    文件:CustomGsonRequestBodyConverter.java   
@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());
}
项目:Smarty-Streets-AutoCompleteTextView    文件:GsonSmartyStreetsApiJsonParser.java   
@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);
    }
}
项目:data-mediator    文件:StudentTypeAdapter.java   
@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();
}
项目:letv    文件:JsonTreeWriter.java   
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();
    }
}
项目:rockscript    文件:Engine.java   
@Override
public void write(JsonWriter out, Instant value) throws IOException {
  if (value!=null) {
    out.value(ISO_FORMATTER.format(value));
  } else {
    out.nullValue();
  }
}
项目:datarouter    文件:OptionalTypeAdapterFactory.java   
@Override
public void write(JsonWriter out, Optional<E> value) throws IOException{
    if(value == null || !value.isPresent()){
        out.nullValue();
    }else{
        typeAdapter.write(out, value.get());
    }
}
项目:mbed-cloud-sdk-java    文件:ApiClient.java   
@Override
public void write(JsonWriter out, DateTime date) throws IOException {
  if (date == null) {
      out.nullValue();
  } else {
      out.value(printFormatter.print(date));
  }
}
项目:ProjectAres    文件:NullableTypeAdapter.java   
@Override
final public void write(JsonWriter out, T value) throws IOException {
  if (value == null) {
    out.nullValue();
  } else {
    writeNonNull(out, value);
  }
}
项目:lams    文件:JsonTreeWriter.java   
@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();
}
项目:XXXX    文件:GsonCodecTest.java   
@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();
}
项目:CaulCrafting    文件:MessagePart.java   
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);
    }
}
项目:ForgeHax    文件:DimensionProperty.java   
@Override
public void serialize(JsonWriter writer) throws IOException {
    writer.beginArray();
    for(DimensionType dimension : dimensions) {
        writer.value(dimension.getName());
    }
    writer.endArray();
}
项目:odoo-work    文件:JsonTreeWriter.java   
@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();
}