Beispiel #1
1
    private SegmentItem deserializeItem(JsonObject json, JsonDeserializationContext context) {
      if (!json.has("actions")) {
        throw new ProtectionParseException("Missing actions identifier");
      }

      SegmentItem segment = new SegmentItem();

      segment.types.addAll(
          deserializeAsArray(
              json.get("actions"),
              context,
              new TypeToken<ItemType>() {},
              new TypeToken<List<ItemType>>() {}.getType()));
      json.remove("actions");

      if (json.has("isAdjacent")) {
        segment.isAdjacent = json.get("isAdjacent").getAsBoolean();
        json.remove("isAdjacent");
      }

      if (json.has("clientUpdate")) {
        JsonObject jsonClientUpdate = json.get("clientUpdate").getAsJsonObject();
        segment.clientUpdate =
            new ClientBlockUpdate(
                (Volume) context.deserialize(jsonClientUpdate.get("coords"), Volume.class));
        if (jsonClientUpdate.has("directional")) {
          segment.directionalClientUpdate = jsonClientUpdate.get("directional").getAsBoolean();
        }
        json.remove("clientUpdate");
      }

      return segment;
    }
  /**
   * Deserializes the given element.
   *
   * @param element The JSON element to deserialize.
   * @param type The type to deserialize into.
   * @param context The context.
   * @return The deserialized object.
   * @throws JsonParseException
   */
  public Movie deserialize(JsonElement element, Type type, JsonDeserializationContext context)
      throws JsonParseException {
    JsonObject object = element.getAsJsonObject();

    ISerializer serializer = getSerializer();

    Movie movie = new Movie();

    element = object.get("id");
    movie.setID(element == null ? "" : element.getAsString());

    element = object.get("name");
    movie.setName(element == null ? "" : element.getAsString());

    element = object.get("year");
    movie.setYear(element == null ? 0 : element.getAsInt());

    element = object.get("synopsis");
    movie.setSynopsis(element == null ? "" : element.getAsString());

    element = object.get("genres");
    for (JsonElement current : element.getAsJsonArray())
      movie.getGenres().add(context.<Genre>deserialize(current, Genre.class));

    element = object.get("addDate");
    movie.setAddDate(
        element == null
            ? new Date(1970, 1, 1)
            : serializer.deserialize(element.getAsString(), Date.class));

    element = object.get("lastWatched");
    movie.setLastWatched(
        element == null ? new User() : context.<User>deserialize(element, User.class));

    element = object.get("lastWatchedDate");
    movie.setLastWatchedDate(new Date(1970, 1, 1));
    if (!element.isJsonNull())
      movie.setLastWatchedDate(serializer.deserialize(element.getAsString(), Date.class));

    element = object.get("poster");
    movie.setPoster(element == null ? "" : element.getAsString());

    element = object.get("director");
    movie.setDirector(element == null ? "" : element.getAsString());

    element = object.get("actors");
    for (JsonElement current : element.getAsJsonArray())
      movie.getActors().add(current.getAsString());

    element = object.get("file");
    movie.setFile(element == null ? "" : element.getAsString());

    element = object.get("encoded");
    movie.isEncoded(element != null && element.getAsBoolean());

    element = object.get("url");
    movie.setUrl(element == null ? "" : element.getAsString());

    return movie;
  }
Beispiel #3
0
 private <T> List<T> deserializeAsArray(
     JsonElement json,
     JsonDeserializationContext context,
     TypeToken<T> typeToken,
     Type listOfT) {
   if (json.isJsonPrimitive()) {
     List<T> list = new ArrayList<T>();
     list.add((T) context.deserialize(json, typeToken.getType()));
     return list;
   } else {
     return context.deserialize(json, listOfT);
   }
 }
