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.

135 lines
6.2KB

  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. #pragma once
  24. //==============================================================================
  25. /**
  26. Contains static methods for converting JSON-formatted text to and from var objects.
  27. The var class is structurally compatible with JSON-formatted data, so these
  28. functions allow you to parse JSON into a var object, and to convert a var
  29. object to JSON-formatted text.
  30. @see var
  31. */
  32. class JUCE_API JSON
  33. {
  34. public:
  35. //==============================================================================
  36. /** Parses a string of JSON-formatted text, and returns a result code containing
  37. any parse errors.
  38. This will return the parsed structure in the parsedResult parameter, and will
  39. return a Result object to indicate whether parsing was successful, and if not,
  40. it will contain an error message.
  41. If you're not interested in the error message, you can use one of the other
  42. shortcut parse methods, which simply return a var() if the parsing fails.
  43. Note that this will only parse valid JSON, which means that the item given must
  44. be either an object or an array definition. If you want to also be able to parse
  45. any kind of primitive JSON object, use the fromString() method.
  46. */
  47. static Result parse (const String& text, var& parsedResult);
  48. /** Attempts to parse some JSON-formatted text, and returns the result as a var object.
  49. If the parsing fails, this simply returns var() - if you need to find out more
  50. detail about the parse error, use the alternative parse() method which returns a Result.
  51. Note that this will only parse valid JSON, which means that the item given must
  52. be either an object or an array definition. If you want to also be able to parse
  53. any kind of primitive JSON object, use the fromString() method.
  54. */
  55. static var parse (const String& text);
  56. /** Attempts to parse some JSON-formatted text from a file, and returns the result
  57. as a var object.
  58. Note that this is just a short-cut for reading the entire file into a string and
  59. parsing the result.
  60. If the parsing fails, this simply returns var() - if you need to find out more
  61. detail about the parse error, use the alternative parse() method which returns a Result.
  62. */
  63. static var parse (const File& file);
  64. /** Attempts to parse some JSON-formatted text from a stream, and returns the result
  65. as a var object.
  66. Note that this is just a short-cut for reading the entire stream into a string and
  67. parsing the result.
  68. If the parsing fails, this simply returns var() - if you need to find out more
  69. detail about the parse error, use the alternative parse() method which returns a Result.
  70. */
  71. static var parse (InputStream& input);
  72. //==============================================================================
  73. /** Returns a string which contains a JSON-formatted representation of the var object.
  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 writeToStream
  77. */
  78. static String toString (const var& objectToFormat,
  79. bool allOnOneLine = false);
  80. /** Parses a string that was created with the toString() method.
  81. This is slightly different to the parse() methods because they will reject primitive
  82. values and only accept array or object definitions, whereas this method will handle
  83. either.
  84. */
  85. static var fromString (StringRef);
  86. /** Writes a JSON-formatted representation of the var object to the given stream.
  87. If allOnOneLine is true, the result will be compacted into a single line of text
  88. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  89. @see toString
  90. */
  91. static void writeToStream (OutputStream& output,
  92. const var& objectToFormat,
  93. bool allOnOneLine = false);
  94. /** Returns a version of a string with any extended characters escaped. */
  95. static String escapeString (StringRef);
  96. /** Parses a quoted string-literal in JSON format, returning the un-escaped result in the
  97. result parameter, and an error message in case the content was illegal.
  98. This advances the text parameter, leaving it positioned after the closing quote.
  99. */
  100. static Result parseQuotedString (String::CharPointerType& text, var& result);
  101. private:
  102. //==============================================================================
  103. JSON() JUCE_DELETED_FUNCTION; // This class can't be instantiated - just use its static methods.
  104. };