Started work on JFile.

This commit is contained in:
Brad Arant 2025-12-04 17:04:55 -08:00
parent 829dc1a3c7
commit 4988f98223
9 changed files with 107 additions and 2 deletions

10
JFile.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "JFile.h"
#include "ZString.h"
#include "Exception.h"
namespace coreutils {
JFile::JFile(ZString filename) : MFile(filename) {}
}

25
JFile.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef __JFile_h__
#define __JFile_h__
#include "JString.h"
#include "MFile.h"
namespace coreutils {
///
/// Use the JFile object to create a JSON object that uses a file for its backing store.
/// The file name is provided at construction time. Contents of the file are updated
/// when the object is changed or the at the end of scope for the object.
///
class JFile : public MFile, public JString {
public:
JFile(ZString filename);
};
}
#endif

View File

@ -28,7 +28,7 @@ namespace coreutils {
/// std::cout << jstring["key"];
///
class JString : public MString {
class JString : virtual public MString {
public:

View File

@ -13,7 +13,7 @@
namespace coreutils {
class MFile : public MString {
class MFile : virtual public MString {
public:
MFile(MString fileName);

View File

@ -3,3 +3,4 @@ g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils -lb64
g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils
g++ -g -std=c++20 -o mfile_test mfile_test.cpp -I.. -L.. -lCoreUtils
g++ -g -std=c++20 -o jfile_test jfile_test.cpp -I.. -L.. -lCoreUtils

68
testing/jfile_test.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <iostream>
#include "../JString.h"
#include "../JFile.h"
#include <string>
#include <sstream>
int main(int argc, char **argv) {
coreutils::MString test0("{ \"Number\": \"0\", \"id\": \"XXXXX\" }");
coreutils::JFile test1("jfile_test.data");
coreutils::JString test9;
test1 = test0;
std::cout << test1 << std::endl;
std::cout << test1["id"] << std::endl;
test1["name"] = "Cohen";
std::cout << test1 << std::endl;
test1["health"] = "100";
test1["comment"] = "this is a comment";
test1["racex"] = "elvin";
test1["race"] = "human";
test1["array"] = "[\"test1\",\"test2\",\"test3\"]";
test1["tester"] = "test field";
test1["object1"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}";
test1["object1.attr2"] = "{\"xattr1\":\"xvalue1\",\"xattr2\":\"xvalue2\",\"xattr3\":\"xvalue3\"}";
test1["object1.attr3"] = "Im not an object";
// test1["age"] = 64; future
std::cout << test1 << std::endl;
coreutils::ZString test2 = test1["name"];
std::cout << test2 << std::endl;
std::cout << test1["name"] << std::endl;
std::cout << test1["health"] << std::endl;
std::cout << test1["comment"] << std::endl;
std::cout << test1["racex"] << std::endl;
std::cout << test1["race"] << std::endl;
std::cout << test1["array"] << std::endl;
std::cout << test1["tester"] << std::endl;
std::cout << test1["object1"] << std::endl;
std::cout << test1["object1.attr2"] << std::endl;
std::cout << test1["object1.attr2.xattr3"] << std::endl;
std::cout << test1["object1.attr3"] << std::endl;
std::cout << test1["array[0]"] << std::endl;
std::cout << test1["array[1]"] << std::endl;
std::cout << test1["array[2]"] << std::endl;
std::string string1 = test1["tester"];
std::cout << "from string: " << string1 << std::endl;
coreutils::MString testout;
testout << "this is a test: " << test1["comment"];
std::cout << testout << std::endl;
test9 = test1["object1"];
std::cout << test9 << std::endl;
std::cout << test9["attr3"] << std::endl;
std::cout << test1.pretty() << std::endl;
return 0;
}

1
testing/jfile_test.data Normal file
View File

@ -0,0 +1 @@
{}

Binary file not shown.

Binary file not shown.