/** * Construct a JSONObject from an Object, using reflection to find the * public members. The resulting JSONObject's keys will be the strings from * the names array, and the values will be the field values associated with * those keys in the object. If a key is not found or not visible, then it * will not be copied into the new JSONObject. * * @param object * An object that has fields that should be used to make a * JSONObject. * @param names * An array of strings, the names of the fields to be obtained * from the object. */ public JSONObject(Object object, String names[]) { this(); Class c = object.getClass(); for (int i = 0; i < names.length; i += 1) { String name = names[i]; try { this.putOpt(name, c.getField(name).get(object)); } catch (Exception ignore) { } } }
/** * * @return this GfJsonObject * @throws GfJsonException If the value is a non-finite number. */ public GfJsonObject put(String key, Collection<?> value) throws GfJsonException { try { jsonObject.putOpt(key, value); } catch (JSONException e) { throw new GfJsonException(e.getMessage()); } return this; }
/** * Construct a JSONObject from an Object, using reflection to find the * public members. The resulting JSONObject's keys will be the strings from * the names array, and the values will be the field values associated with * those keys in the object. If a key is not found or not visible, then it * will not be copied into the new JSONObject. * * @param object * An object that has fields that should be used to make a * JSONObject. * @param names * An array of strings, the names of the fields to be obtained * from the object. */ public JSONObject(Object object, String names[]) { this(names.length); Class<?> c = object.getClass(); for (int i = 0; i < names.length; i += 1) { String name = names[i]; try { this.putOpt(name, c.getField(name).get(object)); } catch (Exception ignore) { } } }
public GfJsonObject putJSONArray(String key, GfJsonArray value) throws GfJsonException { try { jsonObject.putOpt(key, value.getInternalJsonArray()); } catch (JSONException e) { throw new GfJsonException(e.getMessage()); } return this; }
/** * * @return this GfJsonObject * @throws GfJsonException If the value is a non-finite number. */ public GfJsonObject putOpt(String key, Object value) throws GfJsonException { try { jsonObject.putOpt(key, extractInternalForGfJsonOrReturnSame(value)); } catch (JSONException e) { throw new GfJsonException(e.getMessage()); } return this; }
static void jsonObjectPutAll(JSONObject jsonObject, Map<String, Object> map) { Set<Map.Entry<String, Object>> entrySet = map.entrySet(); for (Map.Entry<String, Object> entry : entrySet) { try { jsonObject.putOpt(entry.getKey(), entry.getValue()); } catch (JSONException e) { throw new IllegalArgumentException(e); } } }
json.putOpt(JSON_VALUE, jsonArray);
public static Object getStringPropertyAsJSON( JSONObject jsonObject, String key, String nonJSONPropertyKey ) throws JSONException { Object value = jsonObject.opt(key); if (value != null && value instanceof String) { JSONTokener tokener = new JSONTokener((String) value); value = tokener.nextValue(); } if (value != null && !(value instanceof JSONObject || value instanceof JSONArray)) { if (nonJSONPropertyKey != null) { // Facebook sometimes gives us back a non-JSON value such as // literal "true" or "false" as a result. // If we got something like that, we present it to the caller as a JSONObject // with a single property. We only do this if the caller wants that behavior. jsonObject = new JSONObject(); jsonObject.putOpt(nonJSONPropertyKey, value); return jsonObject; } else { throw new FacebookException("Got an unexpected non-JSON object."); } } return value; }
/** * Serializes the history state to a JSON object using the format described in loadJSON(). */ public JSONObject toJSON() throws JSONException { JSONObject json = new JSONObject(); if (entries.size() > 0) { JSONObject history = new JSONObject(); for (Entry e : entries) { history.put(e.pushId, e.timestamp); } json.put("seen", history); } json.putOpt("lastTime", lastTime); return json; }
public JSONBuilder withAttribute(String key, Object value) { Object jsonValue = value instanceof JSONSerializable ? ((JSONSerializable) value) .toJSONObject() : value; try { json.putOpt(key, jsonValue); } catch (JSONException e) { // somehow value is Double or Float NaN or Infinity, so ignore } return this; }
/** * Construct a JSONObject from an Object, using reflection to find the * public members. The resulting JSONObject's keys will be the strings from * the names array, and the values will be the field values associated with * those keys in the object. If a key is not found or not visible, then it * will not be copied into the new JSONObject. * * @param object * An object that has fields that should be used to make a * JSONObject. * @param names * An array of strings, the names of the fields to be obtained * from the object. */ public JSONObject(Object object, String names[]) { this(names.length); Class<?> c = object.getClass(); for (int i = 0; i < names.length; i += 1) { String name = names[i]; try { this.putOpt(name, c.getField(name).get(object)); } catch (Exception ignore) { } } }
public JSONBuilder withAttribute(String key, Object value) { final Object jsonValue = value instanceof JSONSerializable ? ((JSONSerializable) value).toJSONObject() : value; try { json.putOpt(key, jsonValue); } catch (final JSONException e) { LOGGER.warn("error parsing json", e); } return this; }
recordJson.put(STREAM_NAME_FIELD, source.getStreamName()); recordJson.put(PARTITION_KEY_FIELD, source.getPartitionKey()); recordJson.putOpt(EXPLICIT_HASH_FIELD, source.getExplicitHashKey()); recordJson.putOpt(SEQUENCE_NUMBER_FIELD, source.getSequenceNumberForOrdering()); } catch (final JSONException e) { throw new AmazonClientException("Unable to convert KinesisRecord to JSON "
static void jsonObjectPutAll(JSONObject jsonObject, Map<String, Object> map) { Set<Map.Entry<String, Object>> entrySet = map.entrySet(); for (Map.Entry<String, Object> entry : entrySet) { try { jsonObject.putOpt(entry.getKey(), entry.getValue()); } catch (JSONException e) { throw new IllegalArgumentException(e); } } }
@NonNull private JSONObject toJsonObject(@NonNull Target target) throws JSONException { final JSONObject jsonObject = new JSONObject(); jsonObject.putOpt(TARGET_URL, target.getUrl()); return jsonObject; }
private void initData() { JSONObject json = new JSONObject(); for (String type : SWITCH_PARAM_LIST) { try{ json.putOpt(type, ""); } catch(Exception e) { e.printStackTrace(); } } ServerMessageProcessorManager.sendMessageToClient(EnvSwitchActivity.this, "get" + json.toString()); }
protected void broadcastWorkProductChange(String workProductId, String skipSourceGuid, String workspaceId, JSONObject permissions) { JSONObject json = new JSONObject(); json.put("type", "workProductChange"); json.put("permissions", permissions); JSONObject dataJson = new JSONObject(); dataJson.put("id", workProductId); dataJson.put("workspaceId", workspaceId); dataJson.putOpt("sourceGuid", skipSourceGuid); json.put("data", dataJson); broadcastJson(json); }
public void broadcastWorkProductAncillaryChange(String workProductId, String workspaceId, String ancillaryId, User user, String skipSourceGuid) { JSONObject json = new JSONObject(); json.put("type", "workProductAncillaryChange"); json.put("permissions", getPermissionsWithWorkspace(workspaceId)); JSONObject dataJson = new JSONObject(); dataJson.put("id", ancillaryId); dataJson.put("workspaceId", workspaceId); dataJson.put("productId", workProductId); dataJson.putOpt("sourceGuid", skipSourceGuid); json.put("data", dataJson); broadcastJson(json); }
private static void processEnumType(final EnumTypeDefinition enumLeafType, final JSONObject property) throws JSONException { List<EnumPair> enumPairs = enumLeafType.getValues(); List<String> enumNames = new ArrayList<>(); for (EnumPair enumPair : enumPairs) { enumNames.add(enumPair.getName()); } property.putOpt(ENUM, new JSONArray(enumNames)); }
@Override public JSONObject toJson() { JSONObject json = new JSONObject(); json.put("cpu", JsonSerializables.serialize(this.cpuUsage, false)); json.put("ram", JsonSerializables.serialize(this.ramUsage, false)); json.putOpt("network", JsonSerializables.serialize(this.networkUsage, false)); json.putOpt("disk", JsonSerializables.serialize(this.diskUsage, false)); json.put("utilization", this.resourceUtilization); json.put("overhead", this.overheadMillis); return json; }