Refine search
/** * convert an object to json without wrapping it with the simple class name. * @param object * @return the string of json */ public static String jsonConvertToNoWrap(Object object) { if (object == null) { throw new NullPointerException(); } JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter( new PropertyFilter(){ public boolean apply( Object source, String name, Object value ) { return value == null; } }); JSONObject jsonObject = JSONObject.fromObject( object, jsonConfig ); String json = jsonObject.toString(); return json; }
JsonConfig config = new JsonConfig(); config.registerJsonValueProcessor( Class.forName("java.util.Date"), new NiceDateJsonBeanProcessor()); final JSONObject jsonObjectForDate = JSONSerializer.toJSON(object, jsonConfig)
/** * Returns a JSON response with the statistics for severity. * * @return the UI model as JSON */ @JavaScriptMethod @SuppressWarnings("unused") // Called by jelly view public JSONObject getSeverityDistributionJson() { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes( new String[]{ "buildNumber"} ); return JSONObject.fromObject(severityDistribution, jsonConfig); }
JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[]{"files", "createdBy", "lastUpdatedBy"}); jsonConfig.setIgnoreDefaultExcludes(false); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSONObject jsonObject = JSONObject.fromObject(obj, config);
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input ); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY ); jsonConfig.setRootClass( Integer.TYPE ); int[] output = (int[]) JSONSerializer.toJava( jsonArray, jsonConfig );
JsonConfig config = new JsonConfig(); config.setIgnoreDefaultExcludes(false); config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSONArray jsonArray = JSONArray.fromObject(jsonString,config); List list=JSONArray.toList(jsonArray,beanClass);
/** * Renders HTTP response. */ public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException { // Date needs to be converted into iso-8601 date format. JsonConfig config = new JsonConfig(); config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() { public synchronized Object processArrayValue(Object value, JsonConfig jsonConfig) { if (value != null){ DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy HH:mm:ss 'GMT'Z", Functions.getClientLocale()); return dateFormat.format(value); } return null; } public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { return processArrayValue(value, jsonConfig); } }); JSONObject o = new JSONObject(); o.put("events", JSONArray.fromObject(this, config)); rsp.setContentType("application/javascript;charset=UTF-8"); o.write(rsp.getWriter()); } }
@Override protected String extractErrorMessage(String response) { if (response != null && !response.isEmpty()) { try { JSON jsonResponse = JSONSerializer.toJSON(response, new JsonConfig()); if (jsonResponse instanceof JSONObject) { JSONObject object = (JSONObject) jsonResponse; JSONObject errorObj = object.getJSONObject("error"); if (errorObj.containsKey("message")) { return errorObj.getString("message"); } } } catch (JSONException ex) { log.debug("Cannot parse JSON error response: " + response); } } return response; }
@OperationMethod public Object run() throws NuxeoException, IOException { JSONArray commentsTree = new JSONArray(); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(GregorianCalendar.class, new GregorianCalendarJsonValueProcessor()); JSONObject jsonCommentRoot = new JSONObject(); jsonCommentRoot.element("id", commentRoot.getId()); jsonCommentRoot.element("path", commentRoot.getPathAsString()); String author = (String) commentRoot.getProperty(schemaPrefix, "author"); jsonCommentRoot.element("author", author); jsonCommentRoot.element("children", getCommentsThread(commentRoot, commentableDoc, new JSONArray(), jsonConfig)); commentsTree.add(jsonCommentRoot);
public void toObject(ActionInvocation invocation, Reader in, Object target) throws IOException { StringBuilder sb = new StringBuilder(); char[] buffer = new char[1024]; int len = 0; while ((len = in.read(buffer)) > 0) { sb.append(buffer, 0, len); } if (target != null && sb.length() > 0 && sb.charAt(0) == '[') { JSONArray jsonArray = JSONArray.fromObject(sb.toString()); if (target.getClass().isArray()) { JSONArray.toArray(jsonArray, target, new JsonConfig()); } else { JSONArray.toList(jsonArray, target, new JsonConfig()); } } else { JSONObject jsonObject = JSONObject.fromObject(sb.toString()); JSONObject.toBean(jsonObject, target, new JsonConfig()); } }
public static GitStatusContext fromJsonString(final String jsonString) { final JSONObject jsonObject = JSONObject.fromObject(jsonString); final GitStatusContext result; final JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(GitStatusContext.class); result = (GitStatusContext) JSONObject.toBean(jsonObject, jsonConfig); return result; } }
@SuppressWarnings("unchecked") private Collection<Integer> getReferenceIDs() { HttpPost method = null; try { String url = getMethodURL(PROJECT_MANAGER, "getReferenceIDsForProject"); method = new HttpPost(url); JSONArray parameters = new JSONArray(); parameters.add(this.projectID); String body = REQUEST_PARAMETER + URLEncoder.encode(parameters.toString(), ENCODING); method.setEntity(new StringEntity(body, REQUEST_CONTENT_TYPE)); try (CloseableHttpResponse httpResponse = this.client.execute(method)) { String response = IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8); JSONArray ids = (JSONArray) JSONSerializer.toJSON(response); JsonConfig config = new JsonConfig(); config.setCollectionType(Set.class); return JSONArray.toCollection(ids, config); } } catch (Exception ex) { this.logger.warn("Failed to get the reference IDs: {}", ex.getMessage(), ex); } finally { if (method != null) { method.releaseConnection(); } } return Collections.emptySet(); }
/** * @param _for * Account for which the virtual machine is assigned to. */ public VirtualMachineRef createVirtualMachine(HardwareSpec spec, String _for) throws IOException { JsonConfig jsc = new JsonConfig(); jsc.setIgnoreTransientFields(true); String q = _for != null ? "?for=" + _for : ""; HttpURLConnection con = postJson(open("createVirtualMachine" + q), JSONObject.fromObject(spec,jsc)); verifyResponseStatus(con); return new VirtualMachineRef(new URL(con.getHeaderField("Location")), token); }
private JSONArray ParseResponseMessageToArray(ResponseMessage response, String requestId) throws LogException { String returnStr = encodeResponseBodyToUtf8String(response, requestId); try { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setIgnoreDefaultExcludes(true); return JSONArray.fromObject(returnStr, jsonConfig); } catch (JSONException e) { throw new LogException(ErrorCodes.BAD_RESPONSE, "The response is not valid json string : " + returnStr, e, requestId); } }
@Override public JsonConfig getJsonConfig() { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[]{"markupId"}); jsonConfig.registerJsonValueProcessor(YuiId.class, new YuiIdProcessor()); jsonConfig.registerJsonValueProcessor(JsFunction.class, new JsFunctionProcessor()); return jsonConfig; } };
public JsonConfig getJsonConfig() { if (jsonConfig == null) { jsonConfig = new JsonConfig(); } return jsonConfig; }
public void write(Object object, OutputStream out) throws IOException { JsonConfig cfg = new JsonConfig(); cfg.setIgnoreTransientFields(true); cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSON json = JSONSerializer.toJSON(object, cfg); Writer writer = new PrintWriter(out); json.write(writer); writer.flush(); } }
/** * getJsonConfig * @return * @author 唐延波 * @date 2015年7月6日 */ private JsonConfig getJsonConfig(){ JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(excludes); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor(datePattern)); return jsonConfig; }
jsonConfig = new JsonConfig(); if(jsonConfig.getJsonPropertyFilter() == null && !keepNullProperties) jsonConfig.setJsonPropertyFilter( new NullPropertyFilter()); if(jsonConfig.getJsonValueProcessorMatcher().getClass().equals(JsonValueProcessorMatcher.DEFAULT.getClass())){ PropertyEditorRegistry per = null; String objektName = null; valueProzessor.setObjektName(objektName); jsonConfig.registerJsonValueProcessor( AllwaysMatchingValueProcessorMatcher.class, valueProzessor ); jsonConfig.setJsonValueProcessorMatcher(new AllwaysMatchingValueProcessorMatcher()); jsonConfig.addJsonEventListener(valueProzessor); jsonConfig.enableEventTriggering();
@Override public Object invoke(StaplerRequest request, StaplerResponse response, Object instance, Object[] arguments) throws IllegalAccessException, InvocationTargetException, ServletException { JsonOutputFilter annotation = target.getAnnotation((JsonOutputFilter.class)); if (annotation != null) { JsonConfig config = new JsonConfig(); config.setJsonPropertyFilter(new FilterPropertyFilter(annotation.includes(), annotation.excludes())); config.setIgnoreTransientFields(annotation.ignoreTransient()); config.setIgnoreDefaultExcludes(annotation.ignoreDefaultExcludes()); response.setJsonConfig(config); } return target.invoke(request, response, instance, arguments); } }