json.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* JSON trees
  2. Copyright (C) 2017-2020 Free Software Foundation, Inc.
  3. Contributed by David Malcolm <dmalcolm@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_JSON_H
  17. #define GCC_JSON_H
  18. /* Implementation of JSON, a lightweight data-interchange format.
  19. See http://www.json.org/
  20. and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
  21. and https://tools.ietf.org/html/rfc7159
  22. Supports creating a DOM-like tree of json::value *, and then dumping
  23. json::value * to text. */
  24. namespace json
  25. {
  26. /* Forward decls of json::value and its subclasses (using indentation
  27. to denote inheritance. */
  28. class value;
  29. class object;
  30. class array;
  31. class float_number;
  32. class integer_number;
  33. class string;
  34. class literal;
  35. /* An enum for discriminating the subclasses of json::value. */
  36. enum kind
  37. {
  38. /* class json::object. */
  39. JSON_OBJECT,
  40. /* class json::array. */
  41. JSON_ARRAY,
  42. /* class json::integer_number. */
  43. JSON_INTEGER,
  44. /* class json::float_number. */
  45. JSON_FLOAT,
  46. /* class json::string. */
  47. JSON_STRING,
  48. /* class json::literal uses these three values to identify the
  49. particular literal. */
  50. JSON_TRUE,
  51. JSON_FALSE,
  52. JSON_NULL
  53. };
  54. /* Base class of JSON value. */
  55. class value
  56. {
  57. public:
  58. virtual ~value () {}
  59. virtual enum kind get_kind () const = 0;
  60. virtual void print (pretty_printer *pp) const = 0;
  61. void dump (FILE *) const;
  62. };
  63. /* Subclass of value for objects: an unordered collection of
  64. key/value pairs. */
  65. class object : public value
  66. {
  67. public:
  68. ~object ();
  69. enum kind get_kind () const FINAL OVERRIDE { return JSON_OBJECT; }
  70. void print (pretty_printer *pp) const FINAL OVERRIDE;
  71. void set (const char *key, value *v);
  72. value *get (const char *key) const;
  73. private:
  74. typedef hash_map <char *, value *,
  75. simple_hashmap_traits<nofree_string_hash, value *> > map_t;
  76. map_t m_map;
  77. };
  78. /* Subclass of value for arrays. */
  79. class array : public value
  80. {
  81. public:
  82. ~array ();
  83. enum kind get_kind () const FINAL OVERRIDE { return JSON_ARRAY; }
  84. void print (pretty_printer *pp) const FINAL OVERRIDE;
  85. void append (value *v);
  86. private:
  87. auto_vec<value *> m_elements;
  88. };
  89. /* Subclass of value for floating-point numbers. */
  90. class float_number : public value
  91. {
  92. public:
  93. float_number (double value) : m_value (value) {}
  94. enum kind get_kind () const FINAL OVERRIDE { return JSON_FLOAT; }
  95. void print (pretty_printer *pp) const FINAL OVERRIDE;
  96. double get () const { return m_value; }
  97. private:
  98. double m_value;
  99. };
  100. /* Subclass of value for integer-valued numbers. */
  101. class integer_number : public value
  102. {
  103. public:
  104. integer_number (long value) : m_value (value) {}
  105. enum kind get_kind () const FINAL OVERRIDE { return JSON_INTEGER; }
  106. void print (pretty_printer *pp) const FINAL OVERRIDE;
  107. long get () const { return m_value; }
  108. private:
  109. long m_value;
  110. };
  111. /* Subclass of value for strings. */
  112. class string : public value
  113. {
  114. public:
  115. string (const char *utf8);
  116. ~string () { free (m_utf8); }
  117. enum kind get_kind () const FINAL OVERRIDE { return JSON_STRING; }
  118. void print (pretty_printer *pp) const FINAL OVERRIDE;
  119. const char *get_string () const { return m_utf8; }
  120. private:
  121. char *m_utf8;
  122. };
  123. /* Subclass of value for the three JSON literals "true", "false",
  124. and "null". */
  125. class literal : public value
  126. {
  127. public:
  128. literal (enum kind kind) : m_kind (kind) {}
  129. /* Construct literal for a boolean value. */
  130. literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {}
  131. enum kind get_kind () const FINAL OVERRIDE { return m_kind; }
  132. void print (pretty_printer *pp) const FINAL OVERRIDE;
  133. private:
  134. enum kind m_kind;
  135. };
  136. } // namespace json
  137. #endif /* GCC_JSON_H */