List of usage examples for com.google.gson JsonArray set
public JsonElement set(int index, JsonElement element)
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Sets the field.//from w ww .j av a 2 s. c o m * * @param field the field * @param path the path * @param value the value * @return the json element */ protected JsonElement setField(JsonElement field, JsonElement path, JsonElement value) { if (isNumber(path)) { JsonArray jArray = asJsonArray(field.getAsJsonArray(), new JsonArray()); int index = path.getAsInt(); repleteArray(jArray, index, JsonNull.class); jArray.set(index, value); field = jArray; } else { JsonObject jObject = asJsonObject(field, new JsonObject()); jObject.add(path.getAsString(), value); field = jObject; } return field; }
From source file:com.exalttech.trex.ui.views.streams.binders.BuilderDataBinding.java
License:Apache License
public String serializeAsPacketModel() { JsonObject model = new JsonObject(); model.add("protocols", new JsonArray()); JsonObject fieldEngine = new JsonObject(); fieldEngine.add("instructions", new JsonArray()); fieldEngine.add("global_parameters", new JsonObject()); model.add("field_engine", fieldEngine); Map<String, AddressDataBinding> l3Binds = new HashMap<>(); l3Binds.put("Ether", macDB); boolean isIPv4 = protocolSelection.getIpv4Property().get(); if (isIPv4) { l3Binds.put("IP", ipv4DB); }//from w ww . j av a2s . co m l3Binds.entrySet().stream().forEach(entry -> { JsonObject proto = new JsonObject(); String protoID = entry.getKey(); proto.add("id", new JsonPrimitive(protoID)); JsonArray fields = new JsonArray(); AddressDataBinding binding = entry.getValue(); String srcMode = entry.getValue().getDestination().getModeProperty().get(); String dstMode = entry.getValue().getSource().getModeProperty().get(); if (!MODE_TREX_CONFIG.equals(srcMode)) { fields.add(buildProtoField("src", binding.getSource().getAddressProperty().getValue())); } if (!MODE_TREX_CONFIG.equals(dstMode)) { fields.add(buildProtoField("dst", binding.getDestination().getAddressProperty().getValue())); } if (protoID.equals("Ether") && ethernetDB.getOverrideProperty().get()) { fields.add(buildProtoField("type", ethernetDB.getTypeProperty().getValue())); } proto.add("fields", fields); model.getAsJsonArray("protocols").add(proto); if (!MODE_FIXED.equals(binding.getSource().getModeProperty().get()) && !MODE_TREX_CONFIG.equals(binding.getSource().getModeProperty().get())) { fieldEngine.getAsJsonArray("instructions") .addAll(buildVMInstructions(protoID, "src", binding.getSource())); } if (!MODE_FIXED.equals(binding.getDestination().getModeProperty().get()) && !MODE_TREX_CONFIG.equals(binding.getDestination().getModeProperty().get())) { fieldEngine.getAsJsonArray("instructions") .addAll(buildVMInstructions(protoID, "dst", binding.getDestination())); } }); boolean isVLAN = protocolSelection.getTaggedVlanProperty().get(); String pktLenName = "pkt_len"; String frameLenghtType = protocolSelection.getFrameLengthType(); boolean pktSizeChanged = !frameLenghtType.equals("Fixed"); if (pktSizeChanged) { LinkedHashMap<String, String> instructionParam = new LinkedHashMap<>(); String operation = PacketBuilderHelper.getOperationFromType(frameLenghtType); Integer minLength = Integer.valueOf(protocolSelection.getMinLength()) - 4; Integer maxLength = Integer.valueOf(protocolSelection.getMaxLength()) - 4; instructionParam.put("init_value", minLength.toString()); instructionParam.put("max_value", maxLength.toString()); instructionParam.put("min_value", minLength.toString()); instructionParam.put("name", pktLenName); instructionParam.put("op", operation); instructionParam.put("size", "2"); instructionParam.put("step", "1"); fieldEngine.getAsJsonArray("instructions").add(buildInstruction("STLVmFlowVar", instructionParam)); instructionParam.clear(); instructionParam.put("fv_name", pktLenName); fieldEngine.getAsJsonArray("instructions").add(buildInstruction("STLVmTrimPktSize", instructionParam)); instructionParam.clear(); instructionParam.put("add_val", isVLAN ? "-18" : "-14"); instructionParam.put("is_big", "true"); instructionParam.put("fv_name", pktLenName); instructionParam.put("pkt_offset", isVLAN ? "20" : "16"); fieldEngine.getAsJsonArray("instructions").add(buildInstruction("STLVmWrFlowVar", instructionParam)); } if (isVLAN) { JsonObject dot1QProto = new JsonObject(); dot1QProto.add("id", new JsonPrimitive("Dot1Q")); Map<String, String> fieldsMap = new HashMap<>(); fieldsMap.put("prio", vlanDB.getPriorityProperty().getValue()); fieldsMap.put("id", vlanDB.getCfiProperty().getValue()); fieldsMap.put("vlan", vlanDB.getVIdProperty().getValue()); dot1QProto.add("fields", buildProtoFieldsFromMap(fieldsMap)); JsonArray protocols = model.getAsJsonArray("protocols"); if (protocols.size() == 2) { JsonElement ipv4 = protocols.get(1); protocols.set(1, dot1QProto); protocols.add(ipv4); } else { model.getAsJsonArray("protocols").add(dot1QProto); } if (vlanDB.getOverrideTPIdProperty().getValue()) { JsonArray etherFields = ((JsonObject) model.getAsJsonArray("protocols").get(0)).get("fields") .getAsJsonArray(); if (etherFields.size() == 3) { etherFields.remove(2); } etherFields.add(buildProtoField("type", vlanDB.getTpIdProperty().getValue())); } } boolean isTCP = protocolSelection.getTcpProperty().get(); if (isTCP) { JsonObject tcpProto = new JsonObject(); tcpProto.add("id", new JsonPrimitive("TCP")); Map<String, String> fieldsMap = new HashMap<>(); fieldsMap.put("sport", tcpProtocolDB.getSrcPortProperty().getValue()); fieldsMap.put("dport", tcpProtocolDB.getDstPortProperty().getValue()); fieldsMap.put("chksum", "0x" + tcpProtocolDB.getChecksumProperty().getValue()); fieldsMap.put("seq", tcpProtocolDB.getSequenceNumberProperty().getValue()); fieldsMap.put("urgptr", tcpProtocolDB.getUrgentPointerProperty().getValue()); fieldsMap.put("ack", tcpProtocolDB.getAckNumberProperty().getValue()); int tcp_flags = 0; if (tcpProtocolDB.getUrgProperty().get()) { tcp_flags = tcp_flags | (1 << 5); } if (tcpProtocolDB.getAckProperty().get()) { tcp_flags = tcp_flags | (1 << 4); } if (tcpProtocolDB.getPshProperty().get()) { tcp_flags = tcp_flags | (1 << 3); } if (tcpProtocolDB.getRstProperty().get()) { tcp_flags = tcp_flags | (1 << 2); } if (tcpProtocolDB.getSynProperty().get()) { tcp_flags = tcp_flags | (1 << 1); } if (tcpProtocolDB.getFinProperty().get()) { tcp_flags = tcp_flags | 1; } fieldsMap.put("flags", String.valueOf(tcp_flags)); tcpProto.add("fields", buildProtoFieldsFromMap(fieldsMap)); model.getAsJsonArray("protocols").add(tcpProto); } // Field Engine instructions String cache_size = "5000"; if ("Enable".equals(advancedPropertiesDB.getCacheSizeTypeProperty().getValue())) { cache_size = advancedPropertiesDB.getCacheValueProperty().getValue(); } fieldEngine.getAsJsonObject("global_parameters").add("cache_size", new JsonPrimitive(cache_size)); boolean isUDP = protocolSelection.getUdpProperty().get(); if (isUDP) { JsonObject udpProto = new JsonObject(); udpProto.add("id", new JsonPrimitive("UDP")); Map<String, String> fieldsMap = new HashMap<>(); fieldsMap.put("sport", udpProtocolDB.getSrcPortProperty().getValue()); fieldsMap.put("dport", udpProtocolDB.getDstPortProperty().getValue()); fieldsMap.put("len", udpProtocolDB.getLengthProperty().getValue()); udpProto.add("fields", buildProtoFieldsFromMap(fieldsMap)); model.getAsJsonArray("protocols").add(udpProto); if (pktSizeChanged) { LinkedHashMap<String, String> instructionParam = new LinkedHashMap<>(); instructionParam.put("add_val", isVLAN ? "-38" : "-34"); instructionParam.put("is_big", "true"); instructionParam.put("fv_name", pktLenName); instructionParam.put("pkt_offset", isVLAN ? "42" : "38"); fieldEngine.getAsJsonArray("instructions") .add(buildInstruction("STLVmWrFlowVar", instructionParam)); } } if (ipv4DB.hasInstructions() || pktSizeChanged) { Map<String, String> flowWrVarParameters = new HashMap<>(); flowWrVarParameters.put("offset", "IP"); fieldEngine.getAsJsonArray("instructions").add(buildInstruction("STLVmFixIpv4", flowWrVarParameters)); } return model.toString(); }
From source file:com.flipkart.android.proteus.toolbox.Utils.java
License:Apache License
public static JsonElement merge(JsonElement oldJson, JsonElement newJson, boolean useCopy, Gson gson) { JsonElement newDataElement;//from w w w.j a va 2 s.c o m JsonArray oldArray; JsonArray newArray; JsonElement oldArrayItem; JsonElement newArrayItem; JsonObject oldObject; if (oldJson == null || oldJson.isJsonNull()) { return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson; } if (newJson == null || newJson.isJsonNull()) { newJson = JsonNull.INSTANCE; return newJson; } if (newJson.isJsonPrimitive()) { JsonPrimitive value; if (!useCopy) { return newJson; } if (newJson.getAsJsonPrimitive().isBoolean()) { value = new JsonPrimitive(newJson.getAsBoolean()); } else if (newJson.getAsJsonPrimitive().isNumber()) { value = new JsonPrimitive(newJson.getAsNumber()); } else if (newJson.getAsJsonPrimitive().isString()) { value = new JsonPrimitive(newJson.getAsString()); } else { value = newJson.getAsJsonPrimitive(); } return value; } if (newJson.isJsonArray()) { if (!oldJson.isJsonArray()) { return useCopy ? gson.fromJson(newJson, JsonArray.class) : newJson; } else { oldArray = oldJson.getAsJsonArray(); newArray = newJson.getAsJsonArray(); if (oldArray.size() > newArray.size()) { while (oldArray.size() > newArray.size()) { oldArray.remove(oldArray.size() - 1); } } for (int index = 0; index < newArray.size(); index++) { if (index < oldArray.size()) { oldArrayItem = oldArray.get(index); newArrayItem = newArray.get(index); oldArray.set(index, merge(oldArrayItem, newArrayItem, useCopy, gson)); } else { oldArray.add(newArray.get(index)); } } } } else if (newJson.isJsonObject()) { if (!oldJson.isJsonObject()) { return useCopy ? gson.fromJson(newJson, JsonObject.class) : newJson; } else { oldObject = oldJson.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : newJson.getAsJsonObject().entrySet()) { newDataElement = merge(oldObject.get(entry.getKey()), entry.getValue(), useCopy, gson); oldObject.add(entry.getKey(), newDataElement); } } } else { return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson; } return oldJson; }
From source file:com.ibm.g11n.pipeline.resfilter.impl.JsonResource.java
License:Open Source License
@Override public void write(OutputStream outStream, LanguageBundle languageBundle, FilterOptions options) throws IOException, ResourceFilterException { // extracts key value pairs in original sequence order List<ResourceString> resStrings = languageBundle.getSortedResourceStrings(); JsonObject output = new JsonObject(); JsonObject top_level;/*from w w w . j a v a 2 s . com*/ if (this instanceof GlobalizeJsResource) { String resLanguageCode = languageBundle.getEmbeddedLanguageCode(); if (resLanguageCode == null || resLanguageCode.isEmpty()) { throw new ResourceFilterException( "Missing resource language code in the specified language bundle."); } top_level = new JsonObject(); top_level.add(resLanguageCode, output); } else { top_level = output; } for (ResourceString res : resStrings) { String key = res.getKey(); List<KeyPiece> keyPieces = splitKeyPieces(key); JsonElement current = output; for (int i = 0; i < keyPieces.size(); i++) { if (i + 1 < keyPieces.size()) { // There is structure under this // key piece if (current.isJsonObject()) { JsonObject currentObject = current.getAsJsonObject(); if (!currentObject.has(keyPieces.get(i).keyValue)) { if (keyPieces.get(i + 1).keyType == JsonToken.BEGIN_ARRAY) { currentObject.add(keyPieces.get(i).keyValue, new JsonArray()); } else { currentObject.add(keyPieces.get(i).keyValue, new JsonObject()); } } current = currentObject.get(keyPieces.get(i).keyValue); } else { JsonArray currentArray = current.getAsJsonArray(); Integer idx = Integer.valueOf(keyPieces.get(i).keyValue); for (int arrayIndex = currentArray.size(); arrayIndex <= idx; arrayIndex++) { currentArray.add(JsonNull.INSTANCE); } if (currentArray.get(idx).isJsonNull()) { if (keyPieces.get(i + 1).keyType == JsonToken.BEGIN_ARRAY) { currentArray.set(idx, new JsonArray()); } else { currentArray.set(idx, new JsonObject()); } } current = currentArray.get(idx); } } else { // This is the leaf node if (keyPieces.get(i).keyType == JsonToken.BEGIN_ARRAY) { JsonArray currentArray = current.getAsJsonArray(); Integer idx = Integer.valueOf(keyPieces.get(i).keyValue); JsonPrimitive e = new JsonPrimitive(res.getValue()); for (int arrayIndex = currentArray.size(); arrayIndex <= idx; arrayIndex++) { currentArray.add(JsonNull.INSTANCE); } current.getAsJsonArray().set(idx, e); } else { current.getAsJsonObject().addProperty(keyPieces.get(i).keyValue, res.getValue()); } } } } try (OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(outStream), StandardCharsets.UTF_8)) { new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(top_level, writer); } }
From source file:com.ibm.streamsx.topology.builder.BOperatorInvocation.java
License:Open Source License
@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.values()) oa.set(output.index(), output._complete()); json.add("outputs", oa); }/*from www. j a v a 2s.c o m*/ 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; }
From source file:com.ibm.streamsx.topology.generator.spl.GraphUtilities.java
License:Open Source License
static void insertOperatorBetweenPorts(JsonObject input, JsonObject output, JsonObject op) { String oportName = jstring(output, "name"); String iportName = jstring(input, "name"); JsonObject opInput = op.get("inputs").getAsJsonArray().get(0).getAsJsonObject(); JsonObject opOutput = op.get("outputs").getAsJsonArray().get(0).getAsJsonObject(); String opIportName = jstring(opInput, "name"); String opOportName = jstring(opOutput, "name"); // Attach op in inputs and outputs JsonArray opInputConns = opInput.get("connections").getAsJsonArray(); JsonArray opOutputConns = opOutput.get("connections").getAsJsonArray(); boolean add = true; for (JsonElement conn : opInputConns) if (conn.getAsString().equals(oportName)) { add = false;/*w w w . j a va2 s. c o m*/ break; } if (add) opInputConns.add(new JsonPrimitive(oportName)); add = true; for (JsonElement conn : opOutputConns) if (conn.getAsString().equals(iportName)) { add = false; break; } opOutputConns.add(new JsonPrimitive(iportName)); JsonArray outputConns = output.get("connections").getAsJsonArray(); JsonArray inputConns = input.get("connections").getAsJsonArray(); for (int i = 0; i < outputConns.size(); i++) { if (outputConns.get(i).getAsString().equals(iportName)) outputConns.set(i, new JsonPrimitive(opIportName)); } for (int i = 0; i < inputConns.size(); i++) { if (inputConns.get(i).getAsString().equals(oportName)) inputConns.set(i, new JsonPrimitive(opOportName)); } }
From source file:com.ibm.streamsx.topology.generator.spl.SPLGenerator.java
License:Open Source License
/** * When we create a composite, operators need to create connections with the composite's input port. * @param graph//ww w . ja v a 2 s .c o m * @param startsEndsAndOperators * @param opDefinition */ 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)); } } } } } }
From source file:com.jayway.jsonpath.internal.spi.json.GsonJsonProvider.java
License:Apache License
@Override public void setProperty(Object obj, Object key, Object value) { if (isMap(obj)) toJsonObject(obj).add(key.toString(), createJsonElement(value)); else {//from w w w . jav a 2 s .co m JsonArray array = toJsonArray(obj); int index; if (key != null) { index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString()); } else { index = array.size(); } if (index == array.size()) { array.add(createJsonElement(value)); } else { array.set(index, createJsonElement(value)); } } }
From source file:com.jayway.jsonpath.spi.json.GsonJsonProvider.java
License:Apache License
@Override public void setArrayIndex(final Object array, final int index, final Object newValue) { if (!isArray(array)) { throw new UnsupportedOperationException(); } else {//from w w w.j a va 2 s. com JsonArray arr = toJsonArray(array); if (index == arr.size()) { arr.add(createJsonElement(newValue)); } else { arr.set(index, createJsonElement(newValue)); } } }
From source file:com.jayway.jsonpath.spi.json.GsonJsonProvider.java
License:Apache License
@Override public void setProperty(final Object obj, final Object key, final Object value) { if (isMap(obj)) { toJsonObject(obj).add(key.toString(), createJsonElement(value)); } else {/*from w ww .j a v a2 s . co m*/ JsonArray array = toJsonArray(obj); int index; if (key != null) { index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString()); } else { index = array.size(); } if (index == array.size()) { array.add(createJsonElement(value)); } else { array.set(index, createJsonElement(value)); } } }