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.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.vaadin.external.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. The
  24. * texts produced strictly conform to JSON syntax rules. No whitespace is added,
  25. * 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 text, and a <code>key</code> method for adding keys before
  30. * values in objects. There are <code>array</code> and <code>endArray</code>
  31. * methods that make and bound array values, and <code>object</code> and
  32. * <code>endObject</code> methods which make and bound object values. All of
  33. * these methods return the JSONWriter instance, permitting cascade style. For
  34. * example,
  35. *
  36. * <pre>
  37. * myString = new JSONStringer().object().key(&quot;JSON&quot;).value(&quot;Hello, World!&quot;)
  38. * .endObject().toString();
  39. * </pre>
  40. *
  41. * which produces the string
  42. *
  43. * <pre>
  44. * {"JSON":"Hello, World!"}
  45. * </pre>
  46. * <p>
  47. * The first method called must be <code>array</code> or <code>object</code>.
  48. * There are no methods for adding commas or colons. JSONStringer adds them for
  49. * you. Objects and arrays can be nested up to 20 levels deep.
  50. * <p>
  51. * This can sometimes be easier than using a JSONObject to build a string.
  52. *
  53. * @author JSON.org
  54. * @version 2008-09-18
  55. */
  56. public class JSONStringer extends JSONWriter {
  57. /**
  58. * Make a fresh JSONStringer. It can be used to build one JSON text.
  59. */
  60. public JSONStringer() {
  61. super(new StringWriter());
  62. }
  63. /**
  64. * Return the JSON text. This method is used to obtain the product of the
  65. * JSONStringer instance. It will return <code>null</code> if there was a
  66. * problem in the construction of the JSON text (such as the calls to
  67. * <code>array</code> were not properly balanced with calls to
  68. * <code>endArray</code>).
  69. *
  70. * @return The JSON text.
  71. */
  72. public String toString() {
  73. return this.mode == 'd' ? this.writer.toString() : null;
  74. }
  75. }