ed
json_reader.cpp
Go to the documentation of this file.
1 #include "ed/io/json_reader.h"
2 
3 #include "rapidjson/reader.h"
4 #include "ed/io/data_writer.h"
5 
6 
7 namespace ed
8 {
9 
10 namespace io
11 {
12 
13 // ----------------------------------------------------------------------------------------------------
14 
15 struct MyHandler {
16 
18  {
19  }
20 
21  bool Null() { return true; }
22 
23  bool Bool(bool b) { int i = b; w.setValue(key, i); ; return true; }
24 
25  bool Int(int i) { w.setValue(key, i); return true; }
26 
27  bool Uint(unsigned u) { w.setValue(key, (int)u); return true; }
28 
29  bool Int64(int64_t i) { w.setValue(key, (int)i); return true; }
30 
31  bool Uint64(uint64_t u) { w.setValue(key, (int)u); return true; }
32 
33  bool Double(double d) { w.setValue(key, d); return true; }
34 
35  bool RawNumber(const char* str, rapidjson::SizeType /*len*/, bool /*copy*/)
36  {
37  w.setValue(key, str);
38  return true;
39  }
40  bool String(const char* str, rapidjson::SizeType /*length*/, bool /*copy*/)
41  {
42  w.setValue(key, str);
43  return true;
44  }
45  bool StartObject()
46  {
47  if (stack.empty())
48  {
49  stack.push_back('g');
50  return true;
51  }
52 
53  if (stack.back() == 'a')
54  {
55  stack.push_back('i');
56  w.addArrayItem();
57  }
58  else
59  {
60  stack.push_back('g');
61  w.writeGroup(key);
62  }
63 
64  return true;
65  }
66 
67  bool Key(const char* str, rapidjson::SizeType /*length*/, bool /*copy*/) { key = str; return true; }
68 
69  bool EndObject(rapidjson::SizeType /*memberCount*/)
70  {
71  if (stack.empty())
72  return true;
73 
74  if (stack.back() == 'i')
75  w.endArrayItem();
76  else
77  w.endGroup();
78 
79  stack.pop_back();
80  return true;
81  }
82 
83  bool StartArray()
84  {
85  w.writeArray(key);
86  stack.push_back('a');
87  return true;
88  }
89 
90  bool EndArray(rapidjson::SizeType /*elementCount*/)
91  {
92  w.endArray();
93  stack.pop_back();
94  return true;
95  }
96 
100 
101 };
102 
103 // ----------------------------------------------------------------------------------------------------
104 
105 JSONReader::JSONReader(const char* s) : n_current_(Node(0, MAP))
106 {
108  MyHandler handler(w);
109  rapidjson::StringStream ss(s);
110 
111  rapidjson::Reader reader;
112  reader.Parse(ss, handler);
113 
114  if (reader.HasParseError())
115  {
116  error_ = "Could not parse string";
117  }
118 }
119 
120 // ----------------------------------------------------------------------------------------------------
121 
123 {
124 }
125 
126 // ----------------------------------------------------------------------------------------------------
127 
129 {
132  if (it == map.end())
133  return false;
134 
135  n_current_ = it->second;
136  return true;
137 }
138 
139 // ----------------------------------------------------------------------------------------------------
140 
142 {
144  n_current_.type = MAP;
145  return true;
146 }
147 
148 // ----------------------------------------------------------------------------------------------------
149 
151 {
154  if (it == map.end())
155  return false;
156 
157  n_current_ = it->second;
159 
160  return true;
161 }
162 
163 // ----------------------------------------------------------------------------------------------------
164 
166 {
168  return false;
169 
170  unsigned int& i_next_array_item_ = array_index_stack_.back();
172 
173  if (n_current_.type != ARRAY && i_next_array_item_ > 0)
175  else
177 
178  n_current_.type = MAP;
179 
180  return true;
181 }
182 
183 // ----------------------------------------------------------------------------------------------------
184 
186 {
188  return false;
189 
190  unsigned int& i_next_array_item_ = array_index_stack_.back();
191 
192  if (n_current_.type != ARRAY)
193  {
194  if (i_next_array_item_ == 0)
195  return false;
196 
199  }
200 
202 
203  if (i_next_array_item_ >= array.size())
204  return false;
205 
206  n_current_ = array[i_next_array_item_];
207  ++i_next_array_item_;
208 
209  return true;
210 }
211 
212 // ----------------------------------------------------------------------------------------------------
213 
214 bool JSONReader::readValue(const std::string& key, float& f)
215 {
216  return value<float>(key, f);
217 }
218 
219 // ----------------------------------------------------------------------------------------------------
220 
221 bool JSONReader::readValue(const std::string& key, double& d)
222 {
223  return value<double>(key, d);
224 
225 }
226 
227 // ----------------------------------------------------------------------------------------------------
228 
229 bool JSONReader::readValue(const std::string& key, int& i)
230 {
231  return value<int>(key, i);
232 }
233 
234 // ----------------------------------------------------------------------------------------------------
235 
237 {
238  return value<std::string>(key, s);
239 }
240 
241 }
242 
243 }
ed::io::MyHandler::Double
bool Double(double d)
Definition: json_reader.cpp:33
ed::io::MyHandler::EndObject
bool EndObject(rapidjson::SizeType)
Definition: json_reader.cpp:69
std::string
ed::io::Data::array_parents
std::vector< unsigned int > array_parents
Definition: data.h:41
ed::io::MyHandler::w
ed::io::DataWriter & w
Definition: json_reader.cpp:97
ed::io::ARRAY
@ ARRAY
Definition: data.h:20
ed::io::MyHandler::String
bool String(const char *str, rapidjson::SizeType, bool)
Definition: json_reader.cpp:40
ed::io::Data::map_parents
std::vector< unsigned int > map_parents
Definition: data.h:44
std::vector< unsigned char >
std::map::find
T find(T... args)
std::vector::size
T size(T... args)
ed::io::MyHandler::StartArray
bool StartArray()
Definition: json_reader.cpp:83
ed::io::DataWriter::endArray
void endArray()
Definition: data_writer.h:66
ed::io::DataWriter::writeGroup
void writeGroup(const std::string &key)
Definition: data_writer.h:36
ed::io::JSONReader::~JSONReader
virtual ~JSONReader()
Definition: json_reader.cpp:122
ed::io::JSONReader::JSONReader
JSONReader(const char *s)
Definition: json_reader.cpp:105
ed::io::JSONReader::nextArrayItem
bool nextArrayItem()
Definition: json_reader.cpp:185
ed::io::JSONReader::readValue
bool readValue(const std::string &, float &f)
Definition: json_reader.cpp:214
ed::io::MyHandler::Int64
bool Int64(int64_t i)
Definition: json_reader.cpp:29
ed::io::MAP
@ MAP
Definition: data.h:21
data_writer.h
ed::io::JSONReader::readArray
bool readArray(const std::string &name)
Definition: json_reader.cpp:150
ed::io::MyHandler
Definition: json_reader.cpp:15
std::vector::back
T back(T... args)
ed::io::JSONReader::readGroup
bool readGroup(const std::string &name)
Definition: json_reader.cpp:128
ed::io::JSONReader::n_current_
Node n_current_
Definition: json_reader.h:45
ed::io::DataWriter::endArrayItem
bool endArrayItem()
Definition: data_writer.h:89
ed::io::MyHandler::EndArray
bool EndArray(rapidjson::SizeType)
Definition: json_reader.cpp:90
ed::io::MyHandler::Null
bool Null()
Definition: json_reader.cpp:21
std::vector::push_back
T push_back(T... args)
ed::io::MyHandler::StartObject
bool StartObject()
Definition: json_reader.cpp:45
ed::io::MyHandler::Int
bool Int(int i)
Definition: json_reader.cpp:25
ed::io::MyHandler::key
std::string key
Definition: json_reader.cpp:98
ed::io::MyHandler::Uint
bool Uint(unsigned u)
Definition: json_reader.cpp:27
ed::io::MyHandler::Key
bool Key(const char *str, rapidjson::SizeType, bool)
Definition: json_reader.cpp:67
json_reader.h
ed::io::MyHandler::MyHandler
MyHandler(ed::io::DataWriter &w_)
Definition: json_reader.cpp:17
ed::io::DataWriter::addArrayItem
bool addArrayItem()
Definition: data_writer.h:72
ed::io::JSONReader::data_
Data data_
Definition: json_reader.h:44
ed::io::MyHandler::stack
std::vector< unsigned char > stack
Definition: json_reader.cpp:99
ed::io::MyHandler::Bool
bool Bool(bool b)
Definition: json_reader.cpp:23
std::map
std::vector::pop_back
T pop_back(T... args)
ed::io::Data::arrays
std::vector< std::vector< Node > > arrays
Definition: data.h:40
ed::io::Node::idx
unsigned int idx
Definition: data.h:32
ed::io::Node
Definition: data.h:27
b
void b()
ed::io::JSONReader::endArray
bool endArray()
Definition: json_reader.cpp:165
ed::io::JSONReader::endGroup
bool endGroup()
Definition: json_reader.cpp:141
ed::io::JSONReader::error_
std::string error_
Definition: json_reader.h:48
ed::io::Data::maps
std::vector< std::map< std::string, Node > > maps
Definition: data.h:43
ed::io::Node::type
NodeType type
Definition: data.h:33
ed::io::MyHandler::Uint64
bool Uint64(uint64_t u)
Definition: json_reader.cpp:31
ed::io::DataWriter::endGroup
void endGroup()
Definition: data_writer.h:48
ed::io::DataWriter::writeArray
void writeArray(const std::string &key)
Definition: data_writer.h:54
std::vector::empty
T empty(T... args)
ed
Definition: convex_hull.h:8
ed::io::MyHandler::RawNumber
bool RawNumber(const char *str, rapidjson::SizeType, bool)
Definition: json_reader.cpp:35
std::map::end
T end(T... args)
ed::io::DataWriter::setValue
void setValue(const std::string &key, const Variant &value)
Definition: data_writer.h:30
ed::io::DataWriter
Definition: data_writer.h:14
ed::io::JSONReader::array_index_stack_
std::vector< unsigned int > array_index_stack_
Definition: json_reader.h:46