关键词搜索

源码搜索 ×
×

Java net.sf.json.JSONNull示例

发布2019-04-02浏览1371次

详情内容

原文地址:https://www.programcreek.com/java-api-examples/index.php?api=net.sf.json.JSONNull

以下是使用net.sf.json.JSONNull的示例。

示例1:

  1. /**
  2. * {@inheritDoc}
  3. */
  4. @Override
  5. protected void execute() {
  6. JSONObject postDifferentialCommentResult = postDifferentialComment(comment, SILENT,
  7. commentAction);
  8. if (postDifferentialCommentResult == null ||
  9. !(postDifferentialCommentResult.get("error_info") instanceof JSONNull)) {
  10. if (postDifferentialCommentResult != null) {
  11. info(String.format("Got error %s with action %s",
  12. postDifferentialCommentResult.get("error_info"), commentAction));
  13. }
  14. info("Re-trying with action 'none'");
  15. postDifferentialComment(comment, SILENT, DEFAULT_COMMENT_ACTION);
  16. }
  17. }

示例2:

  1. public CodeCoverageMetrics getParentCoverage(String sha) {
  2. if (sha == null) {
  3. return null;
  4. }
  5. try {
  6. String coverageJSON = getCoverage(sha);
  7. JsonSlurper jsonParser = new JsonSlurper();
  8. JSON responseJSON = jsonParser.parseText(coverageJSON);
  9. if (responseJSON instanceof JSONNull) {
  10. return null;
  11. }
  12. JSONObject coverage = (JSONObject) responseJSON;
  13. return new CodeCoverageMetrics(
  14. ((Double) coverage.getDouble(PACKAGE_COVERAGE_KEY)).floatValue(),
  15. ((Double) coverage.getDouble(FILES_COVERAGE_KEY)).floatValue(),
  16. ((Double) coverage.getDouble(CLASSES_COVERAGE_KEY)).floatValue(),
  17. ((Double) coverage.getDouble(METHOD_COVERAGE_KEY)).floatValue(),
  18. ((Double) coverage.getDouble(LINE_COVERAGE_KEY)).floatValue(),
  19. ((Double) coverage.getDouble(CONDITIONAL_COVERAGE_KEY)).floatValue());
  20. } catch (Exception e) {
  21. e.printStackTrace(logger.getStream());
  22. }
  23. return null;
  24. }

示例3:

  1. public void test_for_issue() throws Exception {
  2. Model model = new Model();
  3. model.object = JSONNull.getInstance();
  4. System.out.println(JSON.toJSONString(model));
  5. // System.out.println(JSON.toJSONString(map));
  6. }

示例4:

  1. /**
  2. * Execute Http request and response code
  3. * @param request - HTTP Request
  4. * @param expectedCode - expected response code
  5. * @return - response in JSONObject
  6. */
  7. public JSON query(HttpRequestBase request, int expectedCode) throws IOException {
  8. log.info("Requesting: " + request);
  9. addRequiredHeader(request);
  10. HttpParams requestParams = request.getParams();
  11. requestParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT * 1000);
  12. requestParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT * 1000);
  13. synchronized (httpClient) {
  14. String response;
  15. try {
  16. HttpResponse result = httpClient.execute(request);
  17. int statusCode = result.getStatusLine().getStatusCode();
  18. response = getResponseEntity(result);
  19. if (statusCode != expectedCode) {
  20. notifier.notifyAbout("Response with code " + statusCode + ": " + extractErrorMessage(response));
  21. throw new IOException("API responded with wrong status code: " + statusCode);
  22. } else {
  23. log.debug("Response: " + response);
  24. }
  25. } finally {
  26. request.abort();
  27. }
  28. if (response == null || response.isEmpty()) {
  29. return JSONNull.getInstance();
  30. } else {
  31. return JSONSerializer.toJSON(response, new JsonConfig());
  32. }
  33. }
  34. }

示例5:

  1. /**
  2. * Try to send a message to harbormaster
  3. * @param unitResults the unit testing results to send
  4. * @param coverage the coverage data to send
  5. * @return false if an error was encountered
  6. */
  7. private boolean sendMessage(UnitResults unitResults, Map<String, String> coverage, LintResults lintResults) throws IOException, ConduitAPIException {
  8. JSONObject result = diffClient.sendHarbormasterMessage(phid, harbormasterSuccess, unitResults, coverage, lintResults);
  9. if (result.containsKey("error_info") && !(result.get("error_info") instanceof JSONNull)) {
  10. info(String.format("Error from Harbormaster: %s", result.getString("error_info")));
  11. failTask();
  12. return false;
  13. } else {
  14. this.result = Result.SUCCESS;
  15. }
  16. return true;
  17. }

示例6:

  1. /**
  2. * Return the local branch name
  3. *
  4. * @return the name of the branch, or unknown
  5. */
  6. public String getBranch() {
  7. Object branchName = rawJSON.get("branch");
  8. if (branchName instanceof JSONNull) {
  9. return "(none)";
  10. }
  11. try {
  12. return (String) branchName;
  13. } catch (ClassCastException e) {
  14. return "(unknown)";
  15. }
  16. }

