public Map<K, V> read(JsonReader in) throws IOException {
   Map<K, V> result = Maps.newHashMap();
   in.beginObject();
   while (in.hasNext()) {
     JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
     K name = keyAdapter.read(in);
     V value = valueAdapter.read(in);
     if (value != null) {
       // If there are repeated keys, overwrite them to only keep the last one
       result.put(name, value);
     }
   }
   in.endObject();
   return ImmutableMap.copyOf(result);
 }
Esempio n. 2
0
 public Object read(JsonReader jsonreader) {
   if (jsonreader.peek() == JsonToken.NULL) {
     jsonreader.nextNull();
     return null;
   } else {
     return a.read(jsonreader);
   }
 }
    public Collection<E> read(JsonReader in) throws IOException {
      if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
      }

      Collection<E> collection = constructor.construct();
      in.beginArray();
      while (in.hasNext()) {
        E instance = elementTypeAdapter.read(in);
        collection.add(instance);
      }
      in.endArray();
      return collection;
    }
Esempio n. 4
0
 @Override
 public QuestSet read(JsonReader in) throws IOException {
   String name = null, description = null;
   requirementMapping.clear();
   optionMapping.clear();
   List<Quest> quests = new ArrayList<Quest>();
   in.beginObject();
   while (in.hasNext()) {
     String next = in.nextName();
     if (next.equalsIgnoreCase(NAME)) {
       name = in.nextString();
     } else if (next.equalsIgnoreCase(DESCRIPTION)) {
       description = in.nextString();
     } else if (next.equalsIgnoreCase(QUESTS)) {
       in.beginArray();
       QUEST_ID = Quest.size();
       while (in.hasNext()) {
         Quest quest = QUEST_ADAPTER.read(in);
         if (quest != null) {
           quests.add(quest);
         }
       }
       in.endArray();
     }
   }
   in.endObject();
   for (QuestSet set : Quest.getQuestSets()) {
     if (set.getName().equals(name)) {
       return removeQuests(quests);
     }
   }
   if (name != null && description != null) {
     QuestSet set = new QuestSet(name, description);
     Quest.getQuestSets().add(set);
     SaveHelper.add(SaveHelper.EditType.SET_CREATE);
     for (Quest quest : quests) {
       quest.setQuestSet(set);
     }
     for (Map.Entry<Quest, List<Integer>> entry : requirementMapping.entrySet()) {
       for (int i : entry.getValue()) entry.getKey().addRequirement(i);
     }
     for (Map.Entry<Quest, List<Integer>> entry : optionMapping.entrySet()) {
       for (int i : entry.getValue()) entry.getKey().addOptionLink(i);
     }
     return set;
   }
   return removeQuests(quests);
 }
  @Override
  public Patch<Data> read(JsonReader in) throws IOException {
    final Set<String> receivedJsonProperties = new HashSet<>();
    JsonReader watchingReader =
        new ObjectWatchingJsonReader(in) {
          @Override
          protected void onNextName(String foundName) {
            receivedJsonProperties.add(foundName);
          }
        };

    Data data = _dataAdapter.read(watchingReader);

    List<Field> changedFields = new ArrayList<>(receivedJsonProperties.size());
    for (Field field : data.getClass().getDeclaredFields()) {
      SerializedName nameSpecifier = field.getAnnotation(SerializedName.class);
      String jsonPropertyName = (nameSpecifier != null ? nameSpecifier.value() : field.getName());
      if (receivedJsonProperties.contains(jsonPropertyName)) {
        changedFields.add(field);
      }
    }

    return new Patch<>(data, changedFields);
  }
    @Override
    public T read(JsonReader in) throws IOException {
      JsonObject el = Streams.parse(in).getAsJsonObject();

      // Strip existing json schedules
      JsonObject directSchedules =
          el.has(SCHEDULES_FIELD_NAME)
              ? el.remove(SCHEDULES_FIELD_NAME).getAsJsonObject()
              : new JsonObject();

      // Strip off @schedule annotations to update schedules attribute
      // Load annotated schedules
      JsonObject annotatedSchedules = new JsonObject();
      // TODO: make this recursive to have nested schedules with dot notation
      for (Map.Entry<String, JsonElement> entry : el.entrySet()) {
        JsonElement entryEl = entry.getValue();
        if (entryEl.isJsonObject()) {
          JsonObject subConfig = entryEl.getAsJsonObject();
          if (subConfig.has(SCHEDULE)) {
            JsonElement scheduleConfig = subConfig.remove(SCHEDULE);
            annotatedSchedules.add(entry.getKey(), scheduleConfig);
          }
        }
      }

      T result = delegateAdapter.read(new JsonTreeReader(el));

      // If there is a 'schedules' field, inject schedules
      Field schedulesField = AnnotationUtil.getField(SCHEDULES_FIELD_NAME, result.getClass());
      if (schedulesField != null) {
        ///// Default schedules for every schedulable top level object
        JsonObject defaultSchedules = new JsonObject();
        List<Field> fields = new ArrayList<Field>();
        for (Field field : AnnotationUtil.getAllFields(fields, result.getClass())) {
          DefaultSchedule defaultSchedule = field.getAnnotation(DefaultSchedule.class);
          if (defaultSchedule == null) {
            boolean currentAccessibility = field.isAccessible();
            try {
              field.setAccessible(true);
              Object fieldValue = field.get(result);
              if (fieldValue != null) {
                Class<?> fieldRuntimeClass = field.get(result).getClass();
                defaultSchedule = fieldRuntimeClass.getAnnotation(DefaultSchedule.class);
              }
            } catch (IllegalArgumentException e) {
              Log.e(LogUtil.TAG, "Bad access of configurable fields!!", e);
            } catch (IllegalAccessException e) {
              Log.e(LogUtil.TAG, "Bad access of configurable fields!!", e);
            } finally {
              field.setAccessible(currentAccessibility);
            }
          }
          if (defaultSchedule != null) {
            defaultSchedules.add(
                field.getName(), gson.toJsonTree(defaultSchedule, DefaultSchedule.class));
          }
        }

        JsonObject schedulesJson = directSchedules;
        JsonUtils.deepCopyOnto(
            defaultSchedules,
            schedulesJson,
            false); // Copy in default schedules, but do not replace
        JsonUtils.deepCopyOnto(
            annotatedSchedules, schedulesJson, true); // Override with annotations

        // For each schedule find default schedule, fill in remainder
        Map<String, Schedule> schedules =
            gson.fromJson(schedulesJson, new TypeToken<Map<String, Schedule>>() {}.getType());

        boolean currentAccessibility = schedulesField.isAccessible();
        try {
          schedulesField.setAccessible(true);
          schedulesField.set(result, schedules);
        } catch (IllegalArgumentException e) {
          Log.e(LogUtil.TAG, "Bad access of configurable fields!!", e);
        } catch (IllegalAccessException e) {
          Log.e(LogUtil.TAG, "Bad access of configurable fields!!", e);
        } finally {
          schedulesField.setAccessible(currentAccessibility);
        }
      }

      return result;
    }
