public static String getJsonWithUpdatedResources(String jsonConf, Map<String, Double> resourceUpdates) { try { JSONParser parser = new JSONParser(); Object obj = parser.parse(jsonConf); JSONObject jsonObject = (JSONObject) obj; Map<String, Double> componentResourceMap = (Map<String, Double>) jsonObject.getOrDefault( Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, new HashMap<String, Double>() ); for (Map.Entry<String, Double> resourceUpdateEntry : resourceUpdates.entrySet()) { if (NormalizedResources.RESOURCE_NAME_NORMALIZER.getResourceNameMapping().containsValue(resourceUpdateEntry.getKey())) { // if there will be legacy values they will be in the outer conf jsonObject.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey())); componentResourceMap.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey())); } componentResourceMap.put(resourceUpdateEntry.getKey(), resourceUpdateEntry.getValue()); } jsonObject.put(Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, componentResourceMap); return jsonObject.toJSONString(); } catch (ParseException ex) { throw new RuntimeException("Failed to parse component resources with json: " + jsonConf); } }
JSONObject root = (JSONObject)parser.parse(message); String msgType = (String)root.getOrDefault("type", "");
public static Response parseFrom(long token, ByteBuffer buf) { if (Response.logger.isDebugEnabled()) { Response.logger.debug( "JSON Recv: Token: {} {}", token, Util.bufferToString(buf)); } JSONObject jsonResp = Util.toJSON(buf); ResponseType responseType = ResponseType.fromValue( ((Long) jsonResp.get("t")).intValue() ); ArrayList<Long> responseNoteVals = (ArrayList<Long>) jsonResp .getOrDefault("n", new ArrayList()); ArrayList<ResponseNote> responseNotes = responseNoteVals .stream() .map(Long::intValue) .map(ResponseNote::maybeFromValue) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toCollection(ArrayList::new)); Builder res = new Builder(token, responseType); if(jsonResp.containsKey("e")){ res.setErrorType(((Long)jsonResp.get("e")).intValue()); } return res.setNotes(responseNotes) .setProfile((JSONArray) jsonResp.getOrDefault("p", null)) .setBacktrace((JSONArray) jsonResp.getOrDefault("b", null)) .setData((JSONArray) jsonResp.getOrDefault("r", new JSONArray())) .build(); }