Jsoncpp 异常封装

#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;
}*/
  
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值