All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
JSON.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2014-2014 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file JSON.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "JSON.h"
30 #include "Property.h"
31 #include "Properties.h"
32 #include "../../../../common/Exception.h"
33 #include "../../../../common/STLUtils.h"
34 #include "../../../../common/Translator.h"
35 #include "Config.h"
36 #include "EnumUtils.h"
37 
38 // Boost
39 #include <boost/property_tree/json_parser.hpp>
40 #include <boost/foreach.hpp>
41 #include "boost/system/system_error.hpp"
42 
43 // STL
44 #include <iostream>
45 #include <fstream>
46 #include <sstream>
47 #include <string>
48 
50 {
51 
52 }
53 
55 {
56 
57 }
58 
60 {
61  std::ostringstream buf;
62  std::string json;
63 
64  boost::property_tree::ptree array = retrievePTree();
65 
66  try
67  {
68  boost::property_tree::write_json (buf, array, false);
69  json = buf.str();
70 
71  std::ofstream outputFile;
72  outputFile.open(m_serializationPath, std::ios::out | std::ios::app);
73  outputFile << json;
74  outputFile.close();
75  }
76  catch(boost::property_tree::json_parser::json_parser_error &je)
77  {
78  std::string errmsg = "Error parsing: " + je.filename() + ": " + je.message();
79  te::common::Exception ex(TR_LAYOUT(errmsg));
80  throw(ex);
81  }
82  catch (std::ofstream::failure e)
83  {
84  std::cerr << e.what() << std::endl;
85  std::string errmsg = "Exception opening/reading/closing file: \n ";
86  te::common::Exception ex(TR_LAYOUT(errmsg));
87  //throw(ex);
88  return false;
89  }
90  catch (std::exception const& e)
91  {
92  std::cerr << e.what() << std::endl;
93  return false;
94  }
95 
96  return true;
97 }
98 
99 boost::property_tree::ptree te::layout::JSON::retrievePTree()
100 {
101  return m_array;
102 }
103 
104 std::vector<te::layout::Properties*> te::layout::JSON::retrieve()
105 {
106  std::vector<te::layout::Properties*> propsRetrieve;
107 
108  //v.first //is the name of the child.
109  //v.second //is the child tree.
110 
111  boost::property_tree::ptree::assoc_iterator it1 = m_array.find("template");
112  boost::property_tree::ptree::assoc_iterator it_nofound = m_array.not_found();
113 
114  if (it1 == it_nofound)
115  return propsRetrieve;
116 
117  boost::property_tree::ptree subtree = (*it1).second;
118 
119  int count = 0;
120  while(true)
121  {
122  std::stringstream ss;//create a stringstream
123  ss << count;//add number to the stream
124 
125  std::string s_prop = "properties_"+ ss.str();
126 
127  boost::property_tree::ptree::assoc_iterator it2 = subtree.find(s_prop);
128  boost::property_tree::ptree::assoc_iterator it_nofound2 = subtree.not_found();
129 
130  if (it2 == it_nofound2)
131  return propsRetrieve;
132 
133  boost::property_tree::ptree subtree1 = (*it2).second;
134 
135  boost::property_tree::ptree::assoc_iterator itName = subtree1.find("name");
136  boost::property_tree::ptree::assoc_iterator it_nofoundName = subtree1.not_found();
137 
138  if (itName == it_nofoundName)
139  return propsRetrieve;
140 
141  te::layout::Properties* props = new te::layout::Properties((*itName).second.data());
142 
143  std::string valName;
144  boost::property_tree::ptree tree;
145  Property prop;
146  BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, subtree.get_child(s_prop))
147  {
148  if(v.first.compare("object_type") == 0)
149  {
150  props->setTypeObj(te::layout::getLayoutAbstractObjectType(v.second.data()));
151  continue;
152  }
153 
154  if(v.first.compare("type") == 0)
155  {
156  prop.setName(valName);
157  prop.setValue(tree.data(), te::layout::getLayoutPropertyDataType(v.second.data()));
158  props->addProperty(prop);
159  prop.clear();
160  }
161  else
162  {
163  std::string val = v.first;
164  valName = val;
165  tree = v.second;
166 
167  retrieveSubPTree(tree, prop);
168  }
169  }
170 
171  propsRetrieve.push_back(props);
172  count+= 1;
173  }
174 
175  return propsRetrieve;
176 }
177 
178 void te::layout::JSON::retrieveSubPTree( boost::property_tree::ptree subTree, Property& prop )
179 {
180  std::string valName;
181  boost::property_tree::ptree tree;
182  BOOST_FOREACH( const boost::property_tree::ptree::value_type &v, subTree.get_child("") )
183  {
184  Property proper;
185  if(v.first.compare("type") == 0)
186  {
187  proper.setName(valName);
188  proper.setValue(tree.data(), te::layout::getLayoutPropertyDataType(v.second.data()));
189  prop.addSubProperty(proper);
190  }
191  else
192  {
193  std::string val = v.first;
194  valName = val;
195  tree = v.second;
196  }
197 
198  // recursive go down the hierarchy
199  retrieveSubPTree(tree, proper);
200  }
201 }
202 
204 {
205  return m_array.empty();
206 }
207 
208 void te::layout::JSON::loadFromPath( std::string loadPath )
209 {
210  m_loadPath = loadPath;
211 
212  std::ifstream inputFile;
213 
214  try
215  {
216  inputFile.open(m_loadPath);
217 
218  if (!inputFile.is_open())
219  return;
220 
221  boost::property_tree::json_parser::read_json(inputFile, m_array);
222  inputFile.close();
223  }
224  catch(boost::property_tree::json_parser::json_parser_error &je)
225  {
226  std::string errmsg = "Error parsing: " + je.filename() + ": " + je.message();
227  te::common::Exception ex(TR_LAYOUT(errmsg));
228  //throw(ex);
229  return;
230  }
231  catch (std::ifstream::failure &e)
232  {
233  std::cerr << e.what() << std::endl;
234  std::string errmsg = "Exception opening/reading/closing file: \n ";
235  te::common::Exception ex(TR_LAYOUT(errmsg));
236  //throw(ex);
237  return;
238  }
239  catch (std::exception const& e)
240  {
241  std::cerr << e.what() << std::endl;
242  }
243 }
244 
245 void te::layout::JSON::loadFromProperties( std::vector<te::layout::Properties*> properties )
246 {
247  m_properties = properties;
248 
249  std::vector<te::layout::Properties*>::iterator it;
250  std::vector<te::layout::Property>::iterator ity;
251 
252  boost::property_tree::ptree rootArray;
253  boost::property_tree::ptree childArray;
254 
255  rootArray.push_back(std::make_pair("name", m_rootKey));
256 
257  int count = 0;
258  for(it = m_properties.begin(); it != m_properties.end(); ++it)
259  {
260  Properties* props = (*it);
261  if(!props)
262  continue;
263 
264  std::vector<te::layout::Property> vec = props->getProperties();
265 
266  if(vec.empty())
267  continue;
268 
269  childArray.clear();
270  childArray.push_back(std::make_pair("object_type", te::layout::getLayoutAbstractObjectType(props->getTypeObj())));
271  for(ity = vec.begin(); ity != vec.end(); ++ity)
272  {
273  Property prop = (*ity);
274  childArray.push_back(std::make_pair(prop.getName(), prop.getValue().convertToString()));
275  childArray.push_back(std::make_pair("type", te::layout::getLayoutPropertyDataType(prop.getType())));
276  searchProperty(prop, childArray);
277  }
278  if(!childArray.empty())
279  {
280  std::stringstream ss;//create a stringstream
281  ss << count;//add number to the stream
282 
283  std::string s_prop = "properties_"+ ss.str();
284  rootArray.push_back(std::make_pair(s_prop, childArray));
285  }
286  count+= 1;
287  }
288  m_array.push_back(std::make_pair("template", rootArray));
289 }
290 
291 void te::layout::JSON::searchProperty( Property property, boost::property_tree::ptree array )
292 {
293  if(!property.getSubProperty().empty())
294  {
295  std::vector<Property> props = property.getSubProperty();
296 
297  std::vector<Property>::iterator it;
298 
299  for(it = props.begin(); it != props.end(); ++it)
300  {
301  Property prop = (*it);
302 
303  boost::property_tree::ptree childArray;
304  childArray.push_back(std::make_pair(prop.getName(), prop.getValue().convertToString()));
305  childArray.push_back(std::make_pair("type", te::layout::getLayoutPropertyDataType(prop.getType())));
306  array.push_back(std::make_pair("child",childArray));
307 
308  searchProperty(prop, childArray);
309  }
310  }
311 }
std::string getName()
Definition: Property.cpp:49
virtual void searchProperty(Property property, boost::property_tree::ptree array)
Definition: JSON.cpp:291
virtual bool isEmpty()
Definition: JSON.cpp:203
virtual bool addProperty(Property property)
Definition: Properties.h:89
Variant getValue()
Definition: Property.cpp:74
void addSubProperty(Property property)
Definition: Property.cpp:126
std::string convertToString()
Definition: Variant.cpp:289
std::string getLayoutAbstractObjectType(int enumVal)
Definition: EnumUtils.cpp:203
virtual ~JSON()
Definition: JSON.cpp:54
virtual bool serialize()
Definition: JSON.cpp:59
virtual std::vector< Property > getProperties()
Definition: Properties.h:116
void setValue(typename ValueType value, LayoutPropertyDataType type)
Definition: Property.h:106
std::string getLayoutPropertyDataType(int enumVal)
Definition: EnumUtils.cpp:119
virtual LayoutAbstractObjectType getTypeObj()
Definition: Properties.h:137
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
virtual boost::property_tree::ptree retrievePTree()
Definition: JSON.cpp:99
#define TR_LAYOUT(message)
It marks a string in order to get translated. This is a special mark used in the Terrralib Layout mod...
Definition: Config.h:58
virtual void retrieveSubPTree(boost::property_tree::ptree subTree, Property &prop)
Definition: JSON.cpp:178
LayoutPropertyDataType getType()
Definition: Property.cpp:59
void setName(std::string name)
Definition: Property.cpp:54
virtual std::vector< te::layout::Properties * > retrieve()
Definition: JSON.cpp:104
virtual void setTypeObj(LayoutAbstractObjectType type)
Definition: Properties.h:142
virtual void loadFromPath(std::string loadPath)
Definition: JSON.cpp:208
std::vector< te::layout::Property > getSubProperty()
Definition: Property.cpp:143
virtual void loadFromProperties(std::vector< te::layout::Properties * > properties)
Definition: JSON.cpp:245