1+ #include < iostream>
12#include < unistd.h>
23
3- #include " simdjson/jsonparser.h"
44#include " simdjson/jsonioutil.h"
5+ #include " simdjson/jsonparser.h"
56
67using namespace std ;
78
8- void compute_dump (ParsedJson::iterator & pjh) {
9- bool inobject = (pjh.get_type () == ' {' );
10- bool inarray = (pjh.get_type () == ' {' );
11- printf (" got this: \n " );
12- pjh.print (std::cout);
13- printf (" \n " );
14- if ((!inobject) && (!inarray)) {
15- pjh.print (std::cout);// just print the lone value
16- pjh.up () && pjh.next ();
17- return ; // we are done
9+ void compute_dump (ParsedJson::iterator &pjh) {
10+ bool inobject = (pjh.get_type () == ' {' );
11+ bool inarray = (pjh.get_type () == ' [' );
12+ if ((!inobject) && (!inarray)) {
13+ pjh.print (std::cout); // just print the lone value
14+ return ; // we are done
15+ }
16+ // we have either an array or an object
17+ pjh.down ();
18+ if (inobject) {
19+ std::cout << " {" ;
20+ assert (pjh.get_type () == ' "' );
21+ pjh.print (std::cout); // must be a string
22+ std::cout << " :" ;
23+ assert (pjh.next ());
24+ compute_dump (pjh); // let us recurse
25+ while (pjh.next ()) {
26+ std::cout << " ," ;
27+ assert (pjh.get_type () == ' "' );
28+ pjh.print (std::cout);
29+ std::cout << " :" ;
30+ assert (pjh.next ());
31+ compute_dump (pjh); // let us recurse
1832 }
19- printf (" going to object/array\n " );
20-
21- // we have either an array or an object
22- pjh.down ();
23- if (inobject) {
24- std::cout <<" {" ;
25- pjh.print (std::cout); // must be a string
26- std::cout <<" :" ;
27- pjh.next ();
28- compute_dump (pjh);// let us recurse
29- while (pjh.next ()) {
30- std::cout <<" ," ;
31- pjh.print (std::cout);
32- std::cout <<" :" ;
33- pjh.next ();
34- compute_dump (pjh);// let us recurse
35- }
36- std::cout <<" }" ;
37- } else {
38- std::cout <<" [" ;
39- compute_dump (pjh);// let us recurse
40- while (pjh.next ()) {
41- std::cout <<" ," ;
42- compute_dump (pjh);// let us recurse
43- }
44- std::cout <<" ]" ;
33+ std::cout << " }" ;
34+ } else {
35+ std::cout << " [" ;
36+ compute_dump (pjh); // let us recurse
37+ while (pjh.next ()) {
38+ std::cout << " ," ;
39+ compute_dump (pjh); // let us recurse
4540 }
46- pjh.up () && pjh.next ();
41+ std::cout << " ]" ;
42+ }
43+ pjh.up ();
4744}
48-
4945
5046int main (int argc, char *argv[]) {
5147 int c;
5248 bool rawdump = false ;
5349 bool apidump = false ;
5450
55- while ((c = getopt (argc, argv, " da" )) != -1 )
56- switch (c)
57- {
58- case ' d' :
59- rawdump = true ;
60- break ;
61- case ' a' :
62- apidump = true ;
63- break ;
64- default :
65- abort ();
66- }
51+ while ((c = getopt (argc, argv, " da" )) != -1 )
52+ switch (c) {
53+ case ' d' :
54+ rawdump = true ;
55+ break ;
56+ case ' a' :
57+ apidump = true ;
58+ break ;
59+ default :
60+ abort ();
61+ }
6762 if (optind >= argc) {
68- cerr << " Reads json in, out the result of the parsing. " << endl;
63+ cerr << " Reads json in, out the result of the parsing. " << endl;
6964 cerr << " Usage: " << argv[0 ] << " <jsonfile>" << endl;
7065 exit (1 );
7166 }
72- const char * filename = argv[optind];
73- if (optind + 1 < argc) {
74- cerr << " warning: ignoring everything after " << argv[optind + 1 ] << endl;
67+ const char *filename = argv[optind];
68+ if (optind + 1 < argc) {
69+ cerr << " warning: ignoring everything after " << argv[optind + 1 ] << endl;
7570 }
7671 std::string_view p;
7772 try {
7873 p = get_corpus (filename);
79- } catch (const std::exception& e) { // caught by reference to base
74+ } catch (const std::exception & e) { // caught by reference to base
8075 std::cout << " Could not load the file " << filename << std::endl;
8176 return EXIT_FAILURE;
8277 }
8378 ParsedJson pj;
8479 bool allocok = pj.allocateCapacity (p.size (), 1024 );
85- if (!allocok) {
80+ if (!allocok) {
8681 std::cerr << " failed to allocate memory" << std::endl;
8782 return EXIT_FAILURE;
8883 }
@@ -91,18 +86,18 @@ int main(int argc, char *argv[]) {
9186 std::cerr << " Parsing failed. " << std::endl;
9287 return EXIT_FAILURE;
9388 }
94- if (apidump) {
95- ParsedJson::iterator pjh (pj);
96- if (!pjh.isOk ()) {
97- std::cerr << " Could not iterate parsed result. " << std::endl;
98- return EXIT_FAILURE;
99- }
100- compute_dump (pjh);
89+ if (apidump) {
90+ ParsedJson::iterator pjh (pj);
91+ if (!pjh.isOk ()) {
92+ std::cerr << " Could not iterate parsed result. " << std::endl;
93+ return EXIT_FAILURE;
94+ }
95+ compute_dump (pjh);
10196 } else {
10297 is_ok = rawdump ? pj.dump_raw_tape () : pj.printjson ();
103- if (!is_ok) {
98+ if (!is_ok) {
10499 std::cerr << " Could not print out parsed result. " << std::endl;
105- return EXIT_FAILURE;
100+ return EXIT_FAILURE;
106101 }
107102 }
108103 return EXIT_SUCCESS;
0 commit comments