示例7:

  1. @Test
  2. public void testGetBranchWithEmptyResponse() throws Exception {
  3. JSONObject empty = new JSONObject();
  4. empty.put("branch", JSONNull.getInstance());
  5. Differential diff = new Differential(empty);
  6. assertEquals("(none)", diff.getBranch());
  7. }

示例8:

  1. public final MapTuple put(final String field, final @Nullable Object obj) {
  2. if (obj == null) {
  3. /*
  4. * We need this because of joins. We don't use Java null because
  5. * too many libraries either return null to represent absence,
  6. * don't allow null entries, or use null to represent something else.
  7. */
  8. _values.put(field, JSONNull.getInstance());
  9. } else {
  10. _values.put(field, obj);
  11. }
  12. return this; // for chaining
  13. }

示例9:

  1. @SuppressWarnings("unchecked")
  2. public static void removeNulls(JSONObject json) {
  3. Iterator<String> keyIter = json.keys();
  4. while(keyIter.hasNext()) {
  5. String key = keyIter.next().toString();
  6. if (json.get(key) instanceof JSONNull || json.get(key) == null) {
  7. json.remove(key);
  8. }
  9. }
  10. }

示例10:

  1. public static JSONObject parseObj(String o) {
  2. JSON j = JSONSerializer.toJSON(o);
  3. if (j instanceof JSONNull) {
  4. return null;
  5. } else {
  6. return (JSONObject)j;
  7. }
  8. }

示例11:

  1. public static JSONArray parseArray(String o) {
  2. JSON j = JSONSerializer.toJSON(o);
  3. if (j instanceof JSONNull) {
  4. return null;
  5. } else {
  6. return (JSONArray)j;
  7. }
  8. }

示例12:

  1. public static String getStringForType(Object value) {
  2. if(value instanceof String) {
  3. return "\""+ StringEscapeUtils.escapeJson((String)value) +"\"";
  4. } else if(value instanceof Integer || value instanceof Float || value instanceof Long || value instanceof Boolean) {
  5. return value.toString();
  6. } else if(value instanceof JSONArray) {
  7. return JSONUtil.toString((JSONArray) value);
  8. } else if(value instanceof JSONObject) {
  9. return JSONUtil.toString((JSONObject) value);
  10. } else if(value instanceof JSONNull || value == null) {
  11. return "null";
  12. } else {
  13. throw new RuntimeException("unrecognized type for value: "+value+"["+value.getClass().getName()+"]");
  14. }
  15. }

示例13:

  1. @Override
  2. public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
  3. // To persist global configuration information,
  4. // set that to properties and call save().
  5. Object identities = formData.get("chefIdentity");
  6. if (!JSONNull.getInstance().equals(identities)) {
  7. chefIdentities = req.bindJSONToList(ChefIdentity.class, identities);
  8. } else {
  9. chefIdentities = null;
  10. }
  11. save();
  12. return super.configure(req,formData);
  13. }

示例14:

  1. @Override
  2. public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
  3. Object s = json.get("servers");
  4. if (!JSONNull.getInstance().equals(s)) {
  5. servers = req.bindJSONToList(Server.class, s);
  6. } else {
  7. servers = null;
  8. }
  9. publicKeyPath = json.getString("publicKeyPath");
  10. save();
  11. return super.configure(req, json);
  12. }

示例15:

  1. @SuppressWarnings("rawtypes")
  2. public Vertex convertElement(Object json, Network network) {
  3. try {
  4. if (json == null) {
  5. return null;
  6. }
  7. Vertex object = null;
  8. if (json instanceof JSONObject) {
  9. object = network.createVertex();
  10. for (Iterator iterator = ((JSONObject)json).keys(); iterator.hasNext(); ) {
  11. String name = (String)iterator.next();
  12. Object value = ((JSONObject)json).get(name);
  13. if (value == null) {
  14. continue;
  15. }
  16. Primitive key = new Primitive(name);
  17. Vertex target = convertElement(value, network);
  18. object.addRelationship(key, target);
  19. }
  20. } else if (json instanceof JSONArray) {
  21. object = network.createInstance(Primitive.ARRAY);
  22. JSONArray array = (JSONArray)json;
  23. for (int index = 0; index < array.size(); index++) {
  24. Vertex element = convertElement(array.get(index), network);
  25. object.addRelationship(Primitive.ELEMENT, element, index);
  26. }
  27. } else if (json instanceof JSONNull) {
  28. object = network.createInstance(Primitive.NULL);
  29. } else if (json instanceof String) {
  30. object = network.createVertex(json);
  31. } else if (json instanceof Number) {
  32. object = network.createVertex(json);
  33. } else if (json instanceof Date) {
  34. object = network.createVertex(json);
  35. } else if (json instanceof Boolean) {
  36. object = network.createVertex(json);
  37. } else {
  38. log("Unknown JSON object", Level.INFO, json);
  39. }
  40. return object;
  41. } catch (Exception exception) {
  42. log(exception);
  43. return null;
  44. }
  45. }

示例16:

  1. /**
  2. * Initialises the value tuple.
  3. *
  4. * @param type
  5. * @param value
  6. */
  7. public ValueTuple(String type, Object value) {
  8. this.type = type;
  9. this.value = (value != null ? value : JSONNull.getInstance());
  10. }

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载