1+ #include " jpp.hh"
2+ #include < fstream>
3+ #include < sstream>
4+ #include < iostream>
5+
6+ std::string read_string_from_file (const std::string &);
7+
8+ int main (int argc, char **argv)
9+ {
10+ try
11+ {
12+ Jpp::Json json;
13+ Jpp::Json json1;
14+ json.parse (read_string_from_file (" json/e1.json" ));
15+ std::cout << json.to_string () << " \n "
16+ << std::endl;
17+ std::cout << json[" quiz" ][" maths" ].to_string () << " \n "
18+ << std::endl;
19+ json[" hello" ] = " world" ;
20+ std::cout << json.to_string () << " \n "
21+ << std::endl;
22+
23+ std::cout << json1.to_string () << " \n "
24+ << std::endl;
25+ json1[" name" ] = " simon" ;
26+ json1[" surname" ] = Jpp::Json (nullptr );
27+ std::cout << json1.to_string () << " \n "
28+ << std::endl;
29+
30+ Jpp::Json car;
31+ Jpp::Json car_collection;
32+ car[" brand" ] = " Brand1" ;
33+ car[" age" ] = 10 ;
34+ car[" model" ] = " Model1" ;
35+
36+ car_collection[" favoriteCar" ] = car;
37+
38+ std::cout << car_collection.to_string () << " \n "
39+ << std::endl;
40+
41+ Jpp::Json array;
42+ array.parse (" [1, 2, 3, \" Hello World\" ]" );
43+ std::cout << array.to_string () << std::endl;
44+ for (auto a : array)
45+ {
46+ std::cout << a.second .to_string () << std::endl;
47+ }
48+ }
49+ catch (const std::exception e)
50+ {
51+ std::cout << e.what () << std::endl;
52+ }
53+ }
54+
55+ std::string read_string_from_file (const std::string &file_path)
56+ {
57+ const std::ifstream input_stream (file_path, std::ios_base::binary);
58+
59+ if (input_stream.fail ())
60+ {
61+ throw std::runtime_error (" Failed to open file" );
62+ }
63+
64+ std::stringstream buffer;
65+ buffer << input_stream.rdbuf ();
66+
67+ return buffer.str ();
68+ }
0 commit comments