如何通过网络序列化并在JSOn字符串中发送std :: list?

时间:2015-03-23 15:31:14

标签: c++ json c++11 serialization boost

我正在尝试通过网络发送 std :: list 容器内的数据列表。我没有单独发送列表中的每个元素,而是试图一次性发送它。为此我使用 boost :: serialization 。我使用以下代码编译:

g++ -o main main.cpp DBAccess11.cpp -lsqlite3 -lboost_serialization

我接受了this example的帮助,这与我正在寻求的非常相似。

虽然,基于示例,我成功编译了我的程序。但不幸的是,我没有看到任何序列化输出。

以下是我的 DBAccess1.h 文件。

#ifndef DBAccess1_HH  
#define DBAccess1_HH

#include <iostream>  // I have deleted some header for sake of readability
#include <list>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/list.hpp>

using namespace std;    
//================================//   
struct SiteCode
{
      int siteID;
      int siteCode;
  private:
      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive & ar, const unsigned int /*version*/)
      {
            ar & siteID;
            ar & siteCode;
      }
 };

inline ostream& operator<< (ostream &out, SiteCode &site)
{
    out << "(" << site.siteID << "," << site.siteCode << ")";
    return out;
}
//================================//

class sqliteDB {
    list<SiteCode> Site_Code_list;
public:
    list<SiteCode> GET_ALL_Site_Code();
    void printList();
};
#endif**

以下是 DBAccess11.cpp 文件,其中定义了所有功能

#include <iostream>
#include <sqlite3.h>
#include <list>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/list.hpp>
#include "DBAccess1.h"

list<SiteCode> sqliteDB::GET_ALL_Site_Code()
{
        sqlite3 *db;
        const char *sql;
        sqlite3_stmt * stmt;

        int rc = sqlite3_open("/path/to/database.db", &db);
        sql = "SELECT * FROM SiteCode;";       
        rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);

    while(sqlite3_step(stmt)==SQLITE_ROW) {

            int A  = sqlite3_column_int(stmt, 0);
            int B = sqlite3_column_int(stmt, 1);

            SiteCode info;
            info.siteID = A;
            info.siteCode = B;              

            cout<<"Preparing to push data into List"<<endl;
            Site_Code_list.push_back(info);
            cout<<"Data was pushed successfully"<<endl;

            std::stringstream out;

            // serialize into the stream
            {
                boost::archive::binary_oarchive oa(out);
                oa << Site_Code_list;
//line-84>>>    cout<< oa << endl;
            }
        }
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    return Site_Code_list;
}
//====================================================//
void sqliteDB::printList()
{
     int s = Site_Code_list.size();
     cout << "The size of List is :" << s << endl;
     for( list<SiteCode> :: iterator it = Site_Code_list.begin(); it !=  Site_Code_list.end(); it++)     
     cout << *it << " ";
}

以下是 main.cpp

#include <iostream>
#include <list>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/list.hpp>
#include "DBAccess1.h"

using namespace std ;

int main()
{
    sqliteDB object1;
    object1.GET_ALL_Site_Code();
    object1.printList();
    cout << "\n\nAll the statement were executed properly\n\n";
    return 0;
}

如果未包含 DBAccess11.cpp LINE-84 ,此代码将完美运行。但我没有看到序列化输出。

没有LINE-84的输出如下:

Preparing to push data into List
Data was pushed successfully
Preparing to push data into List
Data was pushed successfully
Preparing to push data into List
Data was pushed successfully
Preparing to push data into List
Data was pushed successfully
Preparing to push data into List
Data was pushed successfully
Preparing to push data into List
Data was pushed successfully
Preparing to push data into List
Data was pushed successfully
Preparing to push data into List
Data was pushed successfully
Preparing to push data into List
Data was pushed successfully
Preparing to push data into List
Data was pushed successfully
The size of List is :10
(7,786) (8,78) (9,785) (10,998) (11,656) (13,23) (14,7) (15,74) (16,954) (17,752) 

当我尝试 cout&lt;&lt; oa 它显示了数百个错误。第一个错误如下。

DBAccess11.cpp: In member function ‘std::list<SiteCode> sqliteDB::GET_ALL_Site_Code()’:
DBAccess11.cpp:81:9: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘boost::archive::binary_oarchive’)
    cout << oa << endl;
            ^

我知道这是运算符重载问题。但是,我想要的只是看到网络上的序列化数据。我该怎么做呢 ?请帮助。

1 个答案:

答案 0 :(得分:0)

有必要输出cout out类的std::stringstream实例,而不是oa实例,即

std::stringstream out;

            // serialize into the stream
            {
                boost::archive::binary_oarchive oa(out);
                oa << Site_Code_list;
//line-84>>>    cout<< oa << endl;
            }
cout << out.str() << endl;

请注意,oa变量具有binary_oarchive类型,即二进制类型,输出将无法读取。要查看有用的内容,必须使用boost::archive::xml_oarchiveboost::archive::text_oarchive类型。