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.

137 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. //==============================================================================
  69. /** Returns a string which contains a JSON-formatted representation of the var object.
  70. If allOnOneLine is true, the result will be compacted into a single line of text
  71. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  72. The maximumDecimalPlaces parameter determines the precision of floating point numbers
  73. in scientific notation.
  74. @see writeToStream
  75. */
  76. static String toString (const var& objectToFormat,
  77. bool allOnOneLine = false,
  78. int maximumDecimalPlaces = 15);
  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. The maximumDecimalPlaces parameter determines the precision of floating point numbers
  89. in scientific notation.
  90. @see toString
  91. */
  92. static void writeToStream (OutputStream& output,
  93. const var& objectToFormat,
  94. bool allOnOneLine = false,
  95. int maximumDecimalPlaces = 15);
  96. /** Returns a version of a string with any extended characters escaped. */
  97. static String escapeString (StringRef);
  98. /** Parses a quoted string-literal in JSON format, returning the un-escaped result in the
  99. result parameter, and an error message in case the content was illegal.
  100. This advances the text parameter, leaving it positioned after the closing quote.
  101. */
  102. static Result parseQuotedString (String::CharPointerType& text, var& result);
  103. private:
  104. //==============================================================================
  105. JSON() = delete; // This class can't be instantiated - just use its static methods.
  106. };
  107. } // namespace juce