Java Code Examples for com.google.gson.JsonArray#set()

The following examples show how to use com.google.gson.JsonArray#set() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public void addCard(MagicEdition me, MagicCard mc) throws IOException {
	File f = new File(setDirectory, me.getId() + ext);
	JsonObject root = FileTools.readJson(f).getAsJsonObject();
	JsonArray cards = root.get(CARDS).getAsJsonArray();

	int index = indexOf(mc, cards);

	if (index > -1) {
		cards.set(index, new Gson().toJsonTree(mc));
	} else {
		cards.add(new Gson().toJsonTree(mc));
		me.setCardCount(me.getCardCount() + 1);
		root.addProperty("cardCount", me.getCardCount());
	}
	FileTools.saveFile(f, root.toString());
}
 
Example 2
Source Project: che   File: GsonJsonRpcComposer.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private <T> List<T> composeMany(Class<T> type, List<?> paramsList) {
  if (paramsList.isEmpty()) {
    return emptyList();
  }

  if (paramsList.get(0) instanceof JsonElement) {
    JsonArray jsonArray = new JsonArray();
    for (int i = 0; i < paramsList.size(); i++) {
      JsonElement jsonElement = (JsonElement) paramsList.get(i);
      jsonArray.set(i, jsonElement);
    }
    return DtoFactory.getInstance().createListDtoFromJson(jsonArray.toString(), type);
  }

  return cast(paramsList);
}
 
Example 3
@Override
public JsonObject _complete() {
    final JsonObject json = super._complete();

    if (outputs != null) {
        JsonArray oa = new JsonArray();
        // outputs array in java is in port order.
        for (int i = 0; i < outputs.size(); i++)
            oa.add(JsonNull.INSTANCE); // will be overwritten with port info
        for (BOutputPort output : outputs)
            oa.set(output.index(), output._complete());
        json.add("outputs", oa);
    }

    if (inputs != null) {
        JsonArray ia = new JsonArray();
        for (int i = 0; i < inputs.size(); i++)
            ia.add(JsonNull.INSTANCE); // will be overwritten with port info
        for (BInputPort input : inputs)
            ia.set(input.index(), input._complete());
        json.add("inputs", ia);
    }

    return json;
}
 
Example 4
Source Project: js-dossier   File: JsonEncoder.java    License: Apache License 2.0 6 votes vote down vote up
/** Encode the given message as a JSON array. */
public JsonArray encode(Message message) {
  JsonArray array = new JsonArray();
  for (FieldDescriptor field : message.getDescriptorForType().getFields()) {
    if (field.isRepeated() || message.hasField(field)) {
      JsonElement element = encodeField(field, message.getField(field));
      if (!element.isJsonNull()) {
        while (array.size() < field.getNumber()) {
          array.add(JsonNull.INSTANCE);
        }
        array.set(field.getNumber() - 1, element);
      }
    }
  }
  return array;
}
 
Example 5
public static void reduce_value(boolean is_friend, JsonArray target, int idx, int amount, boolean cb_flag) {
    if (idx >= 0 && idx < target.size()) {
        int before_value = target.get(idx).getAsInt();
        int after_value = before_value - amount;
        if (is_friend && after_value <= 0) after_value = damecon_calculate(idx, after_value, cb_flag);
        target.set(idx, new JsonPrimitive(after_value));
    }
}
 
Example 6
public static void reduce_value(boolean is_friend, JsonArray target, int idx, int amount, boolean cb_flag) {
    if (idx >= 0 && idx < target.size()) {
        int before_value = target.get(idx).getAsInt();
        int after_value = before_value - amount;
        if (is_friend && after_value <= 0) after_value = damecon_calculate(idx, after_value, cb_flag);
        target.set(idx, new JsonPrimitive(after_value));
    }
}
 
Example 7
private void modifyUserListChildren(JsonArray children) {
    for(int i = 0 ; i < children.size() ; i++){
        JsonElement child = children.get(i);
        if(child.isJsonObject()){
            JsonObject obj = new JsonObject();
            obj.add(KIND, new JsonPrimitive(RedditType.User.toString()));
            obj.add(DATA, child);
            children.set(i, obj);
        }
    }
}
 
Example 8
Source Project: streamsx.topology   File: SPLGenerator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * When we create a composite, operators need to create connections with the composite's input port.
 * @param graph
 * @param startsEndsAndOperators
 * @param opDefinition
 */
@SuppressWarnings("unused")
private void fixCompositeInputNaming(JsonObject graph, List<List<JsonObject>> startsEndsAndOperators,
        JsonObject opDefinition) {   
    // For each start
    // We iterate like this because we need to also index into the operatorDefinition's inputNames list.
    for(int i = 0; i <  startsEndsAndOperators.get(0).size(); i++){
        JsonObject start = startsEndsAndOperators.get(0).get(i);
        
        //If the start is a source, the name doesn't need fixing.
        // Only input ports that now connect to the Composite input need to be fixed.
        if(start.has("config") && (hasAny(object(start, "config"), compOperatorStarts)))
            continue;
        
        // Given its output port name
        // Region markers like $Parallel$ only have one input and output
        String outputPortName = GsonUtilities.jstring(start.get("outputs").getAsJsonArray().get(0).getAsJsonObject(), "name");
        
        // for each operator downstream from this start
        for(JsonObject downstream : GraphUtilities.getDownstream(start, graph)){
            // for each input in the downstream operator
            JsonArray inputs = array(downstream, "inputs");
            for(JsonElement inputObj : inputs){
                JsonObject input = inputObj.getAsJsonObject();
                // for each connection in that input
                JsonArray connections = array(input, "connections");
                for(int j = 0; j < connections.size(); j++){
                    
                    // Replace the connection with the composite input port name if the 
                    // port has a connection to the start operator. 
                    if(connections.get(j).getAsString().equals(outputPortName)){
                        connections.set(j, GsonUtilities.array(opDefinition, "inputNames").get(i));
                    }
                }
            }        
        }          
    }
    
    
}