diff --git a/JFile.cpp b/JFile.cpp new file mode 100644 index 0000000..4cfac59 --- /dev/null +++ b/JFile.cpp @@ -0,0 +1,10 @@ +#include "JFile.h" +#include "ZString.h" +#include "Exception.h" + +namespace coreutils { + + JFile::JFile(ZString filename) : MFile(filename) {} + + +} diff --git a/JFile.h b/JFile.h new file mode 100644 index 0000000..8dc3a99 --- /dev/null +++ b/JFile.h @@ -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 diff --git a/JString.h b/JString.h index 0890c0d..14f59e7 100644 --- a/JString.h +++ b/JString.h @@ -28,7 +28,7 @@ namespace coreutils { /// std::cout << jstring["key"]; /// - class JString : public MString { + class JString : virtual public MString { public: diff --git a/MFile.h b/MFile.h index 25e9384..2d47f32 100644 --- a/MFile.h +++ b/MFile.h @@ -13,7 +13,7 @@ namespace coreutils { - class MFile : public MString { + class MFile : virtual public MString { public: MFile(MString fileName); diff --git a/testing/compile b/testing/compile index b305a03..9bf376e 100755 --- a/testing/compile +++ b/testing/compile @@ -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 diff --git a/testing/jfile_test.cpp b/testing/jfile_test.cpp new file mode 100644 index 0000000..177add5 --- /dev/null +++ b/testing/jfile_test.cpp @@ -0,0 +1,68 @@ +#include +#include "../JString.h" +#include "../JFile.h" +#include +#include + +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; +} + diff --git a/testing/jfile_test.data b/testing/jfile_test.data new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/testing/jfile_test.data @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/testing/jstring_test b/testing/jstring_test index 74d88c6..689ee78 100755 Binary files a/testing/jstring_test and b/testing/jstring_test differ diff --git a/testing/mfile_test b/testing/mfile_test index e91b3a4..92678a8 100755 Binary files a/testing/mfile_test and b/testing/mfile_test differ