Esempio n. 7
0
 @Override
 public Quest read(JsonReader in) throws IOException {
   int ID_OFFSET = Quest.size();
   QUEST = new Quest(ID_OFFSET, "", "", 0, 0, false);
   List<Integer> requirement = new ArrayList<>(), options = new ArrayList<>();
   in.beginObject();
   while (in.hasNext()) {
     switch (in.nextName()) {
       case NAME:
         QUEST.setName(in.nextString());
         break;
       case DESCRIPTION:
         QUEST.setDescription(in.nextString());
         break;
       case X:
         ReflectionHelper.setPrivateValue(Quest.class, QUEST, in.nextInt(), X);
         break;
       case Y:
         ReflectionHelper.setPrivateValue(Quest.class, QUEST, in.nextInt(), Y);
         break;
       case TRIGGER_TASKS:
         QUEST.setTriggerTasks(in.nextInt());
         break;
       case PARENT_REQUIREMENT:
         QUEST.setParentRequirementCount(in.nextInt());
         break;
       case BIG_ICON:
         QUEST.setBigIcon(in.nextBoolean());
         break;
       case ICON:
         QUEST.setIcon(MinecraftAdapter.ITEM_STACK.read(in));
         break;
       case REQUIREMENTS:
         in.beginArray();
         while (in.hasNext()) {
           requirement.add(in.nextInt() + QUEST_ID);
         }
         in.endArray();
         break;
       case OPTIONS:
         in.beginArray();
         while (in.hasNext()) {
           options.add(in.nextInt() + QUEST_ID);
         }
         in.endArray();
         break;
       case REPEAT:
         QUEST.setRepeatInfo(REPEAT_INFO_ADAPTER.read(in));
         break;
       case TRIGGER:
         QUEST.setTriggerType(TriggerType.valueOf(in.nextString()));
         break;
       case TASKS:
         in.beginArray();
         while (in.hasNext()) {
           QuestTask task = TASK_ADAPTER.read(in);
           if (task != null) {
             QUEST.getTasks().add(task);
           }
         }
         in.endArray();
         break;
       case REWARDS:
         ReflectionHelper.setPrivateValue(
             Quest.class, QUEST, readItemStackArray(in), REWARDS);
         break;
       case REWARDS_CHOICE:
         ReflectionHelper.setPrivateValue(
             Quest.class, QUEST, readItemStackArray(in), REWARDS_CHOICE);
         break;
       case REWARDS_REPUTATION:
         in.beginArray();
         List<Quest.ReputationReward> reputationRewards = new ArrayList<>();
         while (in.hasNext()) {
           Quest.ReputationReward reward = REPUTATION_REWARD_ADAPTER.read(in);
           if (reward != null) reputationRewards.add(reward);
         }
         QUEST.setReputationRewards(reputationRewards);
         in.endArray();
         break;
     }
   }
   in.endObject();
   if (!QUEST.getName().isEmpty()) {
     requirementMapping.put(QUEST, requirement);
     optionMapping.put(QUEST, options);
     return QUEST;
   }
   QuestLine.getActiveQuestLine().quests.remove(QUEST.getId());
   return null;
 }
