1/* JSON trees
2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_JSON_H
22#define GCC_JSON_H
23
24/* Implementation of JSON, a lightweight data-interchange format.
25
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
29
30 Supports creating a DOM-like tree of json::value *, and then dumping
31 json::value * to text. */
32
33/* TODO: `libcpp/mkdeps.cc` wants JSON writing support for p1689r5 output;
34 extract this code and move to libiberty. */
35
36namespace json
37{
38
39/* Forward decls of json::value and its subclasses (using indentation
40 to denote inheritance. */
41
42class value;
43 class object;
44 class array;
45 class float_number;
46 class integer_number;
47 class string;
48 class literal;
49
50/* An enum for discriminating the subclasses of json::value. */
51
52enum kind
53{
54 /* class json::object. */
55 JSON_OBJECT,
56
57 /* class json::array. */
58 JSON_ARRAY,
59
60 /* class json::integer_number. */
61 JSON_INTEGER,
62
63 /* class json::float_number. */
64 JSON_FLOAT,
65
66 /* class json::string. */
67 JSON_STRING,
68
69 /* class json::literal uses these three values to identify the
70 particular literal. */
71 JSON_TRUE,
72 JSON_FALSE,
73 JSON_NULL
74};
75
76/* Base class of JSON value. */
77
78class value
79{
80 public:
81 virtual ~value () {}
82 virtual enum kind get_kind () const = 0;
83 virtual void print (pretty_printer *pp) const = 0;
84
85 void dump (FILE *) const;
86};
87
88/* Subclass of value for objects: a collection of key/value pairs
89 preserving the ordering in which keys were inserted.
90
91 Preserving the order eliminates non-determinism in the output,
92 making it easier for the user to compare repeated invocations. */
93
94class object : public value
95{
96 public:
97 ~object ();
98
99 enum kind get_kind () const final override { return JSON_OBJECT; }
100 void print (pretty_printer *pp) const final override;
101
102 void set (const char *key, value *v);
103 value *get (const char *key) const;
104
105 private:
106 typedef hash_map <char *, value *,
107 simple_hashmap_traits<nofree_string_hash, value *> > map_t;
108 map_t m_map;
109
110 /* Keep track of order in which keys were inserted. */
111 auto_vec <const char *> m_keys;
112};
113
114/* Subclass of value for arrays. */
115
116class array : public value
117{
118 public:
119 ~array ();
120
121 enum kind get_kind () const final override { return JSON_ARRAY; }
122 void print (pretty_printer *pp) const final override;
123
124 void append (value *v);
125
126 private:
127 auto_vec<value *> m_elements;
128};
129
130/* Subclass of value for floating-point numbers. */
131
132class float_number : public value
133{
134 public:
135 float_number (double value) : m_value (value) {}
136
137 enum kind get_kind () const final override { return JSON_FLOAT; }
138 void print (pretty_printer *pp) const final override;
139
140 double get () const { return m_value; }
141
142 private:
143 double m_value;
144};
145
146/* Subclass of value for integer-valued numbers. */
147
148class integer_number : public value
149{
150 public:
151 integer_number (long value) : m_value (value) {}
152
153 enum kind get_kind () const final override { return JSON_INTEGER; }
154 void print (pretty_printer *pp) const final override;
155
156 long get () const { return m_value; }
157
158 private:
159 long m_value;
160};
161
162
163/* Subclass of value for strings. */
164
165class string : public value
166{
167 public:
168 explicit string (const char *utf8);
169 string (const char *utf8, size_t len);
170 ~string () { free (ptr: m_utf8); }
171
172 enum kind get_kind () const final override { return JSON_STRING; }
173 void print (pretty_printer *pp) const final override;
174
175 const char *get_string () const { return m_utf8; }
176 size_t get_length () const { return m_len; }
177
178 private:
179 char *m_utf8;
180 size_t m_len;
181};
182
183/* Subclass of value for the three JSON literals "true", "false",
184 and "null". */
185
186class literal : public value
187{
188 public:
189 literal (enum kind kind) : m_kind (kind) {}
190
191 /* Construct literal for a boolean value. */
192 literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {}
193
194 enum kind get_kind () const final override { return m_kind; }
195 void print (pretty_printer *pp) const final override;
196
197 private:
198 enum kind m_kind;
199};
200
201} // namespace json
202
203#endif /* GCC_JSON_H */
204

source code of gcc/json.h