/hudson-core/src/main/java/hudson/util/JSONCanonicalUtils.java

http://github.com/hudson/hudson · Java · 160 lines · 109 code · 11 blank · 40 comment · 27 complexity · f30d48e3d6f59d3f13e2f5bd002966c5 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2011, Oracle Corporation
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.util;
  25. import java.io.IOException;
  26. import java.io.Writer;
  27. import java.util.Iterator;
  28. import net.sf.json.JSON;
  29. import net.sf.json.JSONArray;
  30. import net.sf.json.JSONFunction;
  31. import net.sf.json.JSONNull;
  32. import net.sf.json.JSONObject;
  33. import net.sf.json.JSONString;
  34. import net.sf.json.util.JSONUtils;
  35. /**
  36. * Json utils for canonical writing.
  37. */
  38. public class JSONCanonicalUtils {
  39. /**
  40. * Write JSON object to writer in canonical form.
  41. *
  42. * @param json JSONObject
  43. * @param writer Writer
  44. * @throws IOException if any
  45. */
  46. public static void write(JSONObject json, Writer writer) throws IOException {
  47. if (json.isNullObject()) {
  48. writer.write(JSONNull.getInstance().toString());
  49. return;
  50. }
  51. boolean b = false;
  52. Iterator keys = json.keys();
  53. writer.write('{');
  54. while (keys.hasNext()) {
  55. if (b) {
  56. writer.write(',');
  57. }
  58. Object k = keys.next();
  59. writer.write(JSONUtils.quote(k.toString()));
  60. writer.write(':');
  61. Object v = json.get(k);
  62. if (v instanceof JSON) {
  63. write((JSON) v, writer);
  64. } else {
  65. writer.write(toCanonical(v));
  66. }
  67. b = true;
  68. }
  69. writer.write('}');
  70. }
  71. /**
  72. * Write JSON array to writer in canonical form.
  73. *
  74. * @param json JSONArray
  75. * @param writer Writer
  76. * @throws IOException
  77. */
  78. public static void write(JSONArray json, Writer writer) throws IOException {
  79. boolean b = false;
  80. int len = json.size();
  81. writer.write('[');
  82. for (int i = 0; i < len; i += 1) {
  83. if (b) {
  84. writer.write(',');
  85. }
  86. Object v = json.get(i);
  87. if (v instanceof JSON) {
  88. write((JSON) v, writer);
  89. } else {
  90. writer.write(toCanonical(v));
  91. }
  92. b = true;
  93. }
  94. writer.write(']');
  95. }
  96. private static void write(JSON o, Writer writer) throws IOException {
  97. if (o instanceof JSONObject) {
  98. write((JSONObject) o, writer);
  99. } else if (o instanceof JSONArray) {
  100. write((JSONArray) o, writer);
  101. }
  102. }
  103. private static String toCanonical(Object value) throws IOException {
  104. if (value == null || JSONUtils.isNull(value)) {
  105. return "null";
  106. }
  107. if (value instanceof JSONFunction) {
  108. return value.toString();
  109. }
  110. if (value instanceof JSONString) {
  111. return ((JSONString) value).toJSONString();
  112. }
  113. if (value instanceof Number) {
  114. return JSONUtils.numberToString((Number) value).toLowerCase();
  115. }
  116. if (value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray) {
  117. return value.toString();
  118. }
  119. return quoteCanonical(value.toString());
  120. }
  121. private static String quoteCanonical(String s) {
  122. if (s == null || s.length() == 0) {
  123. return "\"\"";
  124. }
  125. int len = s.length();
  126. StringBuilder sb = new StringBuilder(len + 4);
  127. sb.append('"');
  128. for (int i = 0; i < len; i += 1) {
  129. char c = s.charAt(i);
  130. switch (c) {
  131. case '\\':
  132. case '"':
  133. sb.append('\\');
  134. sb.append(c);
  135. break;
  136. default:
  137. if (c < ' ') {
  138. String t = "000" + Integer.toHexString(c);
  139. sb.append("\\u")
  140. .append(t.substring(t.length() - 4));
  141. } else {
  142. sb.append(c);
  143. }
  144. }
  145. }
  146. sb.append('"');
  147. return sb.toString();
  148. }
  149. }