Ich bin neu bei Cereal und es fällt mir schwer zu verstehen, wie eine JSON-Zeichenfolge deserialisiert wird. Leider verhindert meine Arbeitsfirewall, dass ich auf Google Groups zugreife, um auf dem Forum zu posten.Verwenden von Cerealien zum Deserialisieren einer JSON-Zeichenfolge
Ich habe eine Klasse, die ich in eine JSON-Zeichenfolge verwandeln kann, kann aber nicht für das Leben von mir die Zeichenfolge nehmen und die Klasse neu erstellen. Jede Hilfe wäre willkommen und bitte seid sanft, ich bin gerade aus der Schule gekommen, es ist meine erste Arbeit, und ich bin weit weg von meiner Tiefe.
Dies ist der Fehler Ich erhalte:
nach dem Werfen einer Instanz genannt terminate 'Getreide :: RapidJSONException' was(): rapidjson interne Assertionsfehler: IsObject()
Im Folgenden finden Sie MyClass .hpp Klasse:
#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;
};
Unten ist mein 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);
}
}
Was ist 'json_str'? Sie haben es als nicht-std-qualifizierten Typ in einem Block deklariert, der endet, bevor Sie ihn verwenden. –