FileReader.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/json/FileReader.hpp"
30 
31 namespace nom {
32  namespace JSON {
33 
34 FileReader::FileReader ( void ) {}
35 FileReader::~FileReader ( void ) {}
36 
38  const std::string& filename,
39  Value& object,
40  bool parse_comments
41  )
42 {
43  std::ifstream fp;
44  Json::Reader parser;
45 
46  fp.open ( filename );
47 
48  if ( fp.is_open() == false || fp.good() == false )
49  {
50 NOM_LOG_ERR ( NOM, "Could not open given file: " + filename );
51  fp.close();
52  return false;
53  }
54 
55  if ( parser.parse ( fp, object.get(), parse_comments ) == false )
56  {
57 NOM_LOG_ERR ( NOM, "Unable to parse input JSON file: " + filename );
58  fp.close();
59  return false;
60  }
61 
62  fp.close();
63  return true;
64 }
65 
66 void FileReader::dump_key ( const ValueType& key ) const
67 {
68  switch ( key.type() )
69  {
70  // Unkonwn type; let jsoncpp try figuring it out
71  default: printf ( " [type=%d, size=%d]", key.type(), key.size() ); break;
72 
73  case 0: // NULL
74  {
75  printf ( " [type=nullValue, size=%d]", key.size() );
76  break;
77  }
78 
79  case 1: // JSON signed integer
80  {
81  printf ( " [type=intValue, size=%lu]", sizeof ( key.asInt() ) );
82  break;
83  }
84 
85  case 2: // JSON unsigned integer
86  {
87  printf ( " [type=uintValue, size=%lu]", sizeof ( key.asUInt() ) );
88  break;
89  }
90 
91  case 3: // JSON double
92  {
93  printf ( " [type=realValue, size=%lu]", sizeof ( key.asDouble() ) );
94  break;
95  }
96 
97  case 4: // JSON string
98  {
99  printf ( " [type=stringValue, size=%lu]", key.asString().length() );
100  break;
101  }
102 
103  case 5: // JSON boolean
104  {
105  printf ( " [type=booleanValue, size=%lu]", sizeof ( key.asBool() ) );
106  break;
107  }
108 
109  case 6: // JSON Array
110  {
111  printf ( " [type=arrayValue, size=%d]", key.size() );
112  break;
113  }
114 
115  case 7: // JSON object
116  {
117  printf ( " [type=objectValue, size=%d]", key.size() );
118  break;
119  }
120  } // switch key.type()
121 }
122 
123 void FileReader::dump_value ( const ValueType& value ) const
124 {
125  if ( value.isString() )
126  {
127  printf ( "string (%s)", value.asString().c_str() );
128  }
129  else if ( value.isBool() )
130  {
131  printf ( "bool (%d)", value.asBool() );
132  }
133  else if ( value.isInt() )
134  {
135  printf ( "int (%d)", value.asInt() );
136  }
137  else if ( value.isUInt() )
138  {
139  printf ( "uint (%u)", value.asUInt() );
140  }
141  else if ( value.isDouble() )
142  {
143  printf ( "double (%f)", value.asDouble() );
144  }
145  else // Unkonwn type; let jsoncpp try figuring it out
146  {
147  printf ( "unknown type=%d", value.type() );
148  }
149 }
150 
151 bool FileReader::dump ( const ValueType& root, int depth ) const
152 {
153  depth += 1;
154 
155  this->dump_key ( root );
156 
157  if ( root.size() > 0 )
158  {
159  std::cout << std::endl;
160  for ( auto itr = root.begin() ; itr != root.end() ; ++itr )
161  {
162  // Pretty print depth.
163  for ( auto tab = 0 ; tab < depth; tab++ )
164  {
165  std::cout << "-";
166  }
167 
168  std::cout << " subvalue (";
169  this->dump_value ( itr.key() );
170  std::cout << ") -";
171  this->dump ( *itr, depth );
172  }
173 
174  return true;
175  }
176  else
177  {
178  std::cout << " ";
179  this->dump_value ( root );
180  std::cout << std::endl;
181  }
182 
183  return true;
184 }
185 
186  } // namespace JSON
187 } // namespace nom
bool dump(const ValueType &object, int depth=0) const
Definition: FileReader.cpp:151
Interface class for JSON data types.
Definition: Value.hpp:45
bool load(const std::string &filename, Value &object, bool parse_comments=false)
Definition: FileReader.cpp:37
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
Definition: jsoncpp.cpp:283
void dump_key(const ValueType &object) const
Debugging aid; helper method for nom::JSON::FileReader::dump.
Definition: FileReader.cpp:66
ArrayIndex size() const
Number of values in array or object.
Definition: jsoncpp.cpp:2506
void dump_value(const ValueType &key) const
Debugging aid; helper method for nom::JSON::FileReader::dump.
Definition: FileReader.cpp:123
Represents a JSON value.
Definition: json.h:433
Unserialize a JSON document into a Value.
Definition: json.h:1455