juce_JSON.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_JSON_H_INCLUDED
  22. #define JUCE_JSON_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. Contains static methods for converting JSON-formatted text to and from var objects.
  26. The var class is structurally compatible with JSON-formatted data, so these
  27. functions allow you to parse JSON into a var object, and to convert a var
  28. object to JSON-formatted text.
  29. @see var
  30. */
  31. class JUCE_API JSON
  32. {
  33. public:
  34. //==============================================================================
  35. /** Parses a string of JSON-formatted text, and returns a result code containing
  36. any parse errors.
  37. This will return the parsed structure in the parsedResult parameter, and will
  38. return a Result object to indicate whether parsing was successful, and if not,
  39. it will contain an error message.
  40. If you're not interested in the error message, you can use one of the other
  41. shortcut parse methods, which simply return a var::null if the parsing fails.
  42. Note that this will only parse valid JSON, which means that the item given must
  43. be either an object or an array definition. If you want to also be able to parse
  44. any kind of primitive JSON object, use the fromString() method.
  45. */
  46. static Result parse (const String& text, var& parsedResult);
  47. /** Attempts to parse some JSON-formatted text, and returns the result as a var object.
  48. If the parsing fails, this simply returns var::null - if you need to find out more
  49. detail about the parse error, use the alternative parse() method which returns a Result.
  50. Note that this will only parse valid JSON, which means that the item given must
  51. be either an object or an array definition. If you want to also be able to parse
  52. any kind of primitive JSON object, use the fromString() method.
  53. */
  54. static var parse (const String& text);
  55. /** Attempts to parse some JSON-formatted text from a file, and returns the result
  56. as a var object.
  57. Note that this is just a short-cut for reading the entire file into a string and
  58. parsing the result.
  59. If the parsing fails, this simply returns var::null - if you need to find out more
  60. detail about the parse error, use the alternative parse() method which returns a Result.
  61. */
  62. static var parse (const File& file);
  63. /** Attempts to parse some JSON-formatted text from a stream, and returns the result
  64. as a var object.
  65. Note that this is just a short-cut for reading the entire stream into a string and
  66. parsing the result.
  67. If the parsing fails, this simply returns var::null - if you need to find out more
  68. detail about the parse error, use the alternative parse() method which returns a Result.
  69. */
  70. static var parse (InputStream& input);
  71. //==============================================================================
  72. /** Returns a string which contains a JSON-formatted representation of the var object.
  73. If allOnOneLine is true, the result will be compacted into a single line of text
  74. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  75. @see writeToStream
  76. */
  77. static String toString (const var& objectToFormat,
  78. bool allOnOneLine = false);
  79. /** Parses a string that was created with the toString() method.
  80. This is slightly different to the parse() methods because they will reject primitive
  81. values and only accept array or object definitions, whereas this method will handle
  82. either.
  83. */
  84. static var fromString (StringRef);
  85. /** Writes a JSON-formatted representation of the var object to the given stream.
  86. If allOnOneLine is true, the result will be compacted into a single line of text
  87. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  88. @see toString
  89. */
  90. static void writeToStream (OutputStream& output,
  91. const var& objectToFormat,
  92. bool allOnOneLine = false);
  93. /** Returns a version of a string with any extended characters escaped. */
  94. static String escapeString (StringRef);
  95. /** Parses a quoted string-literal in JSON format, returning the un-escaped result in the
  96. result parameter, and an error message in case the content was illegal.
  97. This advances the text parameter, leaving it positioned after the closing quote.
  98. */
  99. static Result parseQuotedString (String::CharPointerType& text, var& result);
  100. private:
  101. //==============================================================================
  102. JSON() JUCE_DELETED_FUNCTION; // This class can't be instantiated - just use its static methods.
  103. };
  104. #endif // JUCE_JSON_H_INCLUDED