Commit 2b13c467 authored by luz's avatar luz

p44utils: JsonObject: added methods to load JSON from file or save JSON to file.

parent eeb17c3f
......@@ -27,6 +27,8 @@
#include <json/json_object_private.h> // needed for _ref_count
#endif
#include <sys/stat.h> // for fstat
using namespace p44;
......@@ -57,6 +59,52 @@ JsonObject::~JsonObject()
}
#pragma mark - read and write from files
#define MAX_JSON_FILE_SIZE 200000
// factory method, create JSON object from file
JsonObjectPtr JsonObject::objFromFile(const char *aJsonFilePath)
{
// read file into string
int fd = open(aJsonFilePath, O_RDONLY);
if (fd>=0) {
// opened, now read
struct stat fs;
fstat(fd, &fs);
if (fs.st_size<MAX_JSON_FILE_SIZE) {
char *jsontext = new char[fs.st_size];
read(fd, jsontext, fs.st_size);
JsonObjectPtr json = JsonObject::objFromText(jsontext,fs.st_size);
delete jsontext;
return json;
}
}
// could not open
return JsonObjectPtr(); // nothing read
}
ErrorPtr JsonObject::saveToFile(const char *aJsonFilePath)
{
int fd = open(aJsonFilePath, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd<0) {
return SysError::errNo("Cannot open file to save JSON: ");
}
else {
const char *jsontext = json_c_str();
if (write(fd, jsontext, strlen(jsontext))<0) {
close(fd);
return SysError::errNo("Error writing JSON: ");
}
// success
close(fd);
}
return ErrorPtr();
}
#pragma mark - type
......
......@@ -30,9 +30,6 @@
#include <json/json.h>
#endif
#include <boost/intrusive_ptr.hpp>
using namespace std;
......@@ -159,6 +156,12 @@ namespace p44 {
/// create new object from text
static JsonObjectPtr objFromText(const char *aJsonText, ssize_t aMaxChars = -1);
/// create new object from text file
static JsonObjectPtr objFromFile(const char *aJsonFilePath);
/// save object to text file
ErrorPtr saveToFile(const char *aJsonFilePath);
/// create new array object
static JsonObjectPtr newArray();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment