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.

139 lines
6.3KB

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