#include <json/json.h>
#include <string.h>
using namespace std;
class JsonBuffer{
friend class JsonObject;
Json::Value value;
};
class JsonObject{
public:
static JsonObject parse(const char *szSrc,JsonBuffer& buffer)throw(string){
Json::Reader reader;
if (!reader.parse(szSrc, buffer.value, false)){
throw(string("parse failure"));
}
return buffer.value;
}
JsonObject(const JsonObject &other):m_value(other.m_value),m_key(other.m_key){}
JsonObject(JsonBuffer& buffer):m_value(buffer.value){}
JsonObject &operator=( const Json::Value &value ){
this->m_value = value;
return *this;
}
JsonObject &operator=( const JsonObject &other ){
this->m_value = other.m_value;
return *this;
}
string toString()throw(string){
if(!m_value.isString()){
throw(string(m_key) + " is not string");
}
return m_value.asString();
}
int toInt()throw(string){
if(m_value.isInt()){
return m_value.asInt();
}
if(m_value.isString()){
string strTemp = m_value.asString();
if(strTemp == "0"){
return 0;
}
int iTemp = atoi(strTemp.c_str());
if(iTemp == 0){
throw(string(m_key) + " is string,but atoi fail");
}
return iTemp;
}
throw(string(m_key) + " is not int");
}
double toDouble()throw(string){
if(m_value.isDouble()){
return m_value.asDouble();
}
if(m_value.isString()){
string strTemp = m_value.asString();
if(strTemp == "0.0"){
return 0.0;
}
int iTemp = atof(strTemp.c_str());
if(iTemp == 0.0){
throw(string(m_key) + " is string,but atof fail");
}
return iTemp;
}
throw(string(m_key) + " is not double");
}
bool toBool()throw(string){
if(!m_value.isBool()){
throw(string(m_key) + " is not bool");
}
return m_value.asBool();
}
JsonObject toArray()throw(string){
if(!m_value.isArray()){
throw(string(m_key) + " is not bool");
}
return m_value;
}
bool isNull(){
return m_value.isNull();
}
JsonObject operator[](const char* key)throw(string){
if(!m_value.isObject()){
throw(string(key) + " not is Object");
}
return JsonObject(m_value[key],key);
}
size_t size(){
if(!m_value.isArray()){
throw( m_key + string(" is not array"));
}
m_value.size();
}
JsonObject operator[](int i)throw(string){
if(!m_value.isArray()){
throw( m_key + string(" is not array"));
}
if(i>=m_value.size()){
throw(string("off shore Access , size is ") + std::to_string((int)m_value.size()));
}
return m_value[i];
}
void append(JsonObject&& object){
m_value.append(object.m_value);
}
void append(Json::Value value){
m_value.append(value);
}
string write(){
Json::FastWriter jw;
return jw.write(m_value);
}
private:
JsonObject(Json::Value& value,string key = ""):m_value(value),m_key(key){}
Json::Value& m_value;
string m_key;
};
/*
#include <iostream>
int main(void)
{
try{
JsonBuffer Buffer;
JsonObject root = JsonObject::parse("[{\"name\":[{\"ss\":\"11\"},{\"ss\":22}], \"as\":{\"age\":\"11.0\"}}]",Buffer);
cout << "parse finish" << endl;
string name = root[0]["name"][0]["ss"].toString();
int age = root[0]["as"]["age"].toInt();
cout << "name: " << name << ", age: " << age << endl;
cout << root.write();
root[0]["as"]["age"] = 12;
for(int i = 0;i<root[0]["name"].size();i++){
cout << root[0]["name"][i]["ss"].toInt() << endl;
}
root[0]["name"].append(root[0]["name"][0]);
root[0]["name"].append(11);
cout << root.write();
JsonBuffer Buffer2;
JsonObject root2(Buffer2);
root2["11"] = 11;
cout << root2.write();
}catch(string err){
cout << err << endl;
}
return 0;
}*/
Jsoncpp 异常封装
最新推荐文章于 2024-05-07 20:56:04 发布