$treeview $search $mathjax
00001 // ////////////////////////////////////////////////////////////////////// 00002 // Import section 00003 // ////////////////////////////////////////////////////////////////////// 00004 // Boost Property Tree 00005 #include <boost/property_tree/ptree.hpp> 00006 #include <boost/property_tree/json_parser.hpp> 00007 // Boost ForEach 00008 #include <boost/foreach.hpp> 00009 // AirInvServer 00010 #include <airinv/server/BomPropertyTree.hpp> 00011 00012 namespace bpt = boost::property_tree; 00013 00014 namespace stdair { 00015 00016 // Loads BomPropertyTree structure from the specified JSON file 00017 void BomPropertyTree::load (const std::string& iBomTree) { 00018 // Create an empty property tree object 00019 bpt::ptree pt; 00020 00021 // Load the JSON formatted string into the property tree. If reading fails 00022 // (cannot open stream, parse error), an exception is thrown. 00023 std::istringstream iStr (iBomTree); 00024 read_json (iStr, pt); 00025 00026 // Get the airline_code and store it in the _airlineCode variable. 00027 // Note that we construct the path to the value by separating 00028 // the individual keys with dots. If dots appear in the keys, 00029 // a path type with a different separator can be used. 00030 // If the flight_date.airline_code key is not found, an exception is thrown. 00031 _airlineCode = pt.get<stdair::AirlineCode_T> ("flight_date.airline_code"); 00032 00033 // Get the departure_date and store it in the _departureDate variable. 00034 // This is another version of the get method: if the value is 00035 // not found, the default value (specified by the second 00036 // parameter) is returned instead. The type of the value 00037 // extracted is determined by the type of the second parameter, 00038 // so we can simply write get(...) instead of get<int>(...). 00039 _flightNumber = 00040 pt.get<stdair::FlightNumber_T> ("flight_date.flight_number", 100); 00041 00042 const std::string& lDepartureDateStr = 00043 pt.get<std::string> ("flight_date.departure_date"); 00044 _departureDate = boost::gregorian::from_simple_string (lDepartureDateStr); 00045 00046 // Iterate over the flight_date.airport_codes section and store all found 00047 // codes in the _airportCodeList set. The get_child() function 00048 // returns a reference to the child at the specified path; if 00049 // there is no such child, it throws. Property tree iterators 00050 // are models of BidirectionalIterator. 00051 /* 00052 BOOST_FOREACH (bpt::ptree::value_type &v, 00053 pt.get_child ("flight_date.airport_codes")) { 00054 _airportCodeList.insert (v.second.data()); 00055 } 00056 */ 00057 } 00058 00059 // Saves the BomPropertyTree structure to the specified JSON file 00060 std::string BomPropertyTree::save() const { 00061 std::ostringstream oStr; 00062 00063 // Create an empty property tree object 00064 bpt::ptree pt; 00065 00066 // Put airline code in property tree 00067 pt.put ("flight_date.airline_code", _airlineCode); 00068 00069 // Put flight number level in property tree 00070 pt.put ("flight_date.flight_number", _flightNumber); 00071 00072 // Put the flight departure date in property tree 00073 const std::string& lDepartureDateStr = 00074 boost::gregorian::to_simple_string (_departureDate); 00075 pt.put ("flight_date.departure_date", lDepartureDateStr); 00076 00077 // Iterate over the airport codes in the set and put them in the 00078 // property tree. Note that the put function places the new 00079 // key at the end of the list of keys. This is fine most of 00080 // the time. If you want to place an item at some other place 00081 // (i.e. at the front or somewhere in the middle), this can 00082 // be achieved using a combination of the insert and put_own 00083 // functions. 00084 bpt::ptree lAirportCodeArray; 00085 BOOST_FOREACH (const std::string& name, _airportCodeList) { 00086 lAirportCodeArray.push_back (std::pair<bpt::ptree::key_type, 00087 bpt::ptree::data_type> ("", name)); 00088 } 00089 pt.put_child ("flight_date.airport_codes", lAirportCodeArray); 00090 //pt.push_back (std::make_pair ("flight_date.airport_codes", lAirportCodeArray)); 00091 00092 // Write the property tree to the JSON stream. 00093 write_json (oStr, pt); 00094 00095 return oStr.str(); 00096 } 00097 00098 }