diff --git a/JString.cpp b/JString.cpp index 0131f4e..1c8c5ee 100644 --- a/JString.cpp +++ b/JString.cpp @@ -7,7 +7,7 @@ namespace coreutils { JString::JString(const char *data) : MString(data) {} - void JString::set(ZString path, const char *value) { + void JString::set(ZString &path, ZString &value) { reset(); if(locate(path)) { push(); @@ -32,46 +32,21 @@ namespace coreutils { } } - ZString JString::find(ZString path) { + ZString JString::find(ZString &path) { + reset(); if(locate(path)) { if(ifNext("\"")) return getTokenExclude("\""); // TODO: Handle numbers that are not in double quotes. if(startsWith("[")) { - ZString temp = getContainer(); - std::cout << temp << std::endl; + std::cout << getContainer() << std::endl; } } return ZString(); } - void JString::operator=(ZString &value) { - std::cout << "operator=(ZString &)" << value << "\n"; - } - - JString& JString::operator=(const char *value) { - std::cout << "operator=" << value << "\n"; - set(path, value); - return *this; - } - - // ZString JString::operator[](const char *path) { - // std::cout << "operator[" << path << "]\n"; - // this->path = path; - // reset(); - // return find(path); - // } - - JString& JString::operator[](const char *path) { - std::cout << "operator[" << path << "]\n"; - reset(); - this->path = path; - find(path); - return *this; - } - - bool JString::locate(ZString path) { + bool JString::locate(ZString &path) { path.split(".", 1); notfirst = false; if(ifNext("{")) { @@ -122,5 +97,5 @@ namespace coreutils { } } return false; - } + } } diff --git a/JString.h b/JString.h index 5745e90..0a16dca 100644 --- a/JString.h +++ b/JString.h @@ -35,32 +35,47 @@ namespace coreutils { JString(); JString(const char *data); - void set(ZString path, const char *value); + void set(ZString &path, ZString &value); /// /// Use the find method to look through an object for member specified by - /// the key in parameter 1. The cursor must be pointing to the { character + /// the key in parameter 1. The cursor must be pointing to the { character /// that begins the object's list. /// - ZString find(ZString path); - - void operator=(ZString &value); - JString& operator=(const char *value); - - JString& operator[](const char *path); - // operator const ZString () const { - // return *this; - // } - - // MString & operator[](ZString &key); - - // MString & operator[](std::string key); + ZString find(ZString &path); private: - bool locate(ZString path); + bool locate(ZString &path); bool notfirst; - MString path; + ZString path; + + class Proxy { + + public: + Proxy(JString &jstring, ZString key) : jstring(jstring), key(key) {} + + operator coreutils::ZString () { + ZString result; + result = jstring.find(key); + return result; + } + + Proxy& operator=(coreutils::ZString value) { + jstring.set(key, value); + return *this; + } + + private: + JString &jstring; + ZString key; + }; + + public: + + Proxy operator[](coreutils::ZString key) { + return Proxy(*this, key); + } }; diff --git a/includes_deprecated b/includes_deprecated deleted file mode 100644 index a18c5f3..0000000 --- a/includes_deprecated +++ /dev/null @@ -1,39 +0,0 @@ - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - \ No newline at end of file diff --git a/testing/jstring_test b/testing/jstring_test index 17e2064..4517133 100755 Binary files a/testing/jstring_test and b/testing/jstring_test differ diff --git a/testing/jstring_test.cpp b/testing/jstring_test.cpp index 83e0ac0..ac4fc64 100644 --- a/testing/jstring_test.cpp +++ b/testing/jstring_test.cpp @@ -16,20 +16,37 @@ int main(int argc, char **argv) { // test1.set("key4", "data4"); // std::cout << test1 << std::endl; - coreutils::JString test2("{\"key1\":[{\"id\":\"1\",\"name\":\"Brad\"},{\"id\":\"2\",\"name\":\"Jenn\"},{\"id\":\"3\",\"name\":\"Skye\"}],\"key2\":\"data5\",\"key3\":\"data3\",\"key4\":\"data4\"}"); - std::cout << test2 << std::endl; - std::cout << "--------" << std::endl; +// coreutils::JString test2("{\"key1\":[{\"id\":\"1\",\"name\":\"Brad\"},{\"id\":\"2\",\"name\":\"Jenn\"},{\"id\":\"3\",\"name\":\"Skye\"}],\"key2\":\"data5\",\"key3\":\"data3\",\"key4\":\"data4\"}"); +// std::cout << test2 << std::endl; +// std::cout << "--------" << std::endl; // std::cout << test2["key2"] << std::endl; // std::cout << test2["key1"] << std::endl; // std::cout << "value: " << test2["key1.name[id=2]"] << std::endl; // std::cout << "value: " << test2["key1.name[id=3]"] << std::endl; - test2["key2"] = "newvalue1"; - std::cout << "--------" << std::endl; +// test2["key2"] = "newvalue1"; +// std::cout << "--------" << std::endl; // test2 = "newvalue"; +// std::cout << test2 << std::endl; +// std::cout << "--------" << std::endl; +// std::cout << "key2: " << test2["key2"] << std::endl; + + coreutils::JString test1; + coreutils::MString test2; + test1["name"] = "Cohen"; + test1["health"] = "100"; + test1["comment"] = "this is a comment"; + test1["race"] = "human"; + std::cout << test1 << std::endl; + test2 = test1["name"]; std::cout << test2 << std::endl; - std::cout << "--------" << std::endl; - std::cout << "key2: " << test2["key2"] << std::endl; + test2 = test1["health"]; + std::cout << test2 << std::endl; + test2 = test1["comment"]; + std::cout << test2 << std::endl; + test2 = test1["race"]; + std::cout << test2 << std::endl; +// std::cout << "[" << test1["name"] << std::endl; return 0; } diff --git a/testing/mstring_test b/testing/mstring_test index e2ffe30..abc082a 100755 Binary files a/testing/mstring_test and b/testing/mstring_test differ diff --git a/testing/mstring_test.cpp b/testing/mstring_test.cpp index 453c16c..26ac054 100644 --- a/testing/mstring_test.cpp +++ b/testing/mstring_test.cpp @@ -64,7 +64,9 @@ int main(int argc, char **argv) { coreutils::MString test21; test21 = test20; std::cout << "mstring = zstring: [" << test21 << "]" << std::endl; - + coreutils::MString test22; + test22 << test20; + std::cout << "mstring << zstring: [" << test22 << "]" << std::endl; return 0; }