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.

127 lines
5.7KB

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