Esempio n. 8
0
 @Override
 public QuestTask read(JsonReader in) throws IOException {
   in.beginObject();
   if (!in.nextName().equalsIgnoreCase(TYPE)) {
     throw new IOException("Tasks *MUST* start with the type");
   }
   String task = in.nextString();
   TaskType type = TaskType.valueOf(task);
   if (type == null) {
     throw new IOException("Invalid Task Type: " + task);
   }
   TASK = type.addTask(QUEST);
   while (in.hasNext()) {
     String name = in.nextName();
     if (name.equalsIgnoreCase(DESCRIPTION)) {
       TASK.description = in.nextString();
     } else if (name.equalsIgnoreCase(LONG_DESCRIPTION)) {
       TASK.setLongDescription(in.nextString());
     } else if (TASK instanceof QuestTaskItems && name.equalsIgnoreCase(ITEMS)) {
       List<QuestTaskItems.ItemRequirement> list = new ArrayList<>();
       in.beginArray();
       while (in.hasNext()) {
         QuestTaskItems.ItemRequirement entry = ITEM_REQUIREMENT_ADAPTER.read(in);
         if (entry != null) list.add(entry);
       }
       in.endArray();
       ((QuestTaskItems) TASK)
           .setItems(list.toArray(new QuestTaskItems.ItemRequirement[list.size()]));
     } else if (TASK instanceof QuestTaskDeath && name.equalsIgnoreCase(DEATHS)) {
       int death = in.nextInt();
       ((QuestTaskDeath) TASK).setDeaths(death);
     } else if (TASK instanceof QuestTaskLocation && name.equalsIgnoreCase(LOCATIONS)) {
       List<QuestTaskLocation.Location> list = new ArrayList<QuestTaskLocation.Location>();
       in.beginArray();
       while (in.hasNext()) {
         QuestTaskLocation.Location entry = LOCATION_ADAPTER.read(in);
         if (entry != null) list.add(entry);
       }
       in.endArray();
       ((QuestTaskLocation) TASK).locations =
           list.toArray(new QuestTaskLocation.Location[list.size()]);
     } else if (TASK instanceof QuestTaskMob && name.equalsIgnoreCase(MOBS)) {
       List<QuestTaskMob.Mob> list = new ArrayList<QuestTaskMob.Mob>();
       in.beginArray();
       while (in.hasNext()) {
         QuestTaskMob.Mob entry = MOB_ADAPTER.read(in);
         if (entry != null) list.add(entry);
       }
       in.endArray();
       ((QuestTaskMob) TASK).mobs = list.toArray(new QuestTaskMob.Mob[list.size()]);
     } else if (TASK instanceof QuestTaskReputation && name.equalsIgnoreCase(REPUTATION)) {
       List<QuestTaskReputation.ReputationSetting> list =
           new ArrayList<QuestTaskReputation.ReputationSetting>();
       in.beginArray();
       while (in.hasNext()) {
         list.add(REPUTATION_ADAPTER.read(in));
       }
       in.endArray();
       ReflectionHelper.setPrivateValue(
           QuestTaskReputation.class,
           (QuestTaskReputation) TASK,
           list.toArray(new QuestTaskReputation.ReputationSetting[list.size()]),
           "settings");
     } else if (name.equalsIgnoreCase(KILLS) && TASK instanceof QuestTaskReputationKill) {
       ((QuestTaskReputationKill) TASK).setKills(in.nextInt());
     }
   }
   in.endObject();
   type.addTaskData(QUEST);
   return null;
 }