added a pretty output to JString. Initial.

This commit is contained in:
barant 2024-12-17 16:56:05 -08:00
parent d1e5e41ab2
commit d74fa9689d
4 changed files with 40 additions and 1 deletions

View File

@ -169,5 +169,39 @@ namespace coreutils {
}
reset();
}
MString JString::pretty() {
MString output;
reset();
char ch;
int index = 0;
while(!eod()) {
ch = nextChar();
if(ch == '{') {
index += 3;
std::cout << "{\n";
for(int ix = 0; ix < index; ++ix)
std::cout << ' ';
}
else if(ch == ':') {
std::cout << ": ";
}
else if(ch == '}') {
index -= 3;
std::cout << '\n';
for(int ix = 0; ix < index; ++ix)
std::cout << ' ';
std::cout << ch << '\n';
}
else if(ch == ',') {
std::cout << ",\n";
for(int ix = 0; ix < index; ++ix)
std::cout << ' ';
}
else
std::cout << ch;
}
return output;
}
}

View File

@ -67,7 +67,7 @@ namespace coreutils {
return jstring.find(key).str();
}
Proxy& operator=(coreutils::ZString value) {
Proxy& operator=(coreutils::MString value) {
jstring.set(key, value);
return *this;
}
@ -82,6 +82,8 @@ namespace coreutils {
}
JString & operator=(coreutils:: ZString value);
MString pretty();
private:
void removeWhitespace();

Binary file not shown.

View File

@ -27,6 +27,7 @@ int main(int argc, char **argv) {
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;
@ -58,6 +59,8 @@ int main(int argc, char **argv) {
test9 = test1["object1"];
std::cout << test9 << std::endl;
std::cout << test9["attr3"] << std::endl;
std::cout << test1.pretty() << std::endl;
return 0;
}