The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

122 lines
5.5KB

  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. */
  43. static Result parse (const String& text, var& parsedResult);
  44. /** Attempts to parse some JSON-formatted text, and returns the result as a var object.
  45. If the parsing fails, this simply returns var::null - if you need to find out more
  46. detail about the parse error, use the alternative parse() method which returns a Result.
  47. */
  48. static var parse (const String& text);
  49. /** Attempts to parse some JSON-formatted text from a file, and returns the result
  50. as a var object.
  51. Note that this is just a short-cut for reading the entire file into a string and
  52. parsing the result.
  53. If the parsing fails, this simply returns var::null - if you need to find out more
  54. detail about the parse error, use the alternative parse() method which returns a Result.
  55. */
  56. static var parse (const File& file);
  57. /** Attempts to parse some JSON-formatted text from a stream, and returns the result
  58. as a var object.
  59. Note that this is just a short-cut for reading the entire stream into a string and
  60. parsing the result.
  61. If the parsing fails, this simply returns var::null - if you need to find out more
  62. detail about the parse error, use the alternative parse() method which returns a Result.
  63. */
  64. static var parse (InputStream& input);
  65. //==============================================================================
  66. /** Returns a string which contains a JSON-formatted representation of the var object.
  67. If allOnOneLine is true, the result will be compacted into a single line of text
  68. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  69. @see writeToStream
  70. */
  71. static String toString (const var& objectToFormat,
  72. bool allOnOneLine = false);
  73. /** Writes a JSON-formatted representation of the var object to the given stream.
  74. If allOnOneLine is true, the result will be compacted into a single line of text
  75. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  76. @see toString
  77. */
  78. static void writeToStream (OutputStream& output,
  79. const var& objectToFormat,
  80. bool allOnOneLine = false);
  81. /** Returns a version of a string with any extended characters escaped. */
  82. static String escapeString (StringRef);
  83. /** Parses a quoted string in JSON format, returning the un-escaped result in the
  84. result parameter, and an error message in case the content was illegal.
  85. This advances the text parameter, leaving it positioned after the closing quote.
  86. */
  87. static Result parseQuotedString (String::CharPointerType& text, var& result);
  88. private:
  89. //==============================================================================
  90. JSON() JUCE_DELETED_FUNCTION; // This class can't be instantiated - just use its static methods.
  91. };
  92. #endif // JUCE_JSON_H_INCLUDED