StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BomJSONImport.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 #if BOOST_VERSION >= 104100
8 // Boost Property Tree
9 #include <boost/property_tree/ptree.hpp>
10 #include <boost/property_tree/json_parser.hpp>
11 #endif // BOOST_VERSION >= 104100
12 // StdAir
14 
15 #if BOOST_VERSION >= 104100
16 namespace bpt = boost::property_tree;
17 #else // BOOST_VERSION >= 104100
18 namespace bpt {
19  typedef char ptree;
20 }
21 #endif // BOOST_VERSION >= 104100
22 
23 namespace stdair {
24 
25  // ////////////////////////////////////////////////////////////////////
26  bool BomJSONImport::jsonImportInventoryKey (const std::string& iBomTree,
27  AirlineCode_T& ioAirlineCode) {
28  bool hasKeyBeenSuccessfullyRetrieved = true;
29 
30 #if BOOST_VERSION >= 104100
31  // Create an empty property tree object
32  bpt::ptree pt;
33 
34  try {
35 
36  // Load the JSON formatted string into the property tree.
37  // If reading fails (cannot open stream, parse error), an
38  // exception is thrown.
39  std::istringstream iStr (iBomTree);
40  read_json (iStr, pt);
41 
42  // Get the airline_code and build an InventoryKey structure.
43  // If the flight_date.airline_code key is not found, an
44  // exception is thrown.
45  ioAirlineCode = pt.get<AirlineCode_T> ("flight_date.airline_code");
46 
47  } catch (bpt::ptree_error& bptException) {
48  hasKeyBeenSuccessfullyRetrieved = false;
49  }
50 #endif // BOOST_VERSION >= 104100
51 
52  return hasKeyBeenSuccessfullyRetrieved;
53  }
54 
55  // ////////////////////////////////////////////////////////////////////
56  bool BomJSONImport::jsonImportFlightDateKey (const std::string& iBomTree,
57  FlightNumber_T& ioFlightNumber,
58  Date_T& ioDepartureDate) {
59  bool hasKeyBeenSuccessfullyRetrieved = false;
60 
61 #if BOOST_VERSION >= 104100
62  // Create an empty property tree object
63  bpt::ptree pt;
64 
65  try {
66 
67  // Load the JSON formatted string into the property tree.
68  // If reading fails (cannot open stream, parse error), an
69  // exception is thrown.
70  std::istringstream iStr (iBomTree);
71  read_json (iStr, pt);
72 
73  // Get the flight_number and departure_date and build an
74  // FlightDateKey structure.
75  ioFlightNumber = pt.get<FlightNumber_T> ("flight_date.flight_number");
76 
77  const std::string& lDepartureDateStr =
78  pt.get<std::string> ("flight_date.departure_date");
79  ioDepartureDate = boost::gregorian::from_simple_string (lDepartureDateStr);
80 
81  } catch (bpt::ptree_error& bptException) {
82  hasKeyBeenSuccessfullyRetrieved = false;
83  }
84 #endif // BOOST_VERSION >= 104100
85 
86  return hasKeyBeenSuccessfullyRetrieved;
87  }
88 
89 }