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);
}
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;
}
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;
}
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();
}
}
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);
}
Aggregations