StdAir Logo  0.43.0
C++ Standard Airline IT Library
BomJSONImport.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 // Boost Property Tree
00008 #include <boost/property_tree/ptree.hpp>
00009 #include <boost/property_tree/json_parser.hpp>
00010 // StdAir
00011 #include <stdair/bom/BomJSONImport.hpp>
00012 
00013 namespace bpt = boost::property_tree;
00014 
00015 namespace stdair {
00016 
00017   // ////////////////////////////////////////////////////////////////////
00018   bool BomJSONImport::jsonImportInventoryKey (const std::string& iBomTree,
00019                                               AirlineCode_T& ioAirlineCode) {
00020     bool hasKeyBeenSuccessfullyRetrieved = true;
00021 
00022     // Create an empty property tree object
00023     bpt::ptree pt;
00024 
00025     try {
00026 
00027       // Load the JSON formatted string into the property tree.
00028       // If reading fails (cannot open stream, parse error), an
00029       // exception is thrown.
00030       std::istringstream iStr (iBomTree);
00031       read_json (iStr, pt);
00032 
00033       // Get the airline_code and build an InventoryKey structure.
00034       // If the flight_date.airline_code key is not found, an
00035       // exception is thrown.
00036       ioAirlineCode = pt.get<AirlineCode_T> ("flight_date.airline_code");
00037 
00038     } catch (bpt::ptree_error& bptException) {
00039       hasKeyBeenSuccessfullyRetrieved = false;
00040     }
00041 
00042     return hasKeyBeenSuccessfullyRetrieved;
00043   }
00044 
00045   // ////////////////////////////////////////////////////////////////////
00046   bool BomJSONImport::jsonImportFlightDateKey (const std::string& iBomTree,
00047                                                FlightNumber_T& ioFlightNumber,
00048                                                Date_T& ioDepartureDate) {
00049     bool hasKeyBeenSuccessfullyRetrieved = false;
00050 
00051     // Create an empty property tree object
00052     bpt::ptree pt;
00053 
00054     try {
00055 
00056       // Load the JSON formatted string into the property tree.
00057       // If reading fails (cannot open stream, parse error), an
00058       // exception is thrown.
00059       std::istringstream iStr (iBomTree);
00060       read_json (iStr, pt);
00061 
00062       // Get the flight_number and departure_date and build an
00063       // FlightDateKey structure.
00064       ioFlightNumber = pt.get<FlightNumber_T> ("flight_date.flight_number");
00065 
00066       const std::string& lDepartureDateStr =
00067         pt.get<std::string> ("flight_date.departure_date");
00068       ioDepartureDate = boost::gregorian::from_simple_string (lDepartureDateStr);
00069 
00070     } catch (bpt::ptree_error& bptException) {
00071       hasKeyBeenSuccessfullyRetrieved = false;
00072     }
00073 
00074     return hasKeyBeenSuccessfullyRetrieved;
00075   }
00076 
00077 }