use of org.json.JSONString in project GNS by MobilityFirst.
the class JSONCompare method compareJson.
/**
* Compares {@link JSONString} provided to the expected {@code JSONString}, checking that the
* {@link org.json.JSONString#toJSONString()} are equal.
*
* @param expected Expected {@code JSONstring}
* @param actual {@code JSONstring} to compare
* @return a JSONCompareResult
*/
public static JSONCompareResult compareJson(final JSONString expected, final JSONString actual) {
final JSONCompareResult result = new JSONCompareResult();
final String expectedJson = expected.toJSONString();
final String actualJson = actual.toJSONString();
if (!expectedJson.equals(actualJson)) {
result.fail("");
}
return result;
}
use of org.json.JSONString in project GNS by MobilityFirst.
the class JSONCompare method compareJSON.
/**
* Compares JSON string provided to the expected JSON string using provided comparator, and returns the results of
* the comparison.
*
* @param expectedStr Expected JSON string
* @param actualStr JSON string to compare
* @param comparator Comparator to use
* @return result of the comparison
* @throws JSONException
* @throws IllegalArgumentException when type of expectedStr doesn't match the type of actualStr
*/
public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator) throws JSONException {
Object expected = JSONParser.parseJSON(expectedStr);
Object actual = JSONParser.parseJSON(actualStr);
if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) {
return compareJSON((JSONObject) expected, (JSONObject) actual, comparator);
} else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) {
return compareJSON((JSONArray) expected, (JSONArray) actual, comparator);
} else if (expected instanceof JSONString && actual instanceof JSONString) {
return compareJson((JSONString) expected, (JSONString) actual);
} else if (expected instanceof JSONObject) {
return new JSONCompareResult().fail("", expected, actual);
} else {
return new JSONCompareResult().fail("", expected, actual);
}
}
use of org.json.JSONString in project GNS by MobilityFirst.
the class CanonicalJSON method renderSimpleCanonicalJSON.
/* This should be identical to the standard code to render the JSON object,
* except it forces the keys for maps to be listed in sorted order. */
private static String renderSimpleCanonicalJSON(Object x) {
try {
if (x instanceof JSONObject) {
JSONObject theObject = (JSONObject) x;
// Sort the keys
TreeSet<String> t = new TreeSet<>();
Iterator<?> i = theObject.keys();
while (i.hasNext()) {
t.add((String) i.next());
}
Iterator<String> keys = t.iterator();
StringBuilder sb = new StringBuilder("{");
while (keys.hasNext()) {
if (sb.length() > 1) {
sb.append(',');
}
Object o = keys.next();
sb.append(canonicalQuote(o.toString()));
sb.append(':');
sb.append(renderSimpleCanonicalJSON(theObject.get(o.toString())));
}
sb.append('}');
return sb.toString();
} else if (x instanceof JSONArray) {
JSONArray theArray = (JSONArray) x;
StringBuilder sb = new StringBuilder();
sb.append("[");
int len = theArray.length();
for (int i = 0; i < len; i += 1) {
if (i > 0) {
sb.append(",");
}
sb.append(renderSimpleCanonicalJSON(theArray.get(i)));
}
sb.append("]");
return sb.toString();
} else {
if (x == null || x.equals(null)) {
return "null";
}
if (x instanceof JSONString) {
Object object;
try {
object = ((JSONString) x).toJSONString();
} catch (Exception e) {
throw new JSONException(e.getMessage());
}
if (object instanceof String) {
return (String) object;
}
throw new JSONException("Bad value from toJSONString: " + object);
}
if (x instanceof Number) {
return JSONObject.numberToString((Number) x);
}
if (x instanceof Boolean || x instanceof JSONObject || x instanceof JSONArray) {
return x.toString();
}
if (x instanceof Map) {
return renderSimpleCanonicalJSON(new JSONObject((Map<?, ?>) x));
}
if (x instanceof Collection) {
return renderSimpleCanonicalJSON(new JSONArray((Collection<?>) x));
}
if (x.getClass().isArray()) {
return renderSimpleCanonicalJSON(new JSONArray(x));
}
return canonicalQuote(x.toString());
}
} catch (Exception e) {
return null;
}
}
use of org.json.JSONString in project pmph by BCSquad.
the class PmphUserServiceTest method postJson.
/**
* post请求,参数为json字符串
* @param url 请求地址
* @param jsonString json字符串
* @return 响应
*/
public static String postJson(String url, String jsonString) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
try {
// 解决中文乱码问题
StringEntity stringEntity = new StringEntity(jsonString, "UTF-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
post.setEntity(stringEntity);
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
use of org.json.JSONString in project graal by oracle.
the class ChromeInspectorSubstitutions method wrap.
public static Object wrap(Object object) {
try {
if (object == null) {
return NULL;
}
if (object instanceof JSONObject || object instanceof JSONArray || NULL.equals(object) || object instanceof JSONString || object instanceof Byte || object instanceof Character || object instanceof Short || object instanceof Integer || object instanceof Long || object instanceof Boolean || object instanceof Float || object instanceof Double || object instanceof String || object instanceof BigInteger || object instanceof BigDecimal || object instanceof Enum) {
return object;
}
if (object instanceof Collection) {
Collection<?> coll = (Collection<?>) object;
return new JSONArray(coll);
}
if (object.getClass().isArray()) {
return new JSONArray(object);
}
if (object instanceof Map) {
Map<?, ?> map = (Map<?, ?>) object;
return new JSONObject(map);
}
String className = object.getClass().getName();
String objectPackageName = className.contains(".") ? "" : className.substring(0, className.lastIndexOf("."));
if (objectPackageName.startsWith("java.") || objectPackageName.startsWith("javax.")) {
return object.toString();
}
throw new UnsupportedOperationException("Constructing JSON from an object is not supported in SVM.");
} catch (Exception exception) {
return null;
}
}
Aggregations