messing around with jstring.

This commit is contained in:
barant 2024-12-10 17:10:49 -08:00
parent c644d8346f
commit 0baa44d076
6 changed files with 148 additions and 74 deletions

View File

@ -5,7 +5,7 @@ namespace coreutils {
JString::JString() : MString("{}") {}
JString::JString(const char *data) : MString(data) {}
// JString::JString(const char *data) : MString(data) {}
void JString::set(ZString &path, ZString &value) {
reset();
@ -23,11 +23,21 @@ namespace coreutils {
throw coreutils::Exception("Fix me to accept integer type.");
}
} else {
std::cout << "[" << unparsed() << "]" << std::endl;
std::cout << "[" << unparsed() << "] >" << value << "<" << std::endl;
MString key;
if(notfirst)
if(notfirst)
key << ",";
key << "\"" << path << "\":" << "\"" << value << "\"";
// TODO: The value added must be parsed down to remove all whitespace.
value.push();
value.skipWhitespace();
if(value.startsWith("["))
key << "\"" << path << "\":" << value;
else if(value.startsWith("{"))
key << "\"" << path << "\":" << value;
else
key << "\"" << path << "\":" << "\"" << value << "\"";
value.pop();
insert(key, offset());
}
}
@ -63,14 +73,18 @@ namespace coreutils {
if(label == path[0]) {
if(path.getList().size() == 1)
{
// std::cout << "cursor: " << unparsed() << std::endl;
// std::cout << "found cursor: " << unparsed() << std::endl;
return true;
}
return locate(path[1]);
} if(startsWith("\"")) {
getContainer();;
}
// std::cout << "+++" << unparsed() << std::endl;
if(startsWith("\"")) {
getContainer();
} else if(startsWith("[")) {
getContainer();
} else if(startsWith("{")) {
getContainer();
} else {
throw coreutils::Exception("Unrecognized data type.");
}
@ -96,6 +110,7 @@ namespace coreutils {
}
}
}
// std::cout << "not found cursor: " << unparsed() << std::endl;
return false;
}
}

View File

@ -33,7 +33,7 @@ namespace coreutils {
public:
JString();
JString(const char *data);
// JString(const char *data);
void set(ZString &path, ZString &value);
@ -44,40 +44,31 @@ namespace coreutils {
///
ZString find(ZString &path);
private:
bool locate(ZString &path);
bool notfirst;
ZString path;
class Proxy {
public:
Proxy(JString &jstring, ZString key) : jstring(jstring), key(key) {}
operator coreutils::ZString () {
return jstring.find(key);
}
Proxy& operator=(coreutils::ZString value) {
jstring.set(key, value);
return *this;
}
private:
JString &jstring;
ZString key;
};
ZString keyx;
public:
Proxy operator[](coreutils::ZString key) {
return Proxy(*this, key);
operator ZString () {
std::cout << "operator0" << std::endl;
return find(keyx);
}
JString &operator[](coreutils::ZString key) {
std::cout << "operator[" << key << "]" << std::endl;
keyx = key;
return *this;
}
// JString & operator=(coreutils:: ZString value) {
// }
JString & operator=(coreutils:: ZString value) {
std::cout << "should set key [" << keyx << "] operator=" << value << std::endl;
set(keyx, value);
return *this;
}
};

90
JString.h.old Normal file
View File

@ -0,0 +1,90 @@
#ifndef __JString_h__
#define __JString_h__
#include "MString.h"
#include <iostream>
#include <sstream>
namespace coreutils {
///
/// Use the JString object when you need a JSON interface to C++. JString uses
/// an MString to store a JSON string representing the object(s) contained.
/// The [] operator is overriden from the ZString and provides a method to
/// access the objects elements using a named path as the index to the items.
///
/// JString default constructor will create an empty JString object pointing
/// to an empty string.
///
/// Use a constructor or assignment operator to initialize a JString to an
/// initial JSON string.
///
/// Several constructors are available including a handle to a std::FILE
/// object to initialize the JSON string. On destruction the file is saved back.
///
/// The [] operator can be used to access elements within the JSON string.
/// Use:
/// jstring["key"] = "value";
/// std::cout << jstring["key"];
///
class JString : public MString {
public:
JString();
JString(const char *data);
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
/// that begins the object's list.
///
ZString find(ZString &path);
private:
bool locate(ZString &path);
bool notfirst;
ZString path;
class Proxy {
public:
Proxy(JString &jstring, ZString key) : jstring(jstring), key(key) {}
operator coreutils::ZString () {
std::cout << "operator " << key << std::endl;
return jstring.find(key);
}
Proxy& operator=(coreutils::ZString value) {
std::cout << "proxy operator=" << value << std::endl;
jstring.set(key, value);
return *this;
}
private:
JString &jstring;
ZString key;
};
public:
Proxy operator[](coreutils::ZString key) {
std::cout << "operator[" << key << "]" << std::endl;
return Proxy(*this, key);
}
JString & operator=(coreutils:: ZString value) {
std::cout << "operator=" << value << std::endl;
return *this;
}
};
}
#endif

View File

@ -1,4 +1,4 @@
#!/bin/bash
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
#g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils
#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
g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils

Binary file not shown.

View File

@ -3,34 +3,6 @@
int main(int argc, char **argv) {
// coreutils::JString test1;
// std::cout << "empty jstring: [" << test1 << "]." << std::endl;
// test1.set("key1", "data1");
// std::cout << "add key1 jstring: [" << test1 << "]." << std::endl;
// test1.set("key2", "data2");
// std::cout << "add key2 jstring: [" << test1 << "]." << std::endl;
// test1.set("key3", "data3");
// std::cout << test1["key1"] << test1["key2"] << test1["key3"] << std::endl;
// std::cout << test1 << std::endl;
// test1.set("key2", "data5");
// 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;
// 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 = "newvalue";
// std::cout << test2 << std::endl;
// std::cout << "--------" << std::endl;
// std::cout << "key2: " << test2["key2"] << std::endl;
coreutils::MString test0("{\"Number:\"0\",\"id\":\"XXXXX\"}");
coreutils::JString test1;
coreutils::MString test2;
@ -39,16 +11,22 @@ int main(int argc, char **argv) {
test1["health"] = "100";
test1["comment"] = "this is a comment";
test1["race"] = "human";
test1["array"] = "[\"test1\",\"test2\",\"test3\"]";
test1["tester"] = "test field";
test1["object1"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}";
std::cout << test1 << std::endl;
test2 = test1["name"];
std::cout << test2 << 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;
std::cout << test1["id"] << std::endl;
std::cout << test1["name"] << std::endl;
std::cout << test1["health"] << std::endl;
std::cout << test1["comment"] << std::endl;
std::cout << test1["race"] << std::endl;
std::cout << test1["array"] << std::endl;
std::cout << test1["tester"] << std::endl;
std::cout << test1["object"] << std::endl;
std::cout << test1["object1.attr2"] << std::endl;
return 0;
}