JString now streams properly.

This commit is contained in:
barant 2024-12-11 09:55:40 -08:00
parent 0baa44d076
commit 7531c885a7
5 changed files with 57 additions and 122 deletions

View File

@ -3,9 +3,14 @@
namespace coreutils { namespace coreutils {
JString::JString() : MString("{}") {} std::ostream &operator<<(std::ostream &os, const JString::Proxy &proxy) {
ZString tempkey = proxy.key;
ZString temp = proxy.jstring.find(tempkey);
os << temp;
return os;
}
// JString::JString(const char *data) : MString(data) {} JString::JString() : MString("{}") {}
void JString::set(ZString &path, ZString &value) { void JString::set(ZString &path, ZString &value) {
reset(); reset();
@ -49,9 +54,11 @@ namespace coreutils {
return getTokenExclude("\""); return getTokenExclude("\"");
// TODO: Handle numbers that are not in double quotes. // TODO: Handle numbers that are not in double quotes.
if(startsWith("[")) { else if(startsWith("["))
std::cout << getContainer() << std::endl; return getContainer();
}
else if(startsWith("{"))
return getContainer();
} }
return ZString(); return ZString();
} }
@ -71,8 +78,7 @@ namespace coreutils {
throw coreutils::Exception("Labels must be contained in double quotes."); throw coreutils::Exception("Labels must be contained in double quotes.");
if(ifNext(":")) { if(ifNext(":")) {
if(label == path[0]) { if(label == path[0]) {
if(path.getList().size() == 1) if(path.getList().size() == 1) {
{
// std::cout << "found cursor: " << unparsed() << std::endl; // std::cout << "found cursor: " << unparsed() << std::endl;
return true; return true;
} }

View File

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

View File

@ -1,90 +0,0 @@
#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

Binary file not shown.

View File

@ -5,8 +5,9 @@ int main(int argc, char **argv) {
coreutils::MString test0("{\"Number:\"0\",\"id\":\"XXXXX\"}"); coreutils::MString test0("{\"Number:\"0\",\"id\":\"XXXXX\"}");
coreutils::JString test1; coreutils::JString test1;
coreutils::MString test2;
test1 = test0; test1 = test0;
test1["name"] = "Cohen"; test1["name"] = "Cohen";
test1["health"] = "100"; test1["health"] = "100";
test1["comment"] = "this is a comment"; test1["comment"] = "this is a comment";
@ -15,17 +16,18 @@ int main(int argc, char **argv) {
test1["tester"] = "test field"; test1["tester"] = "test field";
test1["object1"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}"; test1["object1"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}";
std::cout << test1 << std::endl; std::cout << test1 << std::endl;
std::cout << test1["id"] << std::endl; coreutils::MString test2 = test1["name"];
std::cout << test2 << std::endl;
std::cout << test1["name"] << std::endl; std::cout << test1["name"] << std::endl;
std::cout << test1["health"] << std::endl; std::cout << test1["health"] << std::endl;
std::cout << test1["comment"] << std::endl; std::cout << test1["comment"] << std::endl;
std::cout << test1["race"] << std::endl; std::cout << test1["race"] << std::endl;
std::cout << test1["array"] << std::endl; std::cout << test1["array"] << std::endl;
std::cout << test1["tester"] << std::endl; std::cout << test1["tester"] << std::endl;
std::cout << test1["object"] << std::endl; std::cout << test1["object1"] << std::endl;
std::cout << test1["object1.attr2"] << std::endl; std::cout << test1["object1.attr2"] << std::endl;
return 0; return 0;