JString now streams properly.
This commit is contained in:
parent
0baa44d076
commit
7531c885a7
26
JString.cpp
26
JString.cpp
@ -3,10 +3,15 @@
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
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() : MString("{}") {}
|
||||
|
||||
// JString::JString(const char *data) : MString(data) {}
|
||||
|
||||
void JString::set(ZString &path, ZString &value) {
|
||||
reset();
|
||||
if(locate(path)) {
|
||||
@ -46,12 +51,14 @@ namespace coreutils {
|
||||
reset();
|
||||
if(locate(path)) {
|
||||
if(ifNext("\""))
|
||||
return getTokenExclude("\"");
|
||||
return getTokenExclude("\"");
|
||||
// TODO: Handle numbers that are not in double quotes.
|
||||
|
||||
if(startsWith("[")) {
|
||||
std::cout << getContainer() << std::endl;
|
||||
}
|
||||
else if(startsWith("["))
|
||||
return getContainer();
|
||||
|
||||
else if(startsWith("{"))
|
||||
return getContainer();
|
||||
}
|
||||
return ZString();
|
||||
}
|
||||
@ -71,11 +78,10 @@ namespace coreutils {
|
||||
throw coreutils::Exception("Labels must be contained in double quotes.");
|
||||
if(ifNext(":")) {
|
||||
if(label == path[0]) {
|
||||
if(path.getList().size() == 1)
|
||||
{
|
||||
if(path.getList().size() == 1) {
|
||||
// std::cout << "found cursor: " << unparsed() << std::endl;
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return locate(path[1]);
|
||||
}
|
||||
// std::cout << "+++" << unparsed() << std::endl;
|
||||
|
53
JString.h
53
JString.h
@ -33,8 +33,7 @@ namespace coreutils {
|
||||
public:
|
||||
|
||||
JString();
|
||||
// JString(const char *data);
|
||||
|
||||
|
||||
void set(ZString &path, ZString &value);
|
||||
|
||||
///
|
||||
@ -45,28 +44,46 @@ namespace coreutils {
|
||||
|
||||
ZString find(ZString &path);
|
||||
|
||||
private:
|
||||
bool locate(ZString &path);
|
||||
bool notfirst;
|
||||
ZString path;
|
||||
ZString keyx;
|
||||
|
||||
public:
|
||||
|
||||
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;
|
||||
class Proxy {
|
||||
|
||||
public:
|
||||
Proxy(JString &jstring, ZString key) : jstring(jstring), key(key) {}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Proxy &proxy);
|
||||
|
||||
operator coreutils::ZString () {
|
||||
// std::cout << "operator to ZString: " << key << std::endl;
|
||||
return jstring.find(key);
|
||||
}
|
||||
|
||||
operator coreutils::MString () {
|
||||
// std::cout << "operator to MString: " << 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;
|
||||
};
|
||||
|
||||
Proxy operator[](coreutils::ZString key) {
|
||||
// std::cout << "operator[" << key << "]" << std::endl;
|
||||
return Proxy(*this, key);
|
||||
}
|
||||
|
||||
|
||||
JString & operator=(coreutils:: ZString value) {
|
||||
std::cout << "should set key [" << keyx << "] operator=" << value << std::endl;
|
||||
set(keyx, value);
|
||||
std::cout << "operator=" << value << std::endl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -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.
@ -5,8 +5,9 @@ int main(int argc, char **argv) {
|
||||
|
||||
coreutils::MString test0("{\"Number:\"0\",\"id\":\"XXXXX\"}");
|
||||
coreutils::JString test1;
|
||||
coreutils::MString test2;
|
||||
|
||||
test1 = test0;
|
||||
|
||||
test1["name"] = "Cohen";
|
||||
test1["health"] = "100";
|
||||
test1["comment"] = "this is a comment";
|
||||
@ -15,17 +16,18 @@ int main(int argc, char **argv) {
|
||||
test1["tester"] = "test field";
|
||||
test1["object1"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}";
|
||||
|
||||
|
||||
std::cout << test1 << std::endl;
|
||||
|
||||
coreutils::MString test2 = test1["name"];
|
||||
std::cout << test2 << 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"] << std::endl;
|
||||
std::cout << test1["object1.attr2"] << std::endl;
|
||||
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user