1 /*
2 * Copyright 2009-2010 Cybozu Labs, Inc.
3 * Copyright 2011-2014 Kazuho Oku
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "../picojson.h"
29
main(void)30 int main(void)
31 {
32 picojson::value v;
33
34 // read json value from stream
35 std::cin >> v;
36 if (std::cin.fail()) {
37 std::cerr << picojson::get_last_error() << std::endl;
38 return 1;
39 }
40
41 // dump json object
42 std::cout << "---- dump input ----" << std::endl;
43 std::cout << v << std::endl;
44
45 // accessors
46 std::cout << "---- analyzing input ----" << std::endl;
47 if (v.is<picojson::null>()) {
48 std::cout << "input is null" << std::endl;
49 } else if (v.is<bool>()) {
50 std::cout << "input is " << (v.get<bool>() ? "true" : "false") << std::endl;
51 } else if (v.is<double>()) {
52 std::cout << "input is " << v.get<double>() << std::endl;
53 } else if (v.is<std::string>()) {
54 std::cout << "input is " << v.get<std::string>() << std::endl;
55 } else if (v.is<picojson::array>()) {
56 std::cout << "input is an array" << std::endl;
57 const picojson::array& a = v.get<picojson::array>();
58 for (picojson::array::const_iterator i = a.begin(); i != a.end(); ++i) {
59 std::cout << " " << *i << std::endl;
60 }
61 } else if (v.is<picojson::object>()) {
62 std::cout << "input is an object" << std::endl;
63 const picojson::object& o = v.get<picojson::object>();
64 for (picojson::object::const_iterator i = o.begin(); i != o.end(); ++i) {
65 std::cout << i->first << " " << i->second << std::endl;
66 }
67 }
68
69 return 0;
70 }
71