Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JSONStringer.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package org.json;
  2. /*
  3. Copyright (c) 2006 JSON.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. The Software shall be used for Good, not Evil.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. import java.io.StringWriter;
  22. /**
  23. * JSONStringer provides a quick and convenient way of producing JSON text.
  24. * The texts produced strictly conform to JSON syntax rules. No whitespace is
  25. * added, so the results are ready for transmission or storage. Each instance of
  26. * JSONStringer can produce one JSON text.
  27. * <p>
  28. * A JSONStringer instance provides a <code>value</code> method for appending
  29. * values to the
  30. * text, and a <code>key</code>
  31. * method for adding keys before values in objects. There are <code>array</code>
  32. * and <code>endArray</code> methods that make and bound array values, and
  33. * <code>object</code> and <code>endObject</code> methods which make and bound
  34. * object values. All of these methods return the JSONWriter instance,
  35. * permitting cascade style. For example, <pre>
  36. * myString = new JSONStringer()
  37. * .object()
  38. * .key("JSON")
  39. * .value("Hello, World!")
  40. * .endObject()
  41. * .toString();</pre> which produces the string <pre>
  42. * {"JSON":"Hello, World!"}</pre>
  43. * <p>
  44. * The first method called must be <code>array</code> or <code>object</code>.
  45. * There are no methods for adding commas or colons. JSONStringer adds them for
  46. * you. Objects and arrays can be nested up to 20 levels deep.
  47. * <p>
  48. * This can sometimes be easier than using a JSONObject to build a string.
  49. * @author JSON.org
  50. * @version 2008-09-18
  51. */
  52. public class JSONStringer extends JSONWriter {
  53. /**
  54. * Make a fresh JSONStringer. It can be used to build one JSON text.
  55. */
  56. public JSONStringer() {
  57. super(new StringWriter());
  58. }
  59. /**
  60. * Return the JSON text. This method is used to obtain the product of the
  61. * JSONStringer instance. It will return <code>null</code> if there was a
  62. * problem in the construction of the JSON text (such as the calls to
  63. * <code>array</code> were not properly balanced with calls to
  64. * <code>endArray</code>).
  65. * @return The JSON text.
  66. */
  67. public String toString() {
  68. return this.mode == 'd' ? this.writer.toString() : null;
  69. }
  70. }