2017-11-15 4 views
0

私はCerealを初めて使用しており、JSON文字列を逆シリアル化する方法を理解していません。残念ながら、私の仕事のファイアウォールは、Google Groupsにアクセスしてメッセージボードに投稿することを制限しています。穀物を使用してJSON文字列を逆シリアル化する

私はJSON文字列に変換できますが、私の人生では文字列を受け取りクラスを再作成できないクラスがあります。どんな助けもありがたいですし、優しくしてください。私はちょうど学校を離れました。それは私の最初の仕事です。私は自分の深みを乗り越えています。

これは私が取得していますエラーです:内部アサーションの失敗rapidjson:

は何() '穀物:: RapidJSONException' のインスタンスを投げた後に呼び出さTERMINATE ISOBJECT()以下

は、MyClassのです.HPPクラス:以下

#include <cstdio> 
#include <iostream> 
#include <string> 
#include "../cereal/access.hpp" 

class MyClass{ 
Public: //function declarations 
    template<class Archive> // public serialization (normal) 
    void serialize(Archive & ar) 
    { 
     ar(x, y, z); 
    } 

private: // member variables 
    string x; 
    int y; 
    bool z; 
}; 

は私MAIN.CPPです:

#include <../cereal/types/unordered_map.hpp> 
#include <../cereal/types/memory.hpp> 
#include <../cereal/types/concepts/pair_associative_container.hpp> 
#include <../cereal/archives/json.hpp> 
#include <../cereal/types/vector.hpp> 

#include <iostream> 
#include <fstream> 

#include "MyClass.hpp" 

int main(){ 
    // serialize (this part all works... i think) 
    { 
     // create a class object and give it some data 
     MyClass data("hello", 6, true); 
     // create a string stream object 
     std::stringstream os; 
     // assign the string stream object to the Output Archive 
     cereal::JSONOutputArchive archive_out(os); 
     //write data to the output archive 
     archive_out(CEREAL_NVP(data)); 
     // write the string stream buffer to a variable 
     string json_str = os.str(); 
    } 
    // deserialize 
    { 
     // create a string stream object and pass it the string variable holding the JSON archive 
     std::stringstream is(json_str); 
     // pass the stream sting object into the input archive **** this is the line of code that generates the error 
     cereal::JSONInputArchive archive_in(is); 
     // create a new object of MyClass 
     MyClass data_new; 
     // use input archive to write data to my class 
     archive_in(data_new); 
    } 
} 
穀物で
+0

何が 'json_str'ですか?ブロックを使用する前に終了するブロック内で、非標準の型として宣言しました。 –

答えて

0

穀物のドキュメントごとなど、いくつかのアーカイブは唯一の安全に破壊するとその内容をフラッシュし終了することができます。特に出力シリアル化では、アーカイブが終了したら自動的に破棄されるようにしてください。

だから、(os.str呼び出すことimporantある)シリアライズブロックの外側、即ち

MyClass data("hello", 6, true); 
std::stringstream os; 
{ 
    cereal::JSONOutputArchive archive_out(os); 
    archive_out(CEREAL_NVP(data)); 
} 
string json_str = os.str(); 
cout << json_str << endl; 

// deserialize 
std::stringstream is(json_str); 
MyClass data_new; 
{ 
    cereal::JSONInputArchive archive_in(is); 
    archive_in(data_new); 
    cout << data_new.y << endl; 
} 

これは、コードを動作しています。必要に応じてさらに変更することができます

関連する問題