2 Copyright (C) 2017-2023 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/>. */
23 #include "coretypes.h"
25 #include "pretty-print.h"
31 /* class json::value. */
33 /* Dump this json::value tree to OUTF.
35 The key/value pairs of json::objects are printed in the order
36 in which the keys were originally inserted. */
39 value::dump (FILE *outf
, bool formatted
) const
42 pp_buffer (&pp
)->stream
= outf
;
43 print (&pp
, formatted
);
47 /* class json::object, a subclass of json::value, representing
48 an ordered collection of key/value pairs. */
50 /* json:object's dtor. */
54 for (map_t::iterator it
= m_map
.begin (); it
!= m_map
.end (); ++it
)
56 free (const_cast <char *>((*it
).first
));
57 delete ((*it
).second
);
61 /* Implementation of json::value::print for json::object. */
64 object::print (pretty_printer
*pp
, bool formatted
) const
66 pp_character (pp
, '{');
68 pp_indentation (pp
) += 1;
70 /* Iterate in the order that the keys were inserted. */
73 FOR_EACH_VEC_ELT (m_keys
, i
, key
)
86 map_t
&mut_map
= const_cast<map_t
&> (m_map
);
87 value
*value
= *mut_map
.get (key
);
89 pp_string (pp
, key
); // FIXME: escaping?
92 const int indent
= strlen (key
) + 4;
94 pp_indentation (pp
) += indent
;
95 value
->print (pp
, formatted
);
97 pp_indentation (pp
) -= indent
;
100 pp_indentation (pp
) -= 1;
101 pp_character (pp
, '}');
104 /* Set the json::value * for KEY, taking ownership of V
105 (and taking a copy of KEY if necessary). */
108 object::set (const char *key
, value
*v
)
113 value
**ptr
= m_map
.get (key
);
116 /* If the key is already present, delete the existing value
123 /* If the key wasn't already present, take a copy of the key,
124 and store the value. */
125 char *owned_key
= xstrdup (key
);
126 m_map
.put (owned_key
, v
);
127 m_keys
.safe_push (owned_key
);
131 /* Get the json::value * for KEY.
133 The object retains ownership of the value. */
136 object::get (const char *key
) const
140 value
**ptr
= const_cast <map_t
&> (m_map
).get (key
);
147 /* Set value of KEY within this object to a JSON
148 string value based on UTF8_VALUE. */
151 object::set_string (const char *key
, const char *utf8_value
)
153 set (key
, new json::string (utf8_value
));
156 /* Set value of KEY within this object to a JSON
157 integer value based on V. */
160 object::set_integer (const char *key
, long v
)
162 set (key
, new json::integer_number (v
));
165 /* Set value of KEY within this object to a JSON
166 floating point value based on V. */
169 object::set_float (const char *key
, double v
)
171 set (key
, new json::float_number (v
));
174 /* Set value of KEY within this object to the JSON
175 literal true or false, based on V. */
178 object::set_bool (const char *key
, bool v
)
180 set (key
, new json::literal (v
));
183 /* class json::array, a subclass of json::value, representing
184 an ordered collection of values. */
186 /* json::array's dtor. */
192 FOR_EACH_VEC_ELT (m_elements
, i
, v
)
196 /* Implementation of json::value::print for json::array. */
199 array::print (pretty_printer
*pp
, bool formatted
) const
201 pp_character (pp
, '[');
203 pp_indentation (pp
) += 1;
206 FOR_EACH_VEC_ELT (m_elements
, i
, v
)
219 v
->print (pp
, formatted
);
222 pp_indentation (pp
) -= 1;
223 pp_character (pp
, ']');
226 /* Append non-NULL value V to a json::array, taking ownership of V. */
229 array::append (value
*v
)
232 m_elements
.safe_push (v
);
235 /* class json::float_number, a subclass of json::value, wrapping a double. */
237 /* Implementation of json::value::print for json::float_number. */
240 float_number::print (pretty_printer
*pp
,
241 bool formatted ATTRIBUTE_UNUSED
) const
244 snprintf (tmp
, sizeof (tmp
), "%g", m_value
);
248 /* class json::integer_number, a subclass of json::value, wrapping a long. */
250 /* Implementation of json::value::print for json::integer_number. */
253 integer_number::print (pretty_printer
*pp
,
254 bool formatted ATTRIBUTE_UNUSED
) const
257 snprintf (tmp
, sizeof (tmp
), "%ld", m_value
);
262 /* class json::string, a subclass of json::value. */
264 /* json::string's ctor. */
266 string::string (const char *utf8
)
269 m_utf8
= xstrdup (utf8
);
270 m_len
= strlen (utf8
);
273 string::string (const char *utf8
, size_t len
)
276 m_utf8
= XNEWVEC (char, len
);
278 memcpy (m_utf8
, utf8
, len
);
281 /* Implementation of json::value::print for json::string. */
284 string::print (pretty_printer
*pp
,
285 bool formatted ATTRIBUTE_UNUSED
) const
287 pp_character (pp
, '"');
288 for (size_t i
= 0; i
!= m_len
; ++i
)
294 pp_string (pp
, "\\\"");
297 pp_string (pp
, "\\\\");
300 pp_string (pp
, "\\b");
303 pp_string (pp
, "\\f");
306 pp_string (pp
, "\\n");
309 pp_string (pp
, "\\r");
312 pp_string (pp
, "\\t");
315 pp_string (pp
, "\\0");
318 pp_character (pp
, ch
);
321 pp_character (pp
, '"');
324 /* class json::literal, a subclass of json::value. */
326 /* Implementation of json::value::print for json::literal. */
329 literal::print (pretty_printer
*pp
,
330 bool formatted ATTRIBUTE_UNUSED
) const
335 pp_string (pp
, "true");
338 pp_string (pp
, "false");
341 pp_string (pp
, "null");
355 /* Verify that JV->print () prints EXPECTED_JSON. */
358 assert_print_eq (const location
&loc
,
359 const json::value
&jv
,
361 const char *expected_json
)
364 jv
.print (&pp
, formatted
);
365 ASSERT_STREQ_AT (loc
, expected_json
, pp_formatted_text (&pp
));
368 #define ASSERT_PRINT_EQ(JV, FORMATTED, EXPECTED_JSON) \
369 assert_print_eq (SELFTEST_LOCATION, JV, FORMATTED, EXPECTED_JSON)
371 /* Verify that object::get works as expected. */
377 value
*val
= new json::string ("value");
378 obj
.set ("foo", val
);
379 ASSERT_EQ (obj
.get ("foo"), val
);
380 ASSERT_EQ (obj
.get ("not-present"), NULL
);
383 /* Verify that JSON objects are written correctly. */
386 test_writing_objects ()
389 obj
.set_string ("foo", "bar");
390 obj
.set_string ("baz", "quux");
391 /* This test relies on json::object writing out key/value pairs
392 in key-insertion order. */
393 ASSERT_PRINT_EQ (obj
, true,
394 "{\"foo\": \"bar\",\n"
395 " \"baz\": \"quux\"}");
396 ASSERT_PRINT_EQ (obj
, false,
397 "{\"foo\": \"bar\", \"baz\": \"quux\"}");
400 /* Verify that JSON arrays are written correctly. */
403 test_writing_arrays ()
406 ASSERT_PRINT_EQ (arr
, true, "[]");
408 arr
.append (new json::string ("foo"));
409 ASSERT_PRINT_EQ (arr
, true, "[\"foo\"]");
411 arr
.append (new json::string ("bar"));
412 ASSERT_PRINT_EQ (arr
, true,
415 ASSERT_PRINT_EQ (arr
, false,
416 "[\"foo\", \"bar\"]");
419 /* Verify that JSON numbers are written correctly. */
422 test_writing_float_numbers ()
424 ASSERT_PRINT_EQ (float_number (0), true, "0");
425 ASSERT_PRINT_EQ (float_number (42), true, "42");
426 ASSERT_PRINT_EQ (float_number (-100), true, "-100");
427 ASSERT_PRINT_EQ (float_number (123456789), true, "1.23457e+08");
431 test_writing_integer_numbers ()
433 ASSERT_PRINT_EQ (integer_number (0), true, "0");
434 ASSERT_PRINT_EQ (integer_number (42), true, "42");
435 ASSERT_PRINT_EQ (integer_number (-100), true, "-100");
436 ASSERT_PRINT_EQ (integer_number (123456789), true, "123456789");
437 ASSERT_PRINT_EQ (integer_number (-123456789), true, "-123456789");
440 /* Verify that JSON strings are written correctly. */
443 test_writing_strings ()
446 ASSERT_PRINT_EQ (foo
, true, "\"foo\"");
448 string
contains_quotes ("before \"quoted\" after");
449 ASSERT_PRINT_EQ (contains_quotes
, true, "\"before \\\"quoted\\\" after\"");
451 const char data
[] = {'a', 'b', 'c', 'd', '\0', 'e', 'f'};
452 string
not_terminated (data
, 3);
453 ASSERT_PRINT_EQ (not_terminated
, true, "\"abc\"");
454 string
embedded_null (data
, sizeof data
);
455 ASSERT_PRINT_EQ (embedded_null
, true, "\"abcd\\0ef\"");
458 /* Verify that JSON literals are written correctly. */
461 test_writing_literals ()
463 ASSERT_PRINT_EQ (literal (JSON_TRUE
), true, "true");
464 ASSERT_PRINT_EQ (literal (JSON_FALSE
), true, "false");
465 ASSERT_PRINT_EQ (literal (JSON_NULL
), true, "null");
467 ASSERT_PRINT_EQ (literal (true), true, "true");
468 ASSERT_PRINT_EQ (literal (false), true, "false");
471 /* Verify that nested values are formatted correctly when written. */
477 object
*child
= new object
;
478 object
*grandchild
= new object
;
480 obj
.set_string ("str", "bar");
481 obj
.set ("child", child
);
482 obj
.set_integer ("int", 42);
484 child
->set ("grandchild", grandchild
);
485 child
->set_integer ("int", 1776);
487 array
*arr
= new array
;
488 for (int i
= 0; i
< 3; i
++)
489 arr
->append (new integer_number (i
));
490 grandchild
->set ("arr", arr
);
491 grandchild
->set_integer ("int", 1066);
493 /* This test relies on json::object writing out key/value pairs
494 in key-insertion order. */
495 ASSERT_PRINT_EQ (obj
, true,
496 ("{\"str\": \"bar\",\n"
497 " \"child\": {\"grandchild\": {\"arr\": [0,\n"
503 ASSERT_PRINT_EQ (obj
, false,
504 ("{\"str\": \"bar\", \"child\": {\"grandchild\":"
505 " {\"arr\": [0, 1, 2], \"int\": 1066},"
506 " \"int\": 1776}, \"int\": 42}"));
509 /* Run all of the selftests within this file. */
515 test_writing_objects ();
516 test_writing_arrays ();
517 test_writing_float_numbers ();
518 test_writing_integer_numbers ();
519 test_writing_strings ();
520 test_writing_literals ();
524 } // namespace selftest
526 #endif /* #if CHECKING_P */