Exemplo n.º 1
0
 @Override
 public void write(JsonWriter out, Quest value) throws IOException {
   out.beginObject();
   out.name(NAME).value(value.getName());
   out.name(DESCRIPTION).value(value.getDescription());
   out.name(X).value(value.getGuiX());
   out.name(Y).value(value.getGuiY());
   if (value.useBigIcon()) {
     out.name(BIG_ICON).value(true);
   }
   if (value.getIcon() != null) {
     MinecraftAdapter.ITEM_STACK.write(out.name(ICON), value.getIcon());
   }
   writeQuestList(
       out, value.getRequirement(), value.getQuestSet().getQuests(), REQUIREMENTS);
   writeQuestList(out, value.getOptionLinks(), value.getQuestSet().getQuests(), OPTIONS);
   if (value.getRepeatInfo().getType() != RepeatType.NONE) {
     REPEAT_INFO_ADAPTER.write(out.name(REPEAT), value.getRepeatInfo());
   }
   if (value.getTriggerType() != TriggerType.NONE) {
     out.name(TRIGGER).value(value.getTriggerType().name());
   }
   if (value.getTriggerType().isUseTaskCount()) {
     out.name(TRIGGER_TASKS).value(value.getTriggerTasks());
   }
   if (value.getUseModifiedParentRequirement()) {
     out.name(PARENT_REQUIREMENT).value(value.getParentRequirementCount());
   }
   out.name(TASKS).beginArray();
   for (QuestTask task : value.getTasks()) {
     TASK_ADAPTER.write(out, task);
   }
   out.endArray();
   writeItemStackArray(
       out,
       (ItemStack[]) ReflectionHelper.getPrivateValue(Quest.class, value, REWARDS),
       REWARDS);
   writeItemStackArray(
       out,
       (ItemStack[]) ReflectionHelper.getPrivateValue(Quest.class, value, REWARDS_CHOICE),
       REWARDS_CHOICE);
   if (value.getReputationRewards() != null && !value.getReputationRewards().isEmpty()) {
     out.name(REWARDS_REPUTATION).beginArray();
     for (Quest.ReputationReward reward : value.getReputationRewards()) {
       REPUTATION_REWARD_ADAPTER.write(out, reward);
     }
     out.endArray();
   }
   out.endObject();
 }
Exemplo n.º 2
0
  @Override
  public void write(JsonWriter out, Patch<Data> value) throws IOException {
    JsonTreeWriter treeWriter = new JsonTreeWriter();
    treeWriter.setSerializeNulls(true);
    _dataAdapter.write(treeWriter, value.data());
    JsonObject tree = treeWriter.get().getAsJsonObject();

    Set<String> presentJsonProperties = new HashSet<>();
    for (Field field : value.changedFields()) {
      SerializedName nameSpecifier = field.getAnnotation(SerializedName.class);
      String jsonPropertyName = (nameSpecifier != null ? nameSpecifier.value() : field.getName());
      presentJsonProperties.add(jsonPropertyName);
    }

    for (Map.Entry<String, JsonElement> entry : new ArrayList<>(tree.entrySet())) {
      if (!presentJsonProperties.contains(entry.getKey())) {
        tree.remove(entry.getKey());
      }
    }

    // If a data field changed to null the null should be serialized.
    // This is the whole point of this adapter.
    ObjectTargetedJsonWriter patchWriter = new ObjectTargetedJsonWriter(out);
    patchWriter.setTargetedSerializeNulls(true);
    _gson.getAdapter(JsonObject.class).write(patchWriter, tree);
  }
Exemplo n.º 3
0
 /**
  * Converts {@code value} to a JSON tree.
  *
  * @param value the Java object to convert. May be null.
  * @return the converted JSON tree. May be {@link JsonNull}.
  * @since 2.2
  */
 public final JsonElement toJsonTree(T value) {
   try {
     JsonTreeWriter jsonWriter = new JsonTreeWriter();
     write(jsonWriter, value);
     return jsonWriter.get();
   } catch (IOException e) {
     throw new JsonIOException(e);
   }
 }
Exemplo n.º 4
0
 public void write(JsonWriter jsonwriter, Object obj) {
   if (obj == null) {
     jsonwriter.nullValue();
     return;
   } else {
     a.write(jsonwriter, obj);
     return;
   }
 }