Beispiel #4
0
    private SegmentBlock deserializeBlock(JsonObject json, JsonDeserializationContext context) {
      if (!json.has("actions")) {
        throw new ProtectionParseException("Missing actions identifier");
      }
      SegmentBlock segment = new SegmentBlock();
      segment.types.addAll(
          deserializeAsArray(
              json.get("actions"),
              context,
              new TypeToken<BlockType>() {},
              new TypeToken<List<BlockType>>() {}.getType()));
      json.remove("actions");

      if (json.has("meta")) {
        segment.meta = json.get("meta").getAsInt();
        json.remove("meta");
      }

      if (json.has("clientUpdate")) {
        segment.clientUpdate =
            new ClientBlockUpdate(
                (Volume)
                    context.deserialize(
                        json.get("clientUpdate").getAsJsonObject().get("coords"), Volume.class));
        json.remove("clientUpdate");
      }

      return segment;
    }
 @Override
 public TargetInfo deserialize(
     JsonElement element, Type type, final JsonDeserializationContext context)
     throws JsonParseException {
   final JsonObject object = element.getAsJsonObject();
   final List<String> targets = getStringListField(object, "targets");
   final List<String> libraries = getStringListField(object, "libraries");
   final List<String> excludes = getStringListField(object, "excludes");
   final List<SourceRoot> sourceRoots =
       getFieldAsList(
           object.getAsJsonArray("roots"),
           new Function<JsonElement, SourceRoot>() {
             @Override
             public SourceRoot fun(JsonElement element) {
               return context.deserialize(element, SourceRoot.class);
             }
           });
   final TargetAddressInfo addressInfo = context.deserialize(element, TargetAddressInfo.class);
   return new TargetInfo(
       new HashSet<TargetAddressInfo>(Collections.singleton(addressInfo)),
       new HashSet<String>(targets),
       new HashSet<String>(libraries),
       new HashSet<String>(excludes),
       new HashSet<SourceRoot>(sourceRoots));
 }
 @Override
 public AssetReference deserialize(JsonElement j, Type typeOfT, JsonDeserializationContext context)
     throws JsonParseException {
   JsonObject json = j.getAsJsonObject().get("asset").getAsJsonObject();
   AssetReference asset = new AssetReference();
   asset.asset = context.deserialize(json, GsonUtils.readClassProperty(json, context));
   return asset;
 }
Beispiel #7
0
 public T deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context)
     throws JsonParseException {
   final JsonObject wrapper = (JsonObject) elem;
   final JsonElement typeName = get(wrapper, "type");
   final JsonElement data = get(wrapper, "data");
   final Type actualType = typeForName(typeName);
   return context.deserialize(data, actualType);
 }
