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.

193 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Contains static methods for converting JSON-formatted text to and from var objects.
  22. The var class is structurally compatible with JSON-formatted data, so these
  23. functions allow you to parse JSON into a var object, and to convert a var
  24. object to JSON-formatted text.
  25. @see var
  26. @tags{Core}
  27. */
  28. class JUCE_API JSON
  29. {
  30. public:
  31. //==============================================================================
  32. /** Parses a string of JSON-formatted text, and returns a result code containing
  33. any parse errors.
  34. This will return the parsed structure in the parsedResult parameter, and will
  35. return a Result object to indicate whether parsing was successful, and if not,
  36. it will contain an error message.
  37. If you're not interested in the error message, you can use one of the other
  38. shortcut parse methods, which simply return a var() if the parsing fails.
  39. Note that this will only parse valid JSON, which means that the item given must
  40. be either an object or an array definition. If you want to also be able to parse
  41. any kind of primitive JSON object, use the fromString() method.
  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() - if you need to find out more
  46. detail about the parse error, use the alternative parse() method which returns a Result.
  47. Note that this will only parse valid JSON, which means that the item given must
  48. be either an object or an array definition. If you want to also be able to parse
  49. any kind of primitive JSON object, use the fromString() method.
  50. */
  51. static var parse (const String& text);
  52. /** Attempts to parse some JSON-formatted text from a file, and returns the result
  53. as a var object.
  54. Note that this is just a short-cut for reading the entire file into a string and
  55. parsing the result.
  56. If the parsing fails, this simply returns var() - if you need to find out more
  57. detail about the parse error, use the alternative parse() method which returns a Result.
  58. */
  59. static var parse (const File& file);
  60. /** Attempts to parse some JSON-formatted text from a stream, and returns the result
  61. as a var object.
  62. Note that this is just a short-cut for reading the entire stream into a string and
  63. parsing the result.
  64. If the parsing fails, this simply returns var() - if you need to find out more
  65. detail about the parse error, use the alternative parse() method which returns a Result.
  66. */
  67. static var parse (InputStream& input);
  68. enum class Spacing
  69. {
  70. none, ///< All optional whitespace should be omitted
  71. singleLine, ///< All output should be on a single line, but with some additional spacing, e.g. after commas and colons
  72. multiLine, ///< Newlines and spaces will be included in the output, in order to make it easy to read for humans
  73. };
  74. /**
  75. Allows formatting var objects as JSON with various configurable options.
  76. */
  77. class [[nodiscard]] FormatOptions
  78. {
  79. public:
  80. /** Returns a copy of this Formatter with the specified spacing. */
  81. FormatOptions withSpacing (Spacing x) const { return withMember (*this, &FormatOptions::spacing, x); }
  82. /** Returns a copy of this Formatter with the specified maximum number of decimal places.
  83. This option determines the precision of floating point numbers in scientific notation.
  84. */
  85. FormatOptions withMaxDecimalPlaces (int x) const { return withMember (*this, &FormatOptions::maxDecimalPlaces, x); }
  86. /** Returns a copy of this Formatter with the specified indent level.
  87. This should only be necessary when serialising multiline nested types.
  88. */
  89. FormatOptions withIndentLevel (int x) const { return withMember (*this, &FormatOptions::indent, x); }
  90. /** Returns the spacing used by this Formatter. */
  91. Spacing getSpacing() const { return spacing; }
  92. /** Returns the maximum number of decimal places used by this Formatter. */
  93. int getMaxDecimalPlaces() const { return maxDecimalPlaces; }
  94. /** Returns the indent level of this Formatter. */
  95. int getIndentLevel() const { return indent; }
  96. private:
  97. Spacing spacing = Spacing::multiLine;
  98. int maxDecimalPlaces = 15;
  99. int indent = 0;
  100. };
  101. //==============================================================================
  102. /** Returns a string which contains a JSON-formatted representation of the var object.
  103. If allOnOneLine is true, the result will be compacted into a single line of text
  104. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  105. The maximumDecimalPlaces parameter determines the precision of floating point numbers
  106. in scientific notation.
  107. @see writeToStream
  108. */
  109. static String toString (const var& objectToFormat,
  110. bool allOnOneLine = false,
  111. int maximumDecimalPlaces = 15);
  112. /** Returns a string which contains a JSON-formatted representation of the var object, using
  113. formatting described by the FormatOptions parameter.
  114. @see writeToStream
  115. */
  116. static String toString (const var& objectToFormat,
  117. const FormatOptions& formatOptions);
  118. /** Parses a string that was created with the toString() method.
  119. This is slightly different to the parse() methods because they will reject primitive
  120. values and only accept array or object definitions, whereas this method will handle
  121. either.
  122. */
  123. static var fromString (StringRef);
  124. /** Writes a JSON-formatted representation of the var object to the given stream.
  125. If allOnOneLine is true, the result will be compacted into a single line of text
  126. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  127. The maximumDecimalPlaces parameter determines the precision of floating point numbers
  128. in scientific notation.
  129. @see toString
  130. */
  131. static void writeToStream (OutputStream& output,
  132. const var& objectToFormat,
  133. bool allOnOneLine = false,
  134. int maximumDecimalPlaces = 15);
  135. /** Writes a JSON-formatted representation of the var object to the given stream, using
  136. formatting described by the FormatOptions parameter.
  137. @see toString
  138. */
  139. static void writeToStream (OutputStream& output,
  140. const var& objectToFormat,
  141. const FormatOptions& formatOptions);
  142. /** Returns a version of a string with any extended characters escaped. */
  143. static String escapeString (StringRef);
  144. /** Parses a quoted string-literal in JSON format, returning the un-escaped result in the
  145. result parameter, and an error message in case the content was illegal.
  146. This advances the text parameter, leaving it positioned after the closing quote.
  147. */
  148. static Result parseQuotedString (String::CharPointerType& text, var& result);
  149. private:
  150. //==============================================================================
  151. JSON() = delete; // This class can't be instantiated - just use its static methods.
  152. };
  153. } // namespace juce