Title: Implement opt/get methods for accumulated values · Issue #551 · stleary/JSON-java · GitHub
Open Graph Title: Implement opt/get methods for accumulated values · Issue #551 · stleary/JSON-java
X Title: Implement opt/get methods for accumulated values · Issue #551 · stleary/JSON-java
Description: Currently we have an accumulate method on JSONObject, however, we don't have an easy way to get back out a consistent value (i.e. JSONArray). One area where this is most painful is in XML to JSON conversion where some XML documents may c...
Open Graph Description: Currently we have an accumulate method on JSONObject, however, we don't have an easy way to get back out a consistent value (i.e. JSONArray). One area where this is most painful is in XML to JSON c...
X Description: Currently we have an accumulate method on JSONObject, however, we don't have an easy way to get back out a consistent value (i.e. JSONArray). One area where this is most painful is in XML to JS...
Opengraph URL: https://github.com/stleary/JSON-java/issues/551
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Implement opt/get methods for accumulated values","articleBody":"Currently we have an `accumulate` method on `JSONObject`, however, we don't have an easy way to get back out a consistent value (i.e. JSONArray).\r\n\r\nOne area where this is most painful is in XML to JSON conversion where some XML documents may contain a single element, while other documents may contain many.\r\n\r\nExample 1:\r\n```xml\r\n\u003cStudent>\r\n \u003cname>jack\u003c/name>\r\n \u003csubjects>\r\n \u003cclass>english\u003c/class>\u003cgrade>98\u003c/grade>\r\n \u003c/subjects>\r\n \u003csubjects>\r\n \u003cclass>english\u003c/class>\u003cgrade>98\u003c/grade>\r\n \u003c/subjects>\r\n\u003c/Student>\r\n```\r\n\r\nExample 2:\r\n```xml\r\n\u003cStudent>\r\n \u003cname>jack\u003c/name>\r\n \u003csubjects>\r\n \u003cclass>english\u003c/class>\u003cgrade>98\u003c/grade>\r\n \u003c/subjects>\r\n\u003c/Student>\r\n```\r\n\r\nThis currently leads to very messy code that has to check the type of value parsed:\r\n```java\r\nvoid someFunction() {\r\n JSONObject jo = XML.toJSONObject(someXmlSource);\r\n JSONObject student = jo.getJSONObject(\"Student\");\r\n if ( student.optJSONArray(\"subjects\") ==null ) {\r\n processSubject(student.optJSONObject(\"subjects\"));\r\n } else {\r\n processSubjects(student.optJSONArray(\"subjects\"));\r\n }\r\n}\r\n\r\nvoid processSubjects(JSONArray subjects) {\r\n if (subjects == null || subjects.length() == 0) return;\r\n for(int i=0; i\u003csubjects.length; i++) {\r\n processSubject(subjects.getJSONObject(i));\r\n }\r\n}\r\n\r\nvoid processSubject(JSONObject subject) {\r\n // some implementation\r\n}\r\n```\r\n\r\nIt would be nice if there was a convenience function in the JSONObject to get values that are accumulated:\r\n```java\r\n// In JSONObject\r\n\r\n/**\r\n* Always returns a JSONArray, If the key doesn't exist or the value is `null`, the array will be empty.\r\n* If the key holds a non-array/non-collection value, then it is placed in a JSONArray\r\n* before returning.\r\n* If the value is a JSONArray, then a copy is returned.\r\n* If the value is directly convertible to a JSONArray (i.e. a JAVA Array or Collection), then it is\r\n* converted to a JSONArray.\r\n* @param key The key in the JSONObject to fetch\r\n* @returns a JSONArray regardless of the value of the key.\r\n*/\r\npublic JSONArray optAccumulated(String key) {\r\n Object o = opt(key);\r\n if (JSONObject.NULL.equals(o)) {\r\n return new JSONArray();\r\n }\r\n if (o instanceof JSONArray) {\r\n return new JSONArray(((JSONArray)o).myArrayList); // maybe create a copy constructor and/or extend putAll\r\n }\r\n if (o instanceof Collection || o.getClass().isArray()) {\r\n return new JSONArray(o);\r\n }\r\n JSONArray ret = new JSONArray();\r\n ret.put(o);\r\n return ret;\r\n}\r\n\r\n\r\n/**\r\n* Always returns a JSONArray, If the key value is `null`, the array will be empty.\r\n* If the key holds a non-array/non-collection value, then it is placed in a JSONArray\r\n* before returning.\r\n* If the value is a JSONArray, then a copy is returned.\r\n* If the value is directly convertible to a JSONArray (i.e. a JAVA Array or Collection), then it is\r\n* converted to a JSONArray.\r\n* @param key The key in the JSONObject to fetch\r\n* @returns a JSONArray regardless of the value of the key.\r\n* @throws JSONException Thrown when the key does not exist\r\n*/\r\npublic JSONArray getAccumulated(String key) throws JSONException {\r\n Object o = get(key);\r\n if (JSONObject.NULL.equals(o)) {\r\n return new JSONArray();\r\n }\r\n if (o instanceof JSONArray) {\r\n return new JSONArray(((JSONArray)o).myArrayList); // maybe create a copy constructor and/or extend putAll\r\n }\r\n if (o instanceof Collection || o.getClass().isArray()) {\r\n return new JSONArray(o);\r\n }\r\n JSONArray ret = new JSONArray();\r\n ret.put(o);\r\n return ret;\r\n}\r\n```\r\n\r\nSee also #550","author":{"url":"https://github.com/johnjaylward","@type":"Person","name":"johnjaylward"},"datePublished":"2020-07-27T14:05:10Z","url":"https://github.com/stleary/JSON-java/issues/551"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:71d43e51-a59b-5923-233c-d8b12dfd24f6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A54C:19E497:2A51B8:3828CC:6AFD16C7 |
| html-safe-nonce | 8f753252774144c043e446b99345786a780a7dd3aeb74d3e65321f1e4bb2d693 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTRDOjE5RTQ5NzoyQTUxQjg6MzgyOENDOjZBRkQxNkM3IiwidmlzaXRvcl9pZCI6IjM1MDczMDYwODg5NDA4ODkwNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 99f697e50441ab4e2d4c3ba7596669754458073505493221b6dc44a90a66f4e9 |
| hovercard-subject-tag | issue:666321911 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/stleary/JSON-java/551/issue_layout |
| twitter:image | https://opengraph.githubassets.com/60fa39293b2dc21bdb771dba0aa6d2bce95e248e8f266a371c9028ee0e8848ba/stleary/JSON-java/issues/551 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/60fa39293b2dc21bdb771dba0aa6d2bce95e248e8f266a371c9028ee0e8848ba/stleary/JSON-java/issues/551 |
| og:image:alt | Currently we have an accumulate method on JSONObject, however, we don't have an easy way to get back out a consistent value (i.e. JSONArray). One area where this is most painful is in XML to JSON c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | johnjaylward |
| hostname | github.com |
| expected-hostname | github.com |
| None | 065128366c233a9d57ff68ab6c96e25b99dd1b0ab70d23b70873d3dd6b5e4f19 |
| turbo-cache-control | no-preview |
| go-import | github.com/stleary/JSON-java git https://github.com/stleary/JSON-java.git |
| octolytics-dimension-user_id | 5107558 |
| octolytics-dimension-user_login | stleary |
| octolytics-dimension-repository_id | 1187853 |
| octolytics-dimension-repository_nwo | stleary/JSON-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1187853 |
| octolytics-dimension-repository_network_root_nwo | stleary/JSON-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 941021edf0c2ec58298f4a2c8f27ae507d129c92 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width