Beispiel #8
0
 @Override
 public PartitionMethodDescExpr deserialize(
     JsonElement json, Type typeOfT, JsonDeserializationContext context)
     throws JsonParseException {
   JsonObject jsonObject = json.getAsJsonObject();
   PartitionType type =
       PartitionType.valueOf(
           CommonGsonHelper.getOrDie(jsonObject, "PartitionType").getAsString());
   switch (type) {
     case RANGE:
       return context.deserialize(json, RangePartition.class);
     case HASH:
       return context.deserialize(json, HashPartition.class);
     case LIST:
       return context.deserialize(json, ListPartition.class);
     case COLUMN:
       return context.deserialize(json, ColumnPartition.class);
   }
   return null;
 }
  @Override
  public RefObject deserialize(
      JsonElement jsonElement, Type type, JsonDeserializationContext context)
      throws JsonParseException {
    if (jsonElement.isJsonPrimitive()) {
      RefObject refObject = new RefObject();
      refObject.setValue(jsonElement.getAsString());
      return refObject;
    }

    return context.deserialize(jsonElement, JsonRefObject.class);
  }
  @Override
  public FriendList deserialize(JsonElement je, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException {
    FriendList friendList = new FriendList();

    JsonObject response = (JsonObject) je.getAsJsonObject().get("response");
    JsonArray items = response.getAsJsonObject().getAsJsonArray("items");

    for (int i = 0; i < items.size(); i++) {
      Friend friend = context.deserialize(items.get(i), Friend.class);
      friendList.getFriends().add(friend);
    }
    return friendList;
  }
  @Override
  public MoveStrategy deserialize(
      JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String type = jsonObject.get("type").getAsString();
    JsonElement element = jsonObject.get("properties");

    try {
      String thepackage = "bots.";
      return context.deserialize(element, Class.forName(thepackage + type));
    } catch (ClassNotFoundException cnfe) {
      throw new JsonParseException("Unknown element type: " + type, cnfe);
    }
  }
  @Override
  public SageInteract deserialize(
      JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException {

    JsonObject jsonObject = json.getAsJsonObject();
    JsonElement control = jsonObject.get(KEY_CONTROLS);
    varNames = new ArrayList<String>();
    controls = new ArrayList<InteractControl>();
    // Here we modify the model slightly and add the varName indirectly to our InteractControl
    // by obtaining it from the list of keys from the JSON and passing the

    // This way, we can continue to use Gson for the normal deserialization of
    // our InteractControl class

    // Get all the possible keys for InteractControl

    Log.i(TAG, "Got Control" + control.toString());

    Log.i(TAG, "No. of controls " + control.getAsJsonObject().entrySet().size());

    // Iterate through keys and deserialize
    for (Map.Entry<String, JsonElement> keys : control.getAsJsonObject().entrySet()) {
      varNames.add(keys.getKey());
      Log.i(TAG, "Got Key: " + keys.getKey());
      InteractControl interactControl = context.deserialize(keys.getValue(), InteractControl.class);
      Log.i(TAG, "Deserializing: " + interactControl.toString());
      interactControl.setVarName(keys.getKey());
      controls.add(interactControl);
    }

    // TODO Find way to add the omitted data
    final SageInteract interact = new SageInteract();
    interact.setControls(controls);
    interact.setNewInteractID(jsonObject.getAsJsonPrimitive(KEY_NEW_INTERACT_ID).getAsString());

    return interact;
  }
Beispiel #13
0
    public IChatComponent deserialize(
        JsonElement p_deserialize_1_,
        Type p_deserialize_2_,
        JsonDeserializationContext p_deserialize_3_) {
      if (p_deserialize_1_.isJsonPrimitive()) {
        return new ChatComponentText(p_deserialize_1_.getAsString());
      } else if (!p_deserialize_1_.isJsonObject()) {
        if (p_deserialize_1_.isJsonArray()) {
          JsonArray var11 = p_deserialize_1_.getAsJsonArray();
          IChatComponent var12 = null;
          Iterator var15 = var11.iterator();

          while (var15.hasNext()) {
            JsonElement var17 = (JsonElement) var15.next();
            IChatComponent var18 = this.deserialize(var17, var17.getClass(), p_deserialize_3_);

            if (var12 == null) {
              var12 = var18;
            } else {
              var12.appendSibling(var18);
            }
          }

          return var12;
        } else {
          throw new JsonParseException(
              "Don\'t know how to turn " + p_deserialize_1_.toString() + " into a Component");
        }
      } else {
        JsonObject var4 = p_deserialize_1_.getAsJsonObject();
        Object var5;

        if (var4.has("text")) {
          var5 = new ChatComponentText(var4.get("text").getAsString());
        } else if (var4.has("translate")) {
          String var6 = var4.get("translate").getAsString();

          if (var4.has("with")) {
            JsonArray var7 = var4.getAsJsonArray("with");
            Object[] var8 = new Object[var7.size()];

            for (int var9 = 0; var9 < var8.length; ++var9) {
              var8[var9] = this.deserialize(var7.get(var9), p_deserialize_2_, p_deserialize_3_);

              if (var8[var9] instanceof ChatComponentText) {
                ChatComponentText var10 = (ChatComponentText) var8[var9];

                if (var10.getChatStyle().isEmpty() && var10.getSiblings().isEmpty()) {
                  var8[var9] = var10.getChatComponentText_TextValue();
                }
              }
            }

            var5 = new ChatComponentTranslation(var6, var8);
          } else {
            var5 = new ChatComponentTranslation(var6, new Object[0]);
          }
        } else if (var4.has("score")) {
          JsonObject var13 = var4.getAsJsonObject("score");

          if (!var13.has("name") || !var13.has("objective")) {
            throw new JsonParseException("A score component needs a least a name and an objective");
          }

          var5 =
              new ChatComponentScore(
                  JsonUtils.getJsonObjectStringFieldValue(var13, "name"),
                  JsonUtils.getJsonObjectStringFieldValue(var13, "objective"));

          if (var13.has("value")) {
            ((ChatComponentScore) var5)
                .func_179997_b(JsonUtils.getJsonObjectStringFieldValue(var13, "value"));
          }
        } else {
          if (!var4.has("selector")) {
            throw new JsonParseException(
                "Don\'t know how to turn " + p_deserialize_1_.toString() + " into a Component");
          }

          var5 =
              new ChatComponentSelector(JsonUtils.getJsonObjectStringFieldValue(var4, "selector"));
        }

        if (var4.has("extra")) {
          JsonArray var14 = var4.getAsJsonArray("extra");

          if (var14.size() <= 0) {
            throw new JsonParseException("Unexpected empty array of components");
          }

          for (int var16 = 0; var16 < var14.size(); ++var16) {
            ((IChatComponent) var5)
                .appendSibling(
                    this.deserialize(var14.get(var16), p_deserialize_2_, p_deserialize_3_));
          }
        }

        ((IChatComponent) var5)
            .setChatStyle(
                (ChatStyle) p_deserialize_3_.deserialize(p_deserialize_1_, ChatStyle.class));
        return (IChatComponent) var5;
      }
    }
Beispiel #14
0
    @Override
    public Segment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      if (!json.getAsJsonObject().has("class")) {
        throw new ProtectionParseException("One of the segments is missing a class identifier");
      }

      JsonObject jsonObject = json.getAsJsonObject();
      String classString = jsonObject.get("class").getAsString();

      if (!json.getAsJsonObject().has("type")) {
        throw new ProtectionParseException("Segment for " + classString + " is missing a type");
      }
      String type = jsonObject.get("type").getAsString();
      jsonObject.remove("type");

      Segment segment = null;
      if ("specialBlock".equals(type)) {
        segment = deserializeSpecialBlock(jsonObject, context);
      } else if ("block".equals(type)) {
        segment = deserializeBlock(jsonObject, context);
      } else if ("entity".equals(type)) {
        segment = deserializeEntity(jsonObject, context);
      } else if ("item".equals(type)) {
        segment = deserializeItem(jsonObject, context);
      } else if ("tileEntity".equals(type)) {
        segment = deserializeTileEntity(jsonObject, context);
      }

      if (segment == null) {
        throw new ProtectionParseException("Identifier type is invalid");
      }

      try {
        segment.checkClass = Class.forName(classString);
      } catch (ClassNotFoundException ex) {
        // throw new ProtectionParseException("Invalid class identifier: " + classString);
        MyTown.instance.LOG.error(
            "Invalid class identifier {" + classString + "}: >>> Segment Rejected <<<");
        return null;
      }
      jsonObject.remove("class");

      if (!(segment instanceof SegmentSpecialBlock)) {
        if (!json.getAsJsonObject().has("flags")) {
          throw new ProtectionParseException("Segment for " + classString + " is missing flags");
        }
        segment.flags.addAll(
            deserializeAsArray(
                jsonObject.get("flags"),
                context,
                new TypeToken<FlagType<Boolean>>() {},
                new TypeToken<List<FlagType<Boolean>>>() {}.getType()));
        jsonObject.remove("flags");

        if (jsonObject.has("condition")) {
          segment.condition = new Condition(jsonObject.get("condition").getAsString());
          jsonObject.remove("condition");
        }

        if (jsonObject.has("priority")) {
          segment.priority = Priority.valueOf(jsonObject.get("priority").getAsString());
          jsonObject.remove("priority");
        }

        for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
          Getter getter = context.deserialize(entry.getValue(), Getter.class);
          getter.setName(entry.getKey());
          segment.getters.add(getter);
        }
      }

      return segment;
    }