json.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* JSON trees
  2. Copyright (C) 2017-2019 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 number;
  32. class string;
  33. class literal;
  34. /* An enum for discriminating the subclasses of json::value. */
  35. enum kind
  36. {
  37. /* class json::object. */
  38. JSON_OBJECT,
  39. /* class json::array. */
  40. JSON_ARRAY,
  41. /* class json::number. */
  42. JSON_NUMBER,
  43. /* class json::string. */
  44. JSON_STRING,
  45. /* class json::literal uses these three values to identify the
  46. particular literal. */
  47. JSON_TRUE,
  48. JSON_FALSE,
  49. JSON_NULL
  50. };
  51. /* Base class of JSON value. */
  52. class value
  53. {
  54. public:
  55. virtual ~value () {}
  56. virtual enum kind get_kind () const = 0;
  57. virtual void print (pretty_printer *pp) const = 0;
  58. void dump (FILE *) const;
  59. };
  60. /* Subclass of value for objects: an unordered collection of
  61. key/value pairs. */
  62. class object : public value
  63. {
  64. public:
  65. ~object ();
  66. enum kind get_kind () const FINAL OVERRIDE { return JSON_OBJECT; }
  67. void print (pretty_printer *pp) const FINAL OVERRIDE;
  68. void set (const char *key, value *v);
  69. private:
  70. typedef hash_map <char *, value *,
  71. simple_hashmap_traits<nofree_string_hash, value *> > map_t;
  72. map_t m_map;
  73. };
  74. /* Subclass of value for arrays. */
  75. class array : public value
  76. {
  77. public:
  78. ~array ();
  79. enum kind get_kind () const FINAL OVERRIDE { return JSON_ARRAY; }
  80. void print (pretty_printer *pp) const FINAL OVERRIDE;
  81. void append (value *v);
  82. private:
  83. auto_vec<value *> m_elements;
  84. };
  85. /* Subclass of value for numbers. */
  86. class number : public value
  87. {
  88. public:
  89. number (double value) : m_value (value) {}
  90. enum kind get_kind () const FINAL OVERRIDE { return JSON_NUMBER; }
  91. void print (pretty_printer *pp) const FINAL OVERRIDE;
  92. double get () const { return m_value; }
  93. private:
  94. double m_value;
  95. };
  96. /* Subclass of value for strings. */
  97. class string : public value
  98. {
  99. public:
  100. string (const char *utf8);
  101. ~string () { free (m_utf8); }
  102. enum kind get_kind () const FINAL OVERRIDE { return JSON_STRING; }
  103. void print (pretty_printer *pp) const FINAL OVERRIDE;
  104. const char *get_string () const { return m_utf8; }
  105. private:
  106. char *m_utf8;
  107. };
  108. /* Subclass of value for the three JSON literals "true", "false",
  109. and "null". */
  110. class literal : public value
  111. {
  112. public:
  113. literal (enum kind kind) : m_kind (kind) {}
  114. /* Construct literal for a boolean value. */
  115. literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {}
  116. enum kind get_kind () const FINAL OVERRIDE { return m_kind; }
  117. void print (pretty_printer *pp) const FINAL OVERRIDE;
  118. private:
  119. enum kind m_kind;
  120. };
  121. } // namespace json
  122. #endif /* GCC_JSON_H */