Search in sources :

Example 51 with JSONArray

use of net.sf.json.JSONArray in project pinpoint by naver.

the class JsonLibJSONArrayIT method jsonToArrayTest.

@SuppressWarnings("deprecation")
@Test
public void jsonToArrayTest() throws Exception {
    Method fromObject = JSONArray.class.getMethod("fromObject", Object.class);
    Method toArray = JSONArray.class.getMethod("toArray", JSONArray.class);
    Method toList = JSONArray.class.getMethod("toList", JSONArray.class);
    // JSONArray.toCollection() is added in json-lib 2.2. so check toCollection in JSONArray
    Method toCollection = null;
    try {
        toCollection = JSONArray.class.getMethod("toCollection", JSONArray.class);
    } catch (NoSuchMethodException ignored) {
    }
    String json = "[{'string':'JSON'}]";
    JSONArray jsonArray = JSONArray.fromObject(json);
    // JSONArray.toArray() of json-lib 2.0 and below have different return type. so we invoke it by reflection to avoid NoSuchMethodError
    toArray.invoke(null, jsonArray);
    JSONArray.toList(jsonArray);
    if (toCollection != null) {
        JSONArray.toCollection(jsonArray);
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTrace(event(SERVICE_TYPE, fromObject, annotation(ANNOTATION_KEY, json.length())));
    verifier.verifyTrace(event(SERVICE_TYPE, toArray));
    verifier.verifyTrace(event(SERVICE_TYPE, toList));
    if (toCollection != null) {
        verifier.verifyTrace(event(SERVICE_TYPE, toCollection));
    }
    verifier.verifyTraceCount(0);
}
Also used : JSONArray(net.sf.json.JSONArray) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 52 with JSONArray

use of net.sf.json.JSONArray in project zaproxy by zaproxy.

the class ApiResponseList method toJSON.

@Override
public JSON toJSON() {
    if (list == null) {
        return null;
    }
    JSONObject jo = new JSONObject();
    JSONArray array = new JSONArray();
    // Add the array in place to prevent auto conversion
    jo.put(getName(), array);
    for (ApiResponse resp : this.list) {
        if (resp instanceof ApiResponseElement) {
            String value = ((ApiResponseElement) resp).getValue();
            value = JsonUtil.getJsonFriendlyString(value);
            jo.getJSONArray(getName()).add(value);
        } else {
            // toString() is required to prevent auto conversion of text values to JSON
            jo.getJSONArray(getName()).add(resp.toJSON().toString());
        }
    }
    return jo;
}
Also used : JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray)

Example 53 with JSONArray

use of net.sf.json.JSONArray in project zaproxy by zaproxy.

the class ContextAPI method handleApiAction.

