本文整理汇总了Java中com.google.gson.stream.JsonWriter类的典型用法代码示例。如果您正苦于以下问题:Java JsonWriter类的具体用法?Java JsonWriter怎么用?Java JsonWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonWriter类属于com.google.gson.stream包,在下文中一共展示了JsonWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
@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);
}
示例2: saveToWriter
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
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();
}
示例3: gatherParsers
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
@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());
}
});
}
示例4: doubleAdapter
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
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);
}
};
}
示例5: toJSONString
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
/**
* 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;
}
示例6: writeJson
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
@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();
}
示例7: write
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
@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();
}
示例8: write
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
@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();
}
示例9: write
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
@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);
}
示例10: write
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
@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();
}
示例11: toJson
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
/**
* 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);
}
}
示例12: nullSafe
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
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;
}
};
}
示例13: create
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
@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();
}
示例14: writeJson
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
@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();
}
}
示例15: SaveRecipe
import com.google.gson.stream.JsonWriter; //导入依赖的package包/类
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;
}