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.

119 lines
5.1KB

  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_JUCEHEADER__
  22. #define __JUCE_JSON_JUCEHEADER__
  23. #include "../misc/juce_Result.h"
  24. #include "../containers/juce_Variant.h"
  25. class InputStream;
  26. class OutputStream;
  27. class File;
  28. //==============================================================================
  29. /**
  30. Contains static methods for converting JSON-formatted text to and from var objects.
  31. The var class is structurally compatible with JSON-formatted data, so these
  32. functions allow you to parse JSON into a var object, and to convert a var
  33. object to JSON-formatted text.
  34. @see var
  35. */
  36. class JUCE_API JSON
  37. {
  38. public:
  39. //==============================================================================
  40. /** Parses a string of JSON-formatted text, and returns a result code containing
  41. any parse errors.
  42. This will return the parsed structure in the parsedResult parameter, and will
  43. return a Result object to indicate whether parsing was successful, and if not,
  44. it will contain an error message.
  45. If you're not interested in the error message, you can use one of the other
  46. shortcut parse methods, which simply return a var::null if the parsing fails.
  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::null - if you need to find out more
  51. detail about the parse error, use the alternative parse() method which returns a Result.
  52. */
  53. static var parse (const String& text);
  54. /** Attempts to parse some JSON-formatted text from a file, and returns the result
  55. as a var object.
  56. Note that this is just a short-cut for reading the entire file into a string and
  57. parsing the result.
  58. If the parsing fails, this simply returns var::null - if you need to find out more
  59. detail about the parse error, use the alternative parse() method which returns a Result.
  60. */
  61. static var parse (const File& file);
  62. /** Attempts to parse some JSON-formatted text from a stream, and returns the result
  63. as a var object.
  64. Note that this is just a short-cut for reading the entire stream into a string and
  65. parsing the result.
  66. If the parsing fails, this simply returns var::null - if you need to find out more
  67. detail about the parse error, use the alternative parse() method which returns a Result.
  68. */
  69. static var parse (InputStream& input);
  70. //==============================================================================
  71. /** Returns a string which contains a JSON-formatted representation of the var object.
  72. If allOnOneLine is true, the result will be compacted into a single line of text
  73. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  74. @see writeToStream
  75. */
  76. static String toString (const var& objectToFormat,
  77. bool allOnOneLine = false);
  78. /** Writes a JSON-formatted representation of the var object to the given stream.
  79. If allOnOneLine is true, the result will be compacted into a single line of text
  80. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  81. @see toString
  82. */
  83. static void writeToStream (OutputStream& output,
  84. const var& objectToFormat,
  85. bool allOnOneLine = false);
  86. private:
  87. //==============================================================================
  88. JSON(); // This class can't be instantiated - just use its static methods.
  89. };
  90. #endif // __JUCE_JSON_JUCEHEADER__