@Override public boolean hasNext() { return index < jsonArray.length(); }
@Override public int length(Object obj) { if( obj instanceof org.codehaus.jettison.json.JSONArray ) { return ((org.codehaus.jettison.json.JSONArray) obj).length(); } if( obj instanceof org.codehaus.jettison.json.JSONObject ) { return ((org.codehaus.jettison.json.JSONObject)obj).length(); } if( obj instanceof String ) { return ((String)obj).length(); } return 0; }
private Map<Integer, JSONObject> listSessions() throws IOException { String sessionsUrl = livyUrl + "/sessions"; int numSessions; JSONObject sessionsInfo; Map<Integer, JSONObject> sessionsMap = new HashMap<>(); Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", APPLICATION_JSON); headers.put("X-Requested-By", USER); try { sessionsInfo = readJSONFromUrl(sessionsUrl, headers); numSessions = sessionsInfo.getJSONArray("sessions").length(); for (int i = 0; i < numSessions; i++) { int currentSessionId = sessionsInfo.getJSONArray("sessions").getJSONObject(i).getInt("id"); JSONObject currentSession = sessionsInfo.getJSONArray("sessions").getJSONObject(i); sessionsMap.put(currentSessionId, currentSession); } } catch (JSONException e) { throw new IOException(e); } return sessionsMap; }
@Override public boolean hasNext() { return index < jsonArray.length(); }
/** * Get the optional object value associated with an index. * @param index The index must be between 0 and length() - 1. * @return An object value, or null if there is no * object at that index. */ public Object opt(int index) { return (index < 0 || index >= length()) ? null : this.myArrayList.get(index); }
if (offers.length() > 1) { final JSONObject aggregateOffer = new JSONObject(); aggregateOffer.put("@type", "AggregateOffer"); aggregateOffer.put("lowPrice", lowPrice.doubleValue()); aggregateOffer.put("priceCurrency", currency); aggregateOffer.put("offerCount", offers.length()); aggregateOffer.put("offers", offers);
@Override public int length(Object obj) { if( obj instanceof org.codehaus.jettison.json.JSONArray ) { return ((org.codehaus.jettison.json.JSONArray) obj).length(); } if( obj instanceof org.codehaus.jettison.json.JSONObject ) { return ((org.codehaus.jettison.json.JSONObject)obj).length(); } if( obj instanceof String ) { return ((String)obj).length(); } return 0; }
/** * Make a string from the contents of this JSONArray. The * <code>separator</code> string is inserted between each element. * Warning: This method assumes that the data structure is acyclical. * @param separator A string that will be inserted between the elements. * @return a string. * @throws JSONException If the array contains an invalid number. */ public String join(String separator) throws JSONException { int len = length(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i += 1) { if (i > 0) { sb.append(separator); } sb.append(JSONObject.valueToString(this.myArrayList.get(i), escapeForwardSlashAlways)); } return sb.toString(); }
@SuppressWarnings("unchecked") static <T> Collection<T> asCollection(String jsonArrayVal) throws JSONException { if (null == jsonArrayVal) { return null; } Collection<T> result = new LinkedList<T>(); JSONArray arrayVal = new JSONArray(jsonArrayVal); for (int i = 0; i < arrayVal.length(); i++) { result.add((T)arrayVal.get(i)); } return result; }
/** * Produce a JSONObject by combining a JSONArray of names with the values * of this JSONArray. * @param names A JSONArray containing a list of key strings. These will be * paired with the values. * @return A JSONObject, or null if there are no names or if this JSONArray * has no values. * @throws JSONException If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.length() == 0 || length() == 0) { return null; } JSONObject jo = new JSONObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
@SuppressWarnings("unused") private void readAttribute( Node n, String k, JSONArray array ) throws JSONException { for (int i = 0; i < array.length(); i++) { readAttribute( n, k, array.getString( i ) ); } }
/** * Produce a JSONArray containing the names of the elements of this * JSONObject. * @return A JSONArray containing the key strings, or null if the JSONObject * is empty. */ public JSONArray names() { JSONArray ja = new JSONArray(); Iterator keys = keys(); while (keys.hasNext()) { ja.put(keys.next()); } return ja.length() == 0 ? null : ja; }
/** * Produce a JSONArray containing the values of the members of this * JSONObject. * @param names A JSONArray containing a list of key strings. This * determines the sequence of the values in the result. * @return A JSONArray of values. * @throws JSONException If any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; }
/** * Put or replace an object value in the JSONArray. If the index is greater * than the length of the JSONArray, then null elements will be added as * necessary to pad it out. * @param index The subscript. * @param value The value to put into the array. The value should be a * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the * JSONObject.NULL object. * @return this. * @throws JSONException If the index is negative or if the the value is * an invalid number. */ public JSONArray put(int index, Object value) throws JSONException { JSONObject.testValidity(value); if (index < 0) { throw new JSONException("JSONArray[" + index + "] not found."); } if (index < length()) { this.myArrayList.set(index, value); } else { while (index != length()) { put(JSONObject.NULL); } put(value); } return this; }
public MappedXMLStreamReader(JSONObject obj, MappedNamespaceConvention con) throws JSONException, XMLStreamException { String rootName = (String) obj.keys().next(); this.convention = con; this.nodes = new FastStack(); this.ctx = con; Object top = obj.get(rootName); if (top instanceof JSONObject) { this.node = new Node(null, rootName, (JSONObject)top, convention); } else if (top instanceof JSONArray && !(((JSONArray)top).length() == 1 && ((JSONArray)top).get(0).equals(""))) { this.node = new Node(null, rootName, obj, convention); } else { node = new Node(rootName, convention); convention.processAttributesAndNamespaces(node, obj); currentValue = JSONObject.NULL.equals(top) ? null : top.toString(); } nodes.push(node); event = START_DOCUMENT; }
public Set<String> getSubmittedYarnApps() throws Exception { String rmHost = PhoenixMRJobUtil.getActiveResourceManagerHost(conf, zkQuorum); Map<String, String> urlParams = new HashMap<String, String>(); urlParams.put(YarnApplication.APP_STATES_ELEMENT, YarnApplication.state.NEW.toString() + "," + YarnApplication.state.ACCEPTED + "," + YarnApplication.state.SUBMITTED + "," + YarnApplication.state.RUNNING); int rmPort = PhoenixMRJobUtil.getRMPort(conf); String response = PhoenixMRJobUtil.getJobsInformationFromRM(rmHost, rmPort, urlParams); LOG.debug("Already Submitted/Running Apps = " + response); JSONObject jobsJson = new JSONObject(response); JSONObject appsJson = jobsJson.optJSONObject(YarnApplication.APPS_ELEMENT); Set<String> yarnApplicationSet = new HashSet<String>(); if (appsJson == null) { return yarnApplicationSet; } JSONArray appJson = appsJson.optJSONArray(YarnApplication.APP_ELEMENT); if (appJson == null) { return yarnApplicationSet; } for (int i = 0; i < appJson.length(); i++) { Gson gson = new GsonBuilder().create(); YarnApplication yarnApplication = gson.fromJson(appJson.getJSONObject(i).toString(), new TypeToken<YarnApplication>() { }.getType()); yarnApplicationSet.add(yarnApplication.getName()); } return yarnApplicationSet; }
@Override public List<SModelCheckerInstance> getAllRepositoryModelCheckers() throws ServerException, UserException { requireRealUserAuthentication(); try { List<SModelCheckerInstance> modelCheckers = new ArrayList<SModelCheckerInstance>(); String content = NetUtils.getContent(new URL(getServiceMap().get(SettingsInterface.class).getServiceRepositoryUrl() + "/modelcheckers"), 5000); JSONObject root = new JSONObject(new JSONTokener(content)); JSONArray modelCheckersJson = root.getJSONArray("modelcheckers"); for (int i = 0; i < modelCheckersJson.length(); i++) { JSONObject modelCheckerJson = modelCheckersJson.getJSONObject(i); SModelCheckerInstance sModelChecker = new SModelCheckerInstance(); sModelChecker.setName(modelCheckerJson.getString("name")); sModelChecker.setCode(modelCheckerJson.getString("code")); sModelChecker.setDescription(modelCheckerJson.getString("description")); sModelChecker.setModelCheckerPluginClassName(modelCheckerJson.getString("modelCheckerPluginClassName")); modelCheckers.add(sModelChecker); } return modelCheckers; } catch (Exception e) { return handleException(e); } }
JSONObject root = new JSONObject(new JSONTokener(content)); JSONArray extendedDataSchemasJson = root.getJSONArray("extendeddataschemas"); for (int i = 0; i < extendedDataSchemasJson.length(); i++) { JSONObject extendedDataSchemaJson = extendedDataSchemasJson.getJSONObject(i);
&& node.getArray().length() > node.getArrayIndex()) { Node arrayNode = node; int idx = arrayNode.getArrayIndex();