Exemplo n.º 5
0
 @Override
 public void write(JsonWriter out, QuestTask value) throws IOException {
   out.beginObject();
   TaskType type = TaskType.getType(value.getClass());
   out.name(TYPE).value(type.name());
   if (!value.getDescription().equals(type.name))
     out.name(DESCRIPTION).value(value.getDescription());
   if (!value.getLongDescription().equals(type.description))
     out.name(LONG_DESCRIPTION).value(value.getLongDescription());
   if (value instanceof QuestTaskItems) {
     out.name(ITEMS).beginArray();
     for (QuestTaskItems.ItemRequirement requirement : ((QuestTaskItems) value).getItems()) {
       ITEM_REQUIREMENT_ADAPTER.write(out, requirement);
     }
     out.endArray();
   } else if (value instanceof QuestTaskDeath) {
     out.name(DEATHS).value(((QuestTaskDeath) value).getDeaths());
   } else if (value instanceof QuestTaskLocation) {
     out.name(LOCATIONS).beginArray();
     for (QuestTaskLocation.Location requirement : ((QuestTaskLocation) value).locations) {
       LOCATION_ADAPTER.write(out, requirement);
     }
     out.endArray();
   } else if (value instanceof QuestTaskMob) {
     out.name(MOBS).beginArray();
     for (QuestTaskMob.Mob requirement : ((QuestTaskMob) value).mobs) {
       MOB_ADAPTER.write(out, requirement);
     }
     out.endArray();
   } else if (value instanceof QuestTaskReputation) {
     out.name(REPUTATION).beginArray();
     for (QuestTaskReputation.ReputationSetting requirement :
         ((QuestTaskReputation) value).getSettings()) {
       REPUTATION_ADAPTER.write(out, requirement);
     }
     out.endArray();
     if (value instanceof QuestTaskReputationKill) {
       out.name(KILLS).value(((QuestTaskReputationKill) value).getKills());
     }
   }
   out.endObject();
 }
Exemplo n.º 6
0
 @Override
 public void write(JsonWriter out, QuestSet value) throws IOException {
   out.beginObject();
   out.name(NAME).value(value.getName());
   out.name(DESCRIPTION).value(value.getDescription());
   out.name(QUESTS).beginArray();
   for (Quest quest : value.getQuests()) {
     QUEST_ADAPTER.write(out, quest);
   }
   out.endArray().endObject();
 }
    public void write(JsonWriter out, Collection<E> collection) throws IOException {
      if (collection == null) {
        out.nullValue(); // TODO: better policy here?
        return;
      }

      out.beginArray();
      for (E element : collection) {
        elementTypeAdapter.write(out, element);
      }
      out.endArray();
    }
 public void write(JsonWriter out, Map<K, V> value) throws IOException {
   if (value == null) {
     out.nullValue();
     return;
   }
   out.beginObject();
   for (Map.Entry<K, V> element : value.entrySet()) {
     out.name(String.valueOf(element.getKey()));
     valueAdapter.write(out, element.getValue());
   }
   out.endObject();
 }
Exemplo n.º 9
0
 public void toJson(Object obj, Type type, JsonWriter jsonWriter) throws JsonIOException {
   TypeAdapter adapter = getAdapter(TypeToken.get(type));
   boolean isLenient = jsonWriter.isLenient();
   jsonWriter.setLenient(true);
   boolean isHtmlSafe = jsonWriter.isHtmlSafe();
   jsonWriter.setHtmlSafe(this.htmlSafe);
   boolean serializeNulls = jsonWriter.getSerializeNulls();
   jsonWriter.setSerializeNulls(this.serializeNulls);
   try {
     adapter.write(jsonWriter, obj);
     jsonWriter.setLenient(isLenient);
     jsonWriter.setHtmlSafe(isHtmlSafe);
     jsonWriter.setSerializeNulls(serializeNulls);
   } catch (Throwable e) {
     throw new JsonIOException(e);
   } catch (Throwable th) {
     jsonWriter.setLenient(isLenient);
     jsonWriter.setHtmlSafe(isHtmlSafe);
     jsonWriter.setSerializeNulls(serializeNulls);
   }
 }
Exemplo n.º 10
0
 /**
  * Converts {@code value} to a JSON document and writes it to {@code out}. Unlike Gson's similar
  * {@link Gson#toJson(JsonElement, Appendable) toJson} method, this write is strict. Create a
  * {@link JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call {@link
  * #write(com.google.gson.stream.JsonWriter, Object)} for lenient writing.
  *
  * @param value the Java object to convert. May be null.
  * @since 2.2
  */
 public final void toJson(Writer out, T value) throws IOException {
   JsonWriter writer = new JsonWriter(out);
   write(writer, value);
 }
 @Override
 public void write(JsonWriter out, T value) throws IOException {
   // Cannot determine what was annotated, and what was in schedules
   delegateAdapter.write(out, value);
 }