Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add to string function to class Object and Array,let them can be convert... #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
117 changes: 117 additions & 0 deletions json_st.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "json_st.hh"
#include <stdexcept>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
using namespace JSON;
Expand Down Expand Up @@ -300,6 +303,65 @@ size_t Object::size() const
return _object.size();
}

std::string Object::to_string()
{
std::string json_str;
static char buf[128] = {'\0'};
json_str = "{";
for (auto e = this->begin(); e != this->end();)
{
json_str += '"';
json_str += e->first;
json_str += '"';
json_str += ":";
switch(e->second.type())
{
/** Base types */
case INT:
std::memset(buf,0,sizeof(buf));
std::snprintf(buf,sizeof(buf),"%lld",(long long int)e->second);
json_str += buf;
break;
case FLOAT:
std::memset(buf,0,sizeof(buf));
std::snprintf(buf,sizeof(buf),"%Lf",(long double)e->second);
json_str += buf;
break;
case BOOL:
if((bool)e->second) {
json_str += "true";
} else {
json_str += "false";
}
break;
case NIL:
break;
case STRING:
json_str += '"';
json_str += (std::string)e->second;
json_str += '"';
break;
/** Compound types */
case ARRAY:
{
Array a = e->second;
json_str += (std::string)(a);
}
break;
case OBJECT:
{
Object o = e->second;
json_str += (std::string)(o);
}
break;
}
if (++e != this->end())
json_str += ",";
}
json_str += "}";
return json_str;
}

Array::Array() { }

Array::~Array() { }
Expand Down Expand Up @@ -361,6 +423,61 @@ void Array::push_back(const Value& v)
_array.push_back(v);
}

std::string Array::to_string()
{
std::string json_str;
static char buf[128] = {'\0'};
json_str += "[";
for (auto e = this->begin(); e != this->end();)
{
switch(e->type())
{
/** Base types */
case INT:
std::memset(buf,0,sizeof(buf));
std::snprintf(buf,sizeof(buf),"%lld",(long long int)(*e));
json_str += buf;
break;
case FLOAT:
std::memset(buf,0,sizeof(buf));
std::snprintf(buf,sizeof(buf),"%Lf",(long double)(*e));
json_str += buf;
break;
case BOOL:
if((bool)(*e)) {
json_str += "true";
} else {
json_str += "false";
}
break;
case NIL:
break;
case STRING:
json_str += '"';
json_str += (std::string)(*e);
json_str += '"';
break;
/** Compound types */
case ARRAY:
{
Array a = *e;
json_str += (std::string)(a);
}
break;
case OBJECT:
{
Object o = *e;
json_str += (std::string)(o);
}
break;
}
if (++e != this->end())
json_str += ",";
}
json_str += "]";
return json_str;
}

void JSON::indent(ostream& os)
{
for (unsigned int i = 0; i < ind; i++)
Expand Down
19 changes: 18 additions & 1 deletion json_st.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define JSON_ST_HH

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <stack>
Expand Down Expand Up @@ -41,7 +42,13 @@ namespace JSON

/** Move constructor. */
Object(Object&& o);


/** Convert Type to string. */
operator std::string()
{
return to_string();
}

/** Assignment operator.
@param o object to copy from
*/
Expand Down Expand Up @@ -94,6 +101,8 @@ namespace JSON

/** Inner container. */
std::map<std::string, Value> _object;
private:
std::string to_string();
};

/** A JSON array, i.e., an indexed container of elements. It contains
Expand Down Expand Up @@ -128,6 +137,12 @@ namespace JSON
*/
Array& operator=(Array&& a);

/** Convert Type to string. */
operator std::string()
{
return to_string();
}

/** Subscript operator, access an element by index.
@param i index of the element to access
*/
Expand Down Expand Up @@ -167,6 +182,8 @@ namespace JSON
/** Inner container. */
std::vector<Value> _array;

private:
std::string to_string();
};

/** A JSON value. Can have either type in ValueTypes. */
Expand Down
9 changes: 7 additions & 2 deletions test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <string>
#include "json.hh"

using namespace std;
Expand All @@ -9,7 +10,7 @@ int main(int argc, char** argv)
// Load JSON file

Value v = parse_file("tests/comp.json");
cerr << v << endl;
cerr << v["type"] << endl;
cerr << "---" << endl;

// Build object programmatically
Expand Down Expand Up @@ -37,6 +38,10 @@ int main(int argc, char** argv)
obj["beer"] = a;

cerr << obj << endl;

std::string s = obj;

cerr << s << endl;

return 0;
}
}