gcc:
[official-gcc.git] / gcc / json.h
blob154d9e1b57517a75323fb2dc58981935c87ee1e5
1 /* JSON trees
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_JSON_H
22 #define GCC_JSON_H
24 /* Implementation of JSON, a lightweight data-interchange format.
26 See http://www.json.org/
27 and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
28 and https://tools.ietf.org/html/rfc7159
30 Supports creating a DOM-like tree of json::value *, and then dumping
31 json::value * to text. */
33 namespace json
36 /* Forward decls of json::value and its subclasses (using indentation
37 to denote inheritance. */
39 class value;
40 class object;
41 class array;
42 class number;
43 class string;
44 class literal;
46 /* An enum for discriminating the subclasses of json::value. */
48 enum kind
50 /* class json::object. */
51 JSON_OBJECT,
53 /* class json::array. */
54 JSON_ARRAY,
56 /* class json::number. */
57 JSON_NUMBER,
59 /* class json::string. */
60 JSON_STRING,
62 /* class json::literal uses these three values to identify the
63 particular literal. */
64 JSON_TRUE,
65 JSON_FALSE,
66 JSON_NULL
69 /* Base class of JSON value. */
71 class value
73 public:
74 virtual ~value () {}
75 virtual enum kind get_kind () const = 0;
76 virtual void print (pretty_printer *pp) const = 0;
78 void dump (FILE *) const;
81 /* Subclass of value for objects: an unordered collection of
82 key/value pairs. */
84 class object : public value
86 public:
87 ~object ();
89 enum kind get_kind () const FINAL OVERRIDE { return JSON_OBJECT; }
90 void print (pretty_printer *pp) const FINAL OVERRIDE;
92 void set (const char *key, value *v);
94 private:
95 typedef hash_map <char *, value *,
96 simple_hashmap_traits<nofree_string_hash, value *> > map_t;
97 map_t m_map;
100 /* Subclass of value for arrays. */
102 class array : public value
104 public:
105 ~array ();
107 enum kind get_kind () const FINAL OVERRIDE { return JSON_ARRAY; }
108 void print (pretty_printer *pp) const FINAL OVERRIDE;
110 void append (value *v);
112 private:
113 auto_vec<value *> m_elements;
116 /* Subclass of value for numbers. */
118 class number : public value
120 public:
121 number (double value) : m_value (value) {}
123 enum kind get_kind () const FINAL OVERRIDE { return JSON_NUMBER; }
124 void print (pretty_printer *pp) const FINAL OVERRIDE;
126 double get () const { return m_value; }
128 private:
129 double m_value;
132 /* Subclass of value for strings. */
134 class string : public value
136 public:
137 string (const char *utf8);
138 ~string () { free (m_utf8); }
140 enum kind get_kind () const FINAL OVERRIDE { return JSON_STRING; }
141 void print (pretty_printer *pp) const FINAL OVERRIDE;
143 const char *get_string () const { return m_utf8; }
145 private:
146 char *m_utf8;
149 /* Subclass of value for the three JSON literals "true", "false",
150 and "null". */
152 class literal : public value
154 public:
155 literal (enum kind kind) : m_kind (kind) {}
157 enum kind get_kind () const FINAL OVERRIDE { return m_kind; }
158 void print (pretty_printer *pp) const FINAL OVERRIDE;
160 private:
161 enum kind m_kind;
164 } // namespace json
166 #endif /* GCC_JSON_H */