@Override
public ApiResponse handleApiAction(String name, JSONObject params) throws ApiException {
    log.debug("handleApiAction " + name + " " + params.toString());
    Context context;
    TechSet techSet;
    String[] techNames;
    String filename;
    File f;
    switch(name) {
        case ACTION_EXCLUDE_FROM_CONTEXT_REGEX:
            try {
                addExcludeToContext(getContext(params), params.getString(REGEX_PARAM));
            } catch (IllegalArgumentException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, REGEX_PARAM, e);
            }
            break;
        case ACTION_INCLUDE_IN_CONTEXT_REGEX:
            try {
                addIncludeToContext(getContext(params), params.getString(REGEX_PARAM));
            } catch (IllegalArgumentException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, REGEX_PARAM, e);
            }
            break;
        case ACTION_SET_CONTEXT_REGEXS:
            context = getContext(params);
            JSONArray incRegexs;
            JSONArray excRegexs;
            try {
                incRegexs = JSONArray.fromObject(params.get(INC_REGEXS_PARAM));
                context.setIncludeInContextRegexs(JsonUtil.toStringList(incRegexs));
            } catch (JSONException e1) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, INC_REGEXS_PARAM);
            }
            try {
                excRegexs = JSONArray.fromObject(params.get(EXC_REGEXS_PARAM));
                context.setExcludeFromContextRegexs(JsonUtil.toStringList(excRegexs));
            } catch (Exception e1) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, EXC_REGEXS_PARAM);
            }
            Model.getSingleton().getSession().saveContext(context);
            break;
        case ACTION_SET_CONTEXT_CHECKING_STRATEGY:
            context = getContext(params);
            AuthCheckingStrategy checkingStrategy;
            try {
                checkingStrategy = AuthCheckingStrategy.valueOf(params.getString(PARAM_CHECKING_STRATEGRY));
            } catch (Exception e1) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_CHECKING_STRATEGRY);
            }
            if (AuthCheckingStrategy.POLL_URL.equals(checkingStrategy)) {
                AuthPollFrequencyUnits units;
                try {
                    units = AuthPollFrequencyUnits.valueOf(params.getString(PARAM_POLL_FREQ_UNITS));
                } catch (Exception e) {
                    throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_POLL_FREQ_UNITS);
                }
                int freq;
                String pollUrl = params.getString(PARAM_POLL_URL);
                String pollData = params.getString(PARAM_POLL_DATA);
                String pollHeaders = params.getString(PARAM_POLL_HEADERS);
                if (pollUrl == null || pollUrl.isEmpty()) {
                    throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_POLL_URL);
                }
                try {
                    new URI(pollUrl, true);
                } catch (Exception e) {
                    throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_POLL_URL);
                }
                try {
                    freq = params.getInt(PARAM_POLL_FREQ);
                } catch (Exception e) {
                    throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_POLL_FREQ);
                }
                if (freq <= 0) {
                    throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_POLL_FREQ);
                }
                context.getAuthenticationMethod().setPollUrl(pollUrl);
                context.getAuthenticationMethod().setPollData(pollData);
                context.getAuthenticationMethod().setPollHeaders(pollHeaders);
                context.getAuthenticationMethod().setPollFrequency(freq);
                context.getAuthenticationMethod().setPollFrequencyUnits(units);
            }
            context.getAuthenticationMethod().setAuthCheckingStrategy(checkingStrategy);
            Model.getSingleton().getSession().saveContext(context);
            break;
        case ACTION_NEW_CONTEXT:
            String contextName = params.getString(CONTEXT_NAME);
            try {
                context = Model.getSingleton().getSession().getNewContext(contextName);
            } catch (IllegalContextNameException e) {
                throw new ApiException(ApiException.Type.ALREADY_EXISTS, contextName, e);
            }
            Model.getSingleton().getSession().saveContext(context);
            return new ApiResponseElement(CONTEXT_ID, String.valueOf(context.getId()));
        case ACTION_REMOVE_CONTEXT:
            context = getContext(params);
            Model.getSingleton().getSession().deleteContext(context);
            break;
        case ACTION_SET_CONTEXT_IN_SCOPE:
            context = getContext(params);
            context.setInScope(params.getBoolean(IN_SCOPE));
            Model.getSingleton().getSession().saveContext(context);
            break;
        case ACTION_IMPORT_CONTEXT:
            filename = params.getString(CONTEXT_FILE_PARAM);
            f = new File(filename);
            if (!f.exists()) {
                // Try relative to the contexts dir
                f = new File(Constant.getContextsDir(), filename);
            }
            if (!f.exists()) {
                throw new ApiException(ApiException.Type.DOES_NOT_EXIST, f.getAbsolutePath());
            } else {
                try {
                    context = Model.getSingleton().getSession().importContext(f);
                } catch (IllegalContextNameException e) {
                    throw new ApiException(ApiException.Type.BAD_EXTERNAL_DATA, e);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
                }
            }
            return new ApiResponseElement(CONTEXT_ID, String.valueOf(context.getId()));
        case ACTION_EXPORT_CONTEXT:
            filename = params.getString(CONTEXT_FILE_PARAM);
            context = getContext(params);
            f = new File(filename);
            if (!f.getAbsolutePath().equals(filename)) {
                // Not an absolute filename, use one relative to the contexts dir
                f = new File(Constant.getContextsDir(), filename);
            }
            if (!f.getParentFile().canWrite()) {
                // Cant write to the parent dir so not looking good
                throw new ApiException(ApiException.Type.NO_ACCESS, f.getAbsolutePath());
            } else {
                try {
                    Model.getSingleton().getSession().exportContext(context, f);
                } catch (Exception e) {
                    throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
                }
            }
            break;
        case ACTION_INCLUDE_TECHS:
            context = getContext(params);
            techSet = context.getTechSet();
            techNames = getParam(params, PARAM_TECH_NAMES, "").split(",");
            for (String techName : techNames) {
                techSet.include(getTech(techName));
            }
            context.save();
            break;
        case ACTION_INCLUDE_ALL_TECHS:
            context = getContext(params);
            techSet = new TechSet(Tech.getAll());
            context.setTechSet(techSet);
            context.save();
            break;
        case ACTION_EXCLUDE_TECHS:
            context = getContext(params);
            techSet = context.getTechSet();
            techNames = getParam(params, PARAM_TECH_NAMES, "").split(",");
            for (String techName : techNames) {
                techSet.exclude(getTech(techName));
            }
            context.save();
            break;
        case ACTION_EXCLUDE_ALL_TECHS:
            context = getContext(params);
            techSet = context.getTechSet();
            for (Tech tech : Tech.getAll()) {
                techSet.exclude(tech);
            }
            context.save();
            break;
        default:
            throw new ApiException(Type.BAD_ACTION);
    }
    return ApiResponseElement.OK;
}
Also used : Context(org.zaproxy.zap.model.Context) TechSet(org.zaproxy.zap.model.TechSet) AuthPollFrequencyUnits(org.zaproxy.zap.authentication.AuthenticationMethod.AuthPollFrequencyUnits) JSONArray(net.sf.json.JSONArray) AuthCheckingStrategy(org.zaproxy.zap.authentication.AuthenticationMethod.AuthCheckingStrategy) JSONException(net.sf.json.JSONException) URI(org.apache.commons.httpclient.URI) JSONException(net.sf.json.JSONException) IllegalContextNameException(org.zaproxy.zap.model.IllegalContextNameException) Tech(org.zaproxy.zap.model.Tech) IllegalContextNameException(org.zaproxy.zap.model.IllegalContextNameException) File(java.io.File)

