/** * Returns a java.util.List containing all of the elements in this array. * If an element in the array is a JSONArray or JSONObject it will also * be converted. * <p> * Warning: This method assumes that the data structure is acyclical. * * @return a java.util.List containing the elements of this array */ public List<Object> toList() { List<Object> results = new ArrayList<Object>(this.myArrayList.size()); for (Object element : this.myArrayList) { if (element == null || JSONObject.NULL.equals(element)) { results.add(null); } else if (element instanceof JSONArray) { results.add(((JSONArray) element).toList()); } else if (element instanceof JSONObject) { results.add(((JSONObject) element).toMap()); } else { results.add(element); } } return results; } }
/** * Returns a java.util.Map containing all of the entries in this object. * If an entry in the object is a JSONArray or JSONObject it will also * be converted. * <p> * Warning: This method assumes that the data structure is acyclical. * * @return a java.util.Map containing the entries of this object */ public Map<String, Object> toMap() { Map<String, Object> results = new HashMap<String, Object>(); for (Entry<String, Object> entry : this.entrySet()) { Object value; if (entry.getValue() == null || NULL.equals(entry.getValue())) { value = null; } else if (entry.getValue() instanceof JSONObject) { value = ((JSONObject) entry.getValue()).toMap(); } else if (entry.getValue() instanceof JSONArray) { value = ((JSONArray) entry.getValue()).toList(); } else { value = entry.getValue(); } results.put(entry.getKey(), value); } return results; } }
/** * Returns a java.util.List containing all of the elements in this array. * If an element in the array is a JSONArray or JSONObject it will also * be converted. * <p> * Warning: This method assumes that the data structure is acyclical. * * @return a java.util.List containing the elements of this array */ public List<Object> toList() { List<Object> results = new ArrayList<Object>(this.myArrayList.size()); for (Object element : this.myArrayList) { if (element == null || JSONObject.NULL.equals(element)) { results.add(null); } else if (element instanceof JSONArray) { results.add(((JSONArray) element).toList()); } else if (element instanceof JSONObject) { results.add(((JSONObject) element).toMap()); } else { results.add(element); } } return results; }
public List<String> getStrings(MappingDeclaration declaration) { if (!isStrings(declaration)) return null; Object obj = this.opt(declaration.getMapping().name()); if (obj == null) return new ArrayList<>(0); boolean valid = obj instanceof JSONArray; if (!valid) return null; return ((JSONArray) obj).toList().stream().map(o -> Objects.toString(o, null)).collect(Collectors.toList()); }
public List<Integer> getInts(MappingDeclaration declaration) { if (!isInts(declaration)) return null; Object obj = this.opt(declaration.getMapping().name()); boolean valid = obj instanceof JSONArray; assert valid; if (!valid) return null; return ((JSONArray) obj).toList().stream().map(o -> (int) o).collect(Collectors.toList()); }
public List<Long> getLongs(MappingDeclaration declaration) { if (!isLongs(declaration)) return null; Object obj = this.opt(declaration.getMapping().name()); boolean valid = obj instanceof JSONArray; assert valid; if (!valid) return null; return ((JSONArray) obj).toList().stream().map(o -> (long) o).collect(Collectors.toList()); }
/** * Returns a java.util.Map containing all of the entries in this object. * If an entry in the object is a JSONArray or JSONObject it will also * be converted. * <p> * Warning: This method assumes that the data structure is acyclical. * * @return a java.util.Map containing the entries of this object */ public Map<String, Object> toMap() { Map<String, Object> results = new HashMap<String, Object>(); for (Entry<String, Object> entry : this.entrySet()) { Object value; if (entry.getValue() == null || NULL.equals(entry.getValue())) { value = null; } else if (entry.getValue() instanceof JSONObject) { value = ((JSONObject) entry.getValue()).toMap(); } else if (entry.getValue() instanceof JSONArray) { value = ((JSONArray) entry.getValue()).toList(); } else { value = entry.getValue(); } results.put(entry.getKey(), value); } return results; } }
public AuditLogChange createAuditLogChange(JSONObject change) { final String key = change.getString("key"); Object oldValue = change.isNull("old_value") ? null : change.get("old_value"); Object newValue = change.isNull("new_value") ? null : change.get("new_value"); // Don't confront users with JSON if (oldValue instanceof JSONArray || newValue instanceof JSONArray) { oldValue = oldValue instanceof JSONArray ? ((JSONArray) oldValue).toList() : oldValue; newValue = newValue instanceof JSONArray ? ((JSONArray) newValue).toList() : newValue; } else if (oldValue instanceof JSONObject || newValue instanceof JSONObject) { oldValue = oldValue instanceof JSONObject ? ((JSONObject) oldValue).toMap() : oldValue; newValue = newValue instanceof JSONObject ? ((JSONObject) newValue).toMap() : newValue; } return new AuditLogChange(oldValue, newValue, key); }
static Object toJavaValue(Object orig) { if (orig instanceof JSONArray) { return ((JSONArray) orig).toList(); } else if (orig instanceof JSONObject) { return ((JSONObject) orig).toMap(); } else { return orig; } }
/** * Returns array for {@code key} as a value list. * * @param celestaDoc Celesta document. * @param key array key. * @return */ public static List<String> getList(String celestaDoc, String key) { String json = getCelestaDocJSON(celestaDoc); JSONObject metadata = new JSONObject(json); if (metadata.has(key)) { JSONArray options = metadata.getJSONArray(key); return options.toList().stream().map(String::valueOf).collect(Collectors.toList()); } else { return Collections.emptyList(); } }
/** * Map a JSON object to a {@link ForcedRoot}. * * @param json JSON object * @return a new forced root */ static ForcedRoot fromJSON(final JSONObject json) { final ForcedRoot forcedRoot = new ForcedRoot(); if (json.has(KEY_PATH)) { forcedRoot.setPath(json.getString(KEY_PATH)); } if (json.has(KEY_PRIMARY_TYPE)) { forcedRoot.setPrimaryType(json.getString(KEY_PRIMARY_TYPE)); } ofNullable(json.optJSONArray(KEY_MIXIN_TYPES)) .map(types -> types.toList().stream().map(String::valueOf).collect(Collectors.toList())) .ifPresent(forcedRoot::setMixinTypes); return forcedRoot; }
/** * Returns a java.util.Map containing all of the entries in this object. * If an entry in the object is a JSONArray or JSONObject it will also * be converted. * <p> * Warning: This method assumes that the data structure is acyclical. * * @return a java.util.Map containing the entries of this object */ public Map<String, Object> toMap() { Map<String, Object> results = new HashMap<String, Object>(); for (Entry<String, Object> entry : this.map.entrySet()) { Object value; if (entry.getValue() == null || NULL.equals(entry.getValue())) { value = null; } else if (entry.getValue() instanceof JSONObject) { value = ((JSONObject) entry.getValue()).toMap(); } else if (entry.getValue() instanceof JSONArray) { value = ((JSONArray) entry.getValue()).toList(); } else { value = entry.getValue(); } results.put(entry.getKey(), value); } return results; } }
/** * Returns a java.util.List containing all of the elements in this array. * If an element in the array is a JSONArray or JSONObject it will also * be converted. * <p> * Warning: This method assumes that the data structure is acyclical. * * @return a java.util.List containing the elements of this array */ public List<Object> toList() { List<Object> results = new ArrayList<Object>(this.myArrayList.size()); for (Object element : this.myArrayList) { if (element == null || JSONObject.NULL.equals(element)) { results.add(null); } else if (element instanceof JSONArray) { results.add(((JSONArray) element).toList()); } else if (element instanceof JSONObject) { results.add(((JSONObject) element).toMap()); } else { results.add(element); } } return results; }
/** * Returns a java.util.List containing all of the elements in this array. * If an element in the array is a JSONArray or JSONObject it will also * be converted. * <p> * Warning: This method assumes that the data structure is acyclical. * * @return a java.util.List containing the elements of this array */ public List<Object> toList() { List<Object> results = new ArrayList<Object>(this.myArrayList.size()); for (Object element : this.myArrayList) { if (element == null || JSONObject.NULL.equals(element)) { results.add(null); } else if (element instanceof JSONArray) { results.add(((JSONArray) element).toList()); } else if (element instanceof JSONObject) { results.add(((JSONObject) element).toMap()); } else { results.add(element); } } return results; } }
/** * Returns a java.util.Map containing all of the entries in this object. * If an entry in the object is a JSONArray or JSONObject it will also * be converted. * <p> * Warning: This method assumes that the data structure is acyclical. * * @return a java.util.Map containing the entries of this object */ public Map<String, Object> toMap() { Map<String, Object> results = new HashMap<String, Object>(); for (Entry<String, Object> entry : this.entrySet()) { Object value; if (entry.getValue() == null || NULL.equals(entry.getValue())) { value = null; } else if (entry.getValue() instanceof JSONObject) { value = ((JSONObject) entry.getValue()).toMap(); } else if (entry.getValue() instanceof JSONArray) { value = ((JSONArray) entry.getValue()).toList(); } else { value = entry.getValue(); } results.put(entry.getKey(), value); } return results; } }
/** * Returns list of test steps in a test case * @return */ public List<String> getStepListFromTestCase() { if (testCaseInSessionId == null) { return new ArrayList<>(); } try { JSONObject sessionJson = getJSonResponse(Unirest.get(url + TESTCASEINSESSION_API_URL + testCaseInSessionId)); return sessionJson.getJSONArray("testSteps") .toList() .stream() .map(Object::toString) .collect(Collectors.toList()); } catch (UnirestException | JSONException e) { throw new SeleniumRobotServerException("cannot get test step list", e); } }
/** * @return an array of Xids of transactions that are in the prepared state * on the server. * @throws DecoderException if the response from the server is invalid. */ public Xid[] getPreparedTransactions() throws DecoderException { String url = getRoot() + "/" + AGProtocol.GET_PREPARED_TRANSACTIONS; List<Header> headers = new ArrayList<Header>(1); headers.add(new Header(Protocol.ACCEPT_PARAM_NAME, "application/json")); AGJSONArrayHandler handler = new AGJSONArrayHandler(); get(url, headers, null, handler); List<Xid> res = new ArrayList<Xid>(); for (Object entry : handler.getResult().toList()) { res.add(AGXid.AGXidFromString((String) entry)); } return res.toArray(new Xid[0]); }
@Test public void checksJsonSchemaConstraints() throws Exception { final JSONArray testCases = new JSONArray( readFile("org/zalando/nakadi/validation/invalid-json-schema-examples.json")); for (final Object testCaseObject : testCases) { final JSONObject testCase = (JSONObject) testCaseObject; final JSONObject schemaJson = testCase.getJSONObject("schema"); final List<String> errorMessages = testCase .getJSONArray("errors") .toList() .stream() .map(Object::toString) .collect(toList()); final String description = testCase.getString("description"); assertThat(description, service.collectIncompatibilities(schemaJson).stream().map(Object::toString) .collect(toList()), is(errorMessages)); } } }
@Test public void checkJsonSchemaCompatibility() throws Exception { final JSONArray testCases = new JSONArray( readFile("org/zalando/nakadi/validation/invalid-schema-evolution-examples.json")); for (final Object testCaseObject : testCases) { final JSONObject testCase = (JSONObject) testCaseObject; final Schema original = SchemaLoader.load(testCase.getJSONObject("original_schema")); final Schema update = SchemaLoader.load(testCase.getJSONObject("update_schema")); final List<String> errorMessages = testCase .getJSONArray("errors") .toList() .stream() .map(Object::toString) .collect(toList()); final String description = testCase.getString("description"); assertThat(description, service.collectChanges(original, update).stream() .map(change -> change.getType().toString() + " " + change.getJsonPath()) .collect(toList()), is(errorMessages)); } }
static JsonValue of(Object obj) { if (obj instanceof JsonValue) { return (JsonValue) obj; } else if (obj instanceof Map) { return new JsonObject((Map<String, Object>) obj); } else if (obj instanceof List) { return new JsonArray((List<Object>) obj); } else if (obj instanceof JSONObject) { JSONObject jo = (JSONObject) obj; return new JsonObject(jo.toMap()); } else if (obj instanceof JSONArray) { JSONArray arr = (JSONArray) obj; return new JsonArray(arr.toList()); } return new JsonValue(obj); }