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) 2008 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/Property.h"
31 #include "../property/Properties.h"
32 #include "../../../common/Exception.h"
33 #include "../../../common/STLUtils.h"
34 #include "../../../common/Translator.h"
35 #include "../Config.h"
36 #include "../enum/Enums.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.c_str(), 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(TE_TR(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(TE_TR(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 
120 
121  int count = 0;
122  while(true)
123  {
124  std::stringstream ss;//create a stringstream
125  ss << count;//add number to the stream
126 
127  std::string s_prop = "properties_"+ ss.str();
128 
129  boost::property_tree::ptree::assoc_iterator it2 = subtree.find(s_prop);
130  boost::property_tree::ptree::assoc_iterator it_nofound2 = subtree.not_found();
131 
132  if (it2 == it_nofound2)
133  return propsRetrieve;
134 
135  boost::property_tree::ptree subtree1 = (*it2).second;
136 
137  boost::property_tree::ptree::assoc_iterator itName = subtree1.find("name");
138  boost::property_tree::ptree::assoc_iterator it_nofoundName = subtree1.not_found();
139 
140  if (itName == it_nofoundName)
141  return propsRetrieve;
142 
143  te::layout::Properties* props = new te::layout::Properties((*itName).second.data());
144 
146 
147  std::string valName;
148  boost::property_tree::ptree tree;
149  Property prop;
150  BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, subtree.get_child(s_prop))
151  {
152  if(v.first.compare("object_type") == 0)
153  {
154  EnumType* type = enumObj->getEnum(v.second.data());
155  props->setTypeObj(type);
156  continue;
157  }
158 
159  if(v.first.compare("type") == 0)
160  {
161  prop.setName(valName);
162  EnumType* tp = dataType->getEnum(v.second.data());
163  Variant vt;
164  vt.fromPtree(tree, tp);
165  prop.setValue(vt);
166  props->addProperty(prop);
167  prop.clear();
168  }
169  else
170  {
171  std::string val = v.first;
172  valName = val;
173  tree = v.second;
174 
175  retrieveSubPTree(tree, prop);
176  }
177  }
178 
179  propsRetrieve.push_back(props);
180  count+= 1;
181  }
182 
183  return propsRetrieve;
184 }
185 
186 void te::layout::JSON::retrieveSubPTree( boost::property_tree::ptree subTree, Property& prop )
187 {
189 
190  std::string valName;
191  boost::property_tree::ptree tree;
192  BOOST_FOREACH( const boost::property_tree::ptree::value_type &v, subTree.get_child("") )
193  {
194  Property proper;
195  if(v.first.compare("type") == 0)
196  {
197  proper.setName(valName);
198  EnumType* tp = dataType->getEnum(v.second.data());
199  proper.setValue(tree.data(), tp);
200  prop.addSubProperty(proper);
201  }
202  else
203  {
204  std::string val = v.first;
205  valName = val;
206  tree = v.second;
207  }
208 
209  // recursive go down the hierarchy
210  retrieveSubPTree(tree, proper);
211  }
212 }
213 
215 {
216  return m_array.empty();
217 }
218 
219 void te::layout::JSON::loadFromPath( std::string loadPath )
220 {
221  m_loadPath = loadPath;
222 
223  std::ifstream inputFile;
224 
225  try
226  {
227  inputFile.open(m_loadPath.c_str());
228 
229  if (!inputFile.is_open())
230  return;
231 
232  boost::property_tree::json_parser::read_json(inputFile, m_array);
233  inputFile.close();
234  }
235  catch(boost::property_tree::json_parser::json_parser_error &je)
236  {
237  std::string errmsg = "Error parsing: " + je.filename() + ": " + je.message();
238  te::common::Exception ex(TE_TR(errmsg));
239  //throw(ex);
240  return;
241  }
242  catch (std::ifstream::failure &e)
243  {
244  std::cerr << e.what() << std::endl;
245  std::string errmsg = "Exception opening/reading/closing file: \n ";
246  te::common::Exception ex(TE_TR(errmsg));
247  //throw(ex);
248  return;
249  }
250  catch (std::exception const& e)
251  {
252  std::cerr << e.what() << std::endl;
253  }
254 }
255 
256 void te::layout::JSON::loadFromProperties( std::vector<te::layout::Properties*> properties )
257 {
258  m_properties = properties;
259 
260  std::vector<te::layout::Properties*>::iterator it;
261  std::vector<te::layout::Property>::iterator ity;
262 
263  boost::property_tree::ptree rootArray;
264  boost::property_tree::ptree childArray;
265 
266  rootArray.add("name", m_rootKey);
267 
268  int count = 0;
269  for(it = m_properties.begin(); it != m_properties.end(); ++it)
270  {
271  Properties* props = (*it);
272  if(!props)
273  continue;
274 
275  std::vector<te::layout::Property> vec = props->getProperties();
276 
277  if(vec.empty())
278  continue;
279 
280  childArray.clear();
281  childArray.add("object_type", props->getTypeObj()->getName());
282 
283  for(ity = vec.begin(); ity != vec.end(); ++ity)
284  {
285  Property prop = (*ity);
286 
287  childArray.add(prop.getName(), prop.getValue().convertToString());
288  childArray.add("type", prop.getType()->getName());
289 
290  searchProperty(prop, childArray, childArray);
291  }
292  if(!childArray.empty())
293  {
294  std::stringstream ss;//create a stringstream
295  ss << count;//add number to the stream
296 
297  std::string s_prop = "properties_"+ ss.str();
298  rootArray.push_back(std::make_pair(s_prop, childArray));
299  }
300  count+= 1;
301  }
302 
303  m_array.push_back(std::make_pair("template", rootArray));
304 }
305 
306 void te::layout::JSON::searchProperty( Property& property, boost::property_tree::ptree& array, boost::property_tree::ptree& child )
307 {
308  if(!property.getSubProperty().empty())
309  {
310  Property propCopy = property;
311 
312  std::vector<Property> props = property.getSubProperty();
313 
314  std::vector<Property>::iterator it;
315 
316  for(it = props.begin(); it != props.end(); ++it)
317  {
318  Property prop = (*it);
319 
320  boost::property_tree::ptree childArray;
321 
322  childArray.add(prop.getName(), prop.getValue().convertToString());
323  childArray.add("type", prop.getType()->getName());
324 
325  std::string s_name = prop.getName() + "_child";
326 
327  child.push_back(std::make_pair(s_name,childArray));
328 
329  searchProperty(prop, array, childArray);
330  }
331 
332  std::string s_nameChild = propCopy.getName() + "_child";
333  if(array != child)
334  array.push_back(std::make_pair(s_nameChild,child));
335  }
336 }
void clear()
Reset state of this object. Null state.
Definition: Property.cpp:194
std::string getName()
Method that returns the name of this property.
Definition: Property.cpp:57
virtual bool isEmpty()
Definition: JSON.cpp:214
Class to represent a graphic object (MVC component) and widget object (MVC widget) type enumeration...
virtual bool addProperty(Property property)
Adds the specified property to the set of properties for this object.
Definition: Properties.h:193
Variant getValue()
Returns stored value.
Definition: Property.cpp:72
virtual EnumDataType * getEnumDataType()
Returns data type enumeration.
Definition: Enums.cpp:52
void addSubProperty(Property property)
Definition: Property.cpp:124
std::string getName()
Returns name.
Definition: EnumType.cpp:54
virtual std::string convertToString()
Converts the value to a string.
Definition: Variant.cpp:368
virtual ~JSON()
Definition: JSON.cpp:54
The Properties class represents a persistent set of properties. The Properties can be saved to a file...
Definition: Properties.h:52
virtual bool serialize()
Definition: JSON.cpp:59
virtual std::vector< Property > getProperties()
Returns set of all properties.
Definition: Properties.h:220
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
virtual void searchProperty(Property &property, boost::property_tree::ptree &array, boost::property_tree::ptree &child)
Definition: JSON.cpp:306
virtual EnumObjectType * getEnumObjectType()
Returns graphic object (MVC component) and widget object (MVC widget) type enumeration.
Definition: Enums.cpp:62
virtual void setTypeObj(EnumType *type)
Sets object type that owns these properties.
Definition: Properties.h:246
virtual void fromPtree(boost::property_tree::ptree tree, EnumType *type)
Definition: Variant.cpp:214
static Enums & getInstance()
It returns a reference to the singleton instance.
virtual EnumType * getTypeObj()
Returns object type that owns these properties.
Definition: Properties.h:241
Class to represent a data type enumeration. Ex.: int, double, bool, te::color::RGBAColor (color)...
Definition: EnumDataType.h:48
Class acts like a union for some C++/TerraLib5 data types. Responsible for storing the value...
Definition: Variant.h:80
virtual EnumType * getEnum(int enumId) const
Searching for a value of the enumeration by id.
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
void setValue(ValueType value, EnumType *type)
Stores a copy of value.
Definition: Property.h:298
Class that represents the value of an enumeration. An enumeration is made of "1..n" objects EnumType...
Definition: EnumType.h:48
virtual void retrieveSubPTree(boost::property_tree::ptree subTree, Property &prop)
Definition: JSON.cpp:186
void setName(std::string name)
Sets the name of this property.
Definition: Property.cpp:62
Implementation of .json for Serialization. It is a JSON file. Save or change a file ...
virtual std::vector< te::layout::Properties * > retrieve()
Definition: JSON.cpp:104
virtual void loadFromPath(std::string loadPath)
Definition: JSON.cpp:219
std::vector< te::layout::Property > getSubProperty()
Definition: Property.cpp:141
virtual void loadFromProperties(std::vector< te::layout::Properties * > properties)
Definition: JSON.cpp:256
EnumType * getType()
Returns the type of this property.
Definition: Property.cpp:67
A property acts like a attribute member of a object and stores the state of this attribute. A set of properties stores the state of an object. Any data type, not included in the convertValue method in the class te::layout::Variant, it will be by default "std::string" value.
Definition: Property.h:47