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