2 Copyright (C) 2017-2019 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
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
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/>. */
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. */
36 /* Forward decls of json::value and its subclasses (using indentation
37 to denote inheritance. */
46 /* An enum for discriminating the subclasses of json::value. */
50 /* class json::object. */
53 /* class json::array. */
56 /* class json::number. */
59 /* class json::string. */
62 /* class json::literal uses these three values to identify the
63 particular literal. */
69 /* Base class of JSON 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
84 class object
: public value
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
);
93 value
*get (const char *key
) const;
96 typedef hash_map
<char *, value
*,
97 simple_hashmap_traits
<nofree_string_hash
, value
*> > map_t
;
101 /* Subclass of value for arrays. */
103 class array
: public value
108 enum kind
get_kind () const FINAL OVERRIDE
{ return JSON_ARRAY
; }
109 void print (pretty_printer
*pp
) const FINAL OVERRIDE
;
111 void append (value
*v
);
114 auto_vec
<value
*> m_elements
;
117 /* Subclass of value for numbers. */
119 class number
: public value
122 number (double value
) : m_value (value
) {}
124 enum kind
get_kind () const FINAL OVERRIDE
{ return JSON_NUMBER
; }
125 void print (pretty_printer
*pp
) const FINAL OVERRIDE
;
127 double get () const { return m_value
; }
133 /* Subclass of value for strings. */
135 class string
: public value
138 string (const char *utf8
);
139 ~string () { free (m_utf8
); }
141 enum kind
get_kind () const FINAL OVERRIDE
{ return JSON_STRING
; }
142 void print (pretty_printer
*pp
) const FINAL OVERRIDE
;
144 const char *get_string () const { return m_utf8
; }
150 /* Subclass of value for the three JSON literals "true", "false",
153 class literal
: public value
156 literal (enum kind kind
) : m_kind (kind
) {}
158 /* Construct literal for a boolean value. */
159 literal (bool value
): m_kind (value
? JSON_TRUE
: JSON_FALSE
) {}
161 enum kind
get_kind () const FINAL OVERRIDE
{ return m_kind
; }
162 void print (pretty_printer
*pp
) const FINAL OVERRIDE
;
170 #endif /* GCC_JSON_H */