Example 54 with JSONArray

use of net.sf.json.JSONArray in project beam by apache.

the class Main method main.

public static void main(String[] args) {
    try {
        final Logger logger = LoggerFactory.getLogger(Main.class);
        String outputFile;
        if (args.length == 0) {
            logger.info("Output file name missing. Output will be saved to capability.json");
            outputFile = "capability";
        } else {
            outputFile = args[0];
            logger.info("Output will be saved to {}.json", outputFile);
        }
        JSONArray outputDetails = new JSONArray();
        logger.info("Processing Batch Jobs:");
        ModeTestService batchTestService = new ModeTestService("batch");
        outputDetails.add(batchTestService.getTests());
        logger.info("Processing Stream Jobs:");
        ModeTestService streamTestService = new ModeTestService("stream");
        outputDetails.add(streamTestService.getTests());
        try (FileWriter file = new FileWriter(outputFile + ".json")) {
            file.write(outputDetails.toString(3));
            file.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : ModeTestService(org.apache.beam.validate.runner.service.ModeTestService) FileWriter(java.io.FileWriter) JSONArray(net.sf.json.JSONArray) IOException(java.io.IOException) Logger(org.slf4j.Logger) IOException(java.io.IOException)

Example 55 with JSONArray

use of net.sf.json.JSONArray in project fastjson by alibaba.

the class JSONLibXmlTest method test_xml.

public void test_xml() throws Exception {
    XMLSerializer xmlSerializer = new XMLSerializer();
    JSONObject json = new JSONObject();
    json.put("id", 123);
    json.put("name", "jobs");
    json.put("flag", true);
    JSONArray items = new JSONArray();
    items.add("x");
    items.add(234);
    items.add(false);
    json.put("items", items);
    String text = xmlSerializer.write(json);
    System.out.println(text);
}
Also used : XMLSerializer(net.sf.json.xml.XMLSerializer) JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray)

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