Search in sources :

Example 56 with JSONArray

use of net.sf.json.JSONArray in project hudson-2.x by hudson.

the class JSONTest method testCanonicalWriter.

public void testCanonicalWriter() throws IOException {
    JSONArray jsonArray = new JSONArray();
    jsonArray.add(true);
    jsonArray.add(1);
    jsonArray.add(5.3);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("key1", "1");
    jsonObject.put("key2", "2");
    jsonObject.put("key3", "3");
    jsonObject.put("string", "123\r\n\b\t\f\\\\u65E5\\u672C\\u8A9E");
    jsonArray.add(jsonObject);
    Writer writer = new StringWriter();
    JSONCanonicalUtils.write(jsonArray, writer);
    assertEquals(writer.toString(), "[true,1,5.3,{\"key1\":\"1\",\"key2\":\"2\",\"key3\":\"3\",\"string\":\"123\\u000d\\u000a\\u0008\\u0009\\u000c\\\\\\\\u65E5\\\\u672C\\\\u8A9E\"}]");
}
Also used : JSONObject(net.sf.json.JSONObject) StringWriter(java.io.StringWriter) JSONArray(net.sf.json.JSONArray) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 57 with JSONArray

use of net.sf.json.JSONArray in project blueocean-plugin by jenkinsci.

the class HttpResponse method getContent.

@CheckForNull
public InputStream getContent() {
    try {
        HttpEntity entity = response.getEntity();
        if (getStatus() >= 300) {
            ErrorMessage errorMessage = new ErrorMessage(getStatus(), getStatusLine());
            // WireMock returns "Bad Request" for the status message; it's also pretty nondescript
            if (StringUtils.isEmpty(errorMessage.message) || "Bad Request".equals(errorMessage.message)) {
                String message;
                List<ErrorMessage.Error> errors = new ArrayList<>();
                try {
                    JSONObject jsonResponse = JSONObject.fromObject(IOUtils.toString(entity.getContent()));
                    JSONArray arr = jsonResponse.getJSONArray("errors");
                    StringBuilder messageBuilder = new StringBuilder();
                    for (int i = 0; i < arr.size(); i++) {
                        JSONObject err = arr.getJSONObject(i);
                        if (i > 0) {
                            messageBuilder.append(", ");
                        }
                        messageBuilder.append(err.getString("message"));
                        StringBuilder details = new StringBuilder();
                        if (err.has("details")) {
                            JSONArray errorDetails = err.getJSONArray("details");
                            for (int detailIdx = 0; detailIdx < errorDetails.size(); detailIdx++) {
                                details.append(errorDetails.getString(detailIdx)).append("\n");
                            }
                        } else {
                            details.append("no error details");
                        }
                        errors.add(new ErrorMessage.Error("", err.getString("exceptionName"), details.toString()));
                    }
                    message = messageBuilder.toString();
                } catch (Exception e) {
                    logger.error("An error occurred getting BitBucket API error content", e);
                    message = "An unknown error was reported from the BitBucket server";
                }
                errorMessage = new ErrorMessage(getStatus(), message).addAll(errors);
            } else {
                EntityUtils.consume(entity);
            }
            throw new ServiceException(getStatus(), errorMessage, null);
        }
        return entity == null ? null : entity.getContent();
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) JSONArray(net.sf.json.JSONArray) IOException(java.io.IOException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) CheckForNull(javax.annotation.CheckForNull)

Example 58 with JSONArray

use of net.sf.json.JSONArray in project blueocean-plugin by jenkinsci.

the class JenkinsJSExtensions method getExtensionsData.

/**
 * Return the actual data, from /js-extensions
 */
public static JSONArray getExtensionsData() {
    Object jsExtensionData = getJenkinsJSExtensionData();
    JSONArray jsExtensionDataJson = JSONArray.fromObject(jsExtensionData);
    return jsExtensionDataJson;
}
Also used : JSONArray(net.sf.json.JSONArray)

Example 59 with JSONArray

use of net.sf.json.JSONArray in project blueocean-plugin by jenkinsci.

the class SSEConnection method toJSONArray.

private JSONArray toJSONArray(Collection<EventFilter> filters) {
    JSONArray a = new JSONArray();
    for (EventFilter f : filters) {
        JSONObject o = new JSONObject();
        o.putAll(f);
        a.add(o);
    }
    return a;
}
Also used : JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray) EventFilter(org.jenkinsci.plugins.pubsub.EventFilter)

Example 60 with JSONArray

use of net.sf.json.JSONArray in project cachecloud by sohutv.

the class AppController method assembleJson.

private String assembleJson(List<AppCommandStats> appCommandStatsList, Integer addDay) {
    if (appCommandStatsList == null || appCommandStatsList.isEmpty()) {
        return "[]";
    }
    List<SimpleChartData> list = new ArrayList<SimpleChartData>();
    for (AppCommandStats stat : appCommandStatsList) {
        try {
            SimpleChartData chartData = SimpleChartData.getFromAppCommandStats(stat, addDay);
            list.add(chartData);
        } catch (ParseException e) {
            logger.info(e.getMessage(), e);
        }
    }
    JSONArray jsonArray = JSONArray.fromObject(list);
    return jsonArray.toString();
}
Also used : ArrayList(java.util.ArrayList) JSONArray(net.sf.json.JSONArray) SimpleChartData(com.sohu.cache.web.chart.model.SimpleChartData) ParseException(java.text.ParseException)

Aggregations

JSONArray (net.sf.json.JSONArray)144 JSONObject (net.sf.json.JSONObject)109 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)22 HashMap (java.util.HashMap)20 File (java.io.File)16 Test (org.junit.Test)15 JSON (net.sf.json.JSON)14 Map (java.util.Map)12 JsonConfig (net.sf.json.JsonConfig)10 URISyntaxException (java.net.URISyntaxException)9 URL (java.net.URL)9 URI (java.net.URI)8 SimpleChartData (com.sohu.cache.web.chart.model.SimpleChartData)6 Date (java.util.Date)6 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)5 OutputStream (java.io.OutputStream)5 List (java.util.List)5 CAFunctorFactory (edu.uiuc.ncsa.myproxy.oa4mp.oauth2.claims.CAFunctorFactory)4 OA2Client (edu.uiuc.ncsa.security.oauth_2_0.OA2Client)4