read and write to Json files
Added by Malin Edvardsen almost 12 years ago
Hello there Wt folks!
So, as you might guess form the title, yes I am not to good at this :P I have tried to figure out how to do this from the wt documentation, but I am not quite sure if I am doing things correctly or not. What I want (or a simplified explanation of what I need) is to be able to read data from a json file, put some of the results into a editable table and then be able to write the contents of that table back to the json files again.
My idea was then to make a class like this:
JsonFile.h
class JsonFile : public Wt::WResource {
public:
JsonFile(const Wt::WString & fileName);
virtual ~JsonFile();
protected:
Wt::Json::Object jsonObject;
void handleRequest(const Wt::Http::Request& request,
Wt::Http::Response& response);
};
JsonFile.cpp
JsonFile::JsonFile(const Wt::WString & fileName)
{
suggestFileName(fileName);
}
JsonFile::~JsonFile()
{
beingDeleted();
}
void JsonFile::handleRequest(const Wt::Http::Request& request,
Wt::Http::Response& response)
{
Wt::Json::parse(??????,
jsonObject);
}
Then I will later have different classes that inherit JsonFile and that will organize the content into the table depending on the class type. Am I no the right track of something here?
Now, as you might see, the part where I get a bit confused is the handleRequest function.. I did see the examples here http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WResource.html
but I guess I am just not clever enough to make a lot of sense out of it. How should I use it, how should I call it? (yes I know there is a lot of issues with several people using the app at the same time, reading and writing to these files, but I have plans on how to handle that issue, required login and separate git branches included in these plans). So if someone could help me in the right direction here, I would be trilled!
Regards, new Wt user
Malin
Replies (2)
RE: read and write to Json files - Added by Malin Edvardsen almost 12 years ago
Or might I be wrong using WResource for this, and should instead just use std::ifstream?
RE: read and write to Json files - Added by Koen Deforche almost 12 years ago
Hey,
Our json interface probably should be extended to allow reading from an std::istream too.
But the following can help you in the mean time:
std::ifstream f("file.json");
std::istreambuf_iterator<char> eos;
std::string s(std::istreambuf_iterator<char>(f), eos);
Wt::Json::Object o;
Wt::Json::ParseError e;
if (Wt::Json::parse(response.body(), o, e)) {
...
}
Note, writing out JSon is a feature request currently pending in our tracker.
Regards,
koen