Java Code Examples for com.eclipsesource.json.JsonObject#names()

The following examples show how to use com.eclipsesource.json.JsonObject#names() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZCashClientCaller.java    From zencash-swing-wallet-ui with MIT License 6 votes vote down vote up
private void decomposeJSONValue(String name, JsonValue val, Map<String, String> map)
{
	if (val.isObject())
	{
		JsonObject obj = val.asObject();
		for (String memberName : obj.names())
		{
			this.decomposeJSONValue(name + "." + memberName, obj.get(memberName), map);
		}
	} else if (val.isArray())
	{
		JsonArray arr = val.asArray();
		for (int i = 0; i < arr.size(); i++)
		{
			this.decomposeJSONValue(name + "[" + i + "]", arr.get(i), map);
		}
	} else
	{
		map.put(name, val.toString());
	}
}
 
Example 2
Source File: TestUtil.java    From r2cloud with Apache License 2.0 6 votes vote down vote up
public static void assertJson(JsonObject expected, JsonObject actual) {
	StringBuilder message = new StringBuilder();
	for (String name : expected.names()) {
		JsonValue value = actual.get(name);
		if (value == null) {
			message.append("missing field: " + name).append("\n");
			continue;
		}
		if (expected.get(name).isObject() && value.isObject()) {
			assertJson(expected.get(name).asObject(), value.asObject());
			continue;
		}
		String expectedValue = expected.get(name).toString();
		String actualValue = value.toString();
		if (!actualValue.equals(expectedValue)) {
			message.append("field: \"" + name + "\" expected: " + expectedValue + " actual: " + actualValue + "\n");
		}
	}

	if (message.length() > 0) {
		fail(message.toString().trim());
	}
}
 
Example 3
Source File: JsonRunner_Test.java    From minimal-json with MIT License 6 votes vote down vote up
private boolean equalsIgnoreOrder(JsonObject object1, JsonValue value2) {
  if (!value2.isObject()) {
    return false;
  }
  JsonObject object2 = value2.asObject();
  List<String> names1 = object1.names();
  List<String> names2 = object2.names();
  if (!names1.containsAll(names2) || !names2.containsAll(names1)) {
    return false;
  }
  for (String name : names1) {
    if (!equalsIgnoreOrder(object1.get(name), object2.get(name))) {
      return false;
    }
  }
  return true;
}
 
Example 4
Source File: ZCashClientCaller.java    From zencash-swing-wallet-ui with MIT License 5 votes vote down vote up
public synchronized Map<String, String> getRawTransactionDetails(String txID)
	throws WalletCallException, IOException, InterruptedException
{
	JsonObject jsonTransaction = this.executeCommandAndGetJsonObject(
		"gettransaction", wrapStringParameter(txID));

	Map<String, String> map = new HashMap<String, String>();

	for (String name : jsonTransaction.names())
	{
		this.decomposeJSONValue(name, jsonTransaction.get(name), map);
	}
			
	return map;
}
 
Example 5
Source File: Body.java    From JWT4B with GNU General Public License v3.0 5 votes vote down vote up
private KeyValuePair lookForJwtInJsonObject(JsonObject object) {
	KeyValuePair rec;
	for (String name : object.names()) {
		if (object.get(name).isString()) {
			if (TokenCheck.isValidJWT(object.get(name).asString())) {
				return new KeyValuePair(name, object.get(name).asString().trim());
			}
		} else if (object.get(name).isObject()) {
			if ((rec = lookForJwtInJsonObject(object.get(name).asObject())) != null) {
				return rec;
			}
		}
	}
	return null;
}
 
Example 6
Source File: ObjectJsonRule.java    From dungeon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void validate(JsonValue value) {
  if (!value.isObject()) {
    throw new IllegalArgumentException(value + " is not an object.");
  }
  JsonObject object = value.asObject();
  Set<String> names = new HashSet<>(object.names());
  for (Entry<String, JsonRule> entry : rules.entrySet()) {
    entry.getValue().validate(object.get(entry.getKey()));
    names.remove(entry.getKey());
  }
  if (!names.isEmpty()) {
    throw new IllegalArgumentException(String.format("%s does not have a rule.", names.iterator().next()));
  }
}