PageRenderTime 3816ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/rythmengine/utils/JSONWrapper.java

http://github.com/greenlaw110/Rythm
Java | 106 lines | 38 code | 9 blank | 59 comment | 4 complexity | 102ea22ed3a0deb7a664fc3111ac9d16 MD5 | raw file
  1. /**
  2. * Copyright (C) 2013-2016 The Rythm Engine project
  3. * for LICENSE and other details see:
  4. * https://github.com/rythmengine/rythmengine
  5. */
  6. package org.rythmengine.utils;
  7. /*-
  8. * #%L
  9. * Rythm Template Engine
  10. * %%
  11. * Copyright (C) 2017 - 2021 OSGL (Open Source General Library)
  12. * %%
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. * #L%
  25. */
  26. import com.alibaba.fastjson.JSON;
  27. import com.alibaba.fastjson.JSONArray;
  28. import com.alibaba.fastjson.JSONObject;
  29. import java.util.List;
  30. import java.util.Map;
  31. /**
  32. * Define a type to wrap a JSON string
  33. */
  34. public class JSONWrapper {
  35. private JSON j_;
  36. /**
  37. * Construct a JSONWrapper with a JSON string. If the string is not valid JSON, then
  38. * a RuntimeException will thrown out
  39. *
  40. * @param str
  41. */
  42. public JSONWrapper(String str) {
  43. if (S.empty(str)) throw new IllegalArgumentException("empty json str");
  44. Object o;
  45. try {
  46. o = JSON.parse(str);
  47. } catch (com.alibaba.fastjson.JSONException e) {
  48. throw new RuntimeException("Invalid JSON string: " + str);
  49. }
  50. if (o instanceof JSON) {
  51. j_ = (JSON) o;
  52. } else {
  53. throw new RuntimeException("JSON string parse to unknown object type: " + o.getClass());
  54. }
  55. }
  56. /**
  57. * Return true if the underline JSON data is an array
  58. *
  59. * @return true if the JSON data is an array
  60. */
  61. public boolean isArray() {
  62. return j_ instanceof JSONArray;
  63. }
  64. /**
  65. * Return a List of object contained in the JSON array.
  66. * <p/>
  67. * <p></p>If the data is not a JSON array then a ClassCastException will
  68. * be thrown out</p>
  69. *
  70. * @return the List of objects
  71. */
  72. public List<Object> getArray() {
  73. return (JSONArray) j_;
  74. }
  75. /**
  76. * Return a Map of String and object contained in the JSON object.
  77. * <p/>
  78. * <p></p>If the data is a JSON array then a ClassCastException will
  79. * be thrown out</p>
  80. *
  81. * @return the mapped json attributes
  82. */
  83. public Map<String, Object> getObject() {
  84. return (JSONObject) j_;
  85. }
  86. /**
  87. * Parse the string and return the JSONWrapper
  88. * @param s
  89. * @return JSON wrapper of the string
  90. */
  91. public static JSONWrapper wrap(String s) {
  92. if (S.empty(s)) {
  93. return null;
  94. }
  95. return new JSONWrapper(s);
  96. }
  97. }