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.

116 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_JSON_JUCEHEADER__
  19. #define __JUCE_JSON_JUCEHEADER__
  20. #include "../misc/juce_Result.h"
  21. #include "../containers/juce_Variant.h"
  22. class InputStream;
  23. class OutputStream;
  24. class File;
  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 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::null if the parsing fails.
  44. */
  45. static Result parse (const String& text, var& parsedResult);
  46. /** Attempts to parse some JSON-formatted text, and returns the result as a var object.
  47. If the parsing fails, this simply returns var::null - if you need to find out more
  48. detail about the parse error, use the alternative parse() method which returns a Result.
  49. */
  50. static var parse (const String& text);
  51. /** Attempts to parse some JSON-formatted text from a file, and returns the result
  52. as a var object.
  53. Note that this is just a short-cut for reading the entire file into a string and
  54. parsing the result.
  55. If the parsing fails, this simply returns var::null - if you need to find out more
  56. detail about the parse error, use the alternative parse() method which returns a Result.
  57. */
  58. static var parse (const File& file);
  59. /** Attempts to parse some JSON-formatted text from a stream, and returns the result
  60. as a var object.
  61. Note that this is just a short-cut for reading the entire stream into a string and
  62. parsing the result.
  63. If the parsing fails, this simply returns var::null - if you need to find out more
  64. detail about the parse error, use the alternative parse() method which returns a Result.
  65. */
  66. static var parse (InputStream& input);
  67. //==============================================================================
  68. /** Returns a string which contains a JSON-formatted representation of the var object.
  69. If allOnOneLine is true, the result will be compacted into a single line of text
  70. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  71. @see writeToStream
  72. */
  73. static String toString (const var& objectToFormat,
  74. bool allOnOneLine = false);
  75. /** Writes a JSON-formatted representation of the var object to the given stream.
  76. If allOnOneLine is true, the result will be compacted into a single line of text
  77. with no carriage-returns. If false, it will be laid-out in a more human-readable format.
  78. @see toString
  79. */
  80. static void writeToStream (OutputStream& output,
  81. const var& objectToFormat,
  82. bool allOnOneLine = false);
  83. private:
  84. //==============================================================================
  85. JSON(); // This class can't be instantiated - just use its static methods.
  86. };
  87. #endif // __JUCE_JSON_JUCEHEADER__