當前位置: 首頁>>代碼示例>>Java>>正文


Java JsonWriter類代碼示例

本文整理匯總了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);
}
 
開發者ID:EntryPointKR,項目名稱:MCLibrary,代碼行數:17,代碼來源:NumberPredictObjectTypeAdapterFactory.java

示例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();
}
 
開發者ID:mbuzdalov,項目名稱:non-dominated-sorting,代碼行數:23,代碼來源:Records.java

示例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());
        }
    });
}
 
開發者ID:tterrag1098,項目名稱:MCBot,代碼行數:19,代碼來源:CommandTrick.java

示例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);
    }
  };
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:24,代碼來源:Gson.java

示例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;
}
 
開發者ID:Slaymd,項目名稱:CaulCrafting,代碼行數:23,代碼來源:FancyMessage.java

示例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();
}
 
開發者ID:Chazmondo,項目名稱:RankVouchers,代碼行數:10,代碼來源:TextualComponent.java

示例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();
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:20,代碼來源:ReflectiveTypeAdapterFactory.java

示例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();
}
 
開發者ID:LightSun,項目名稱:data-mediator,代碼行數:18,代碼來源:BaseTypeAdapter.java

示例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);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:17,代碼來源:ObjectTypeAdapter.java

示例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();
}
 
開發者ID:odoo-mobile-intern,項目名稱:odoo-work,代碼行數:22,代碼來源:TypeAdapters.java

示例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);
  }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:22,代碼來源:Gson.java

示例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;
        }
    };
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:20,代碼來源:TypeAdapter.java

示例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();
}
 
開發者ID:synthetichealth,項目名稱:synthea_java,代碼行數:39,代碼來源:InnerClassTypeAdapterFactory.java

示例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();
	}
}
 
開發者ID:Chazmondo,項目名稱:RankVouchers,代碼行數:13,代碼來源:FancyMessage.java

示例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;
}
 
開發者ID:NMSU-SIC-Club,項目名稱:JavaFX_Tutorial,代碼行數:14,代碼來源:RecipeLoaderService.java


注:本文中的com.google.gson.stream.JsonWriter類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。