示例#1
0
文件: coder.cpp 项目: tuita/DOMQ
int JsonRpcCoder::EncodeResult(const Result& res, Json::Value& obj) const
{
    Json::Value msgObj;
    if(0 != res.JsonEncode(msgObj))
    {
        LOG_ERROR("serialNo:[%d], jsonencode error, res:[%s]", res.serialNo, res.ToString().c_str());
        return -1;
    }
    
    obj["id"] = ToJsonValue(Json::Int(res.context.peer.msgId));
    obj["errno"] = ToJsonValue(Json::Int(res.error));
    if (res.errMsg.size()>0)
    {
        obj["errmsg"] = ToJsonValue(res.errMsg);
    }
    if( msgObj.type() != Json::nullValue)
    {
        obj["result"] = msgObj;
    }
    std::string msgName = res.GetTypeName();
    const std::string& method = GetMethodName(msgName);
    obj["method"] = ToJsonValue(method);
    return 0;
}
示例#2
0
JSONValue *
JSONAPI::ToJsonValue(JsonNode *node)
{
    JsonType type = node->GetType();
    JSONValue *val = (JSONValue *) NULL;

    switch(type) {
    case JsonType_Object:
        {
        JSONObject *jsonobj = new JSONObject();
        std::list<JsonNode *> children = node->GetChildren();
        std::list<JsonNode *>::iterator iter;

        for (iter = children.begin(); iter != children.end(); ++iter) {
            jsonobj->Append(ToJsonValue(*iter));
        }
        val = static_cast<JSONValue *>(jsonobj);
        }
        break;
    case JsonType_Array:
        {
        JSONArray *jsonarray = new JSONArray();
        std::list<JsonNode *> children = node->GetChildren();
        std::list<JsonNode *>::iterator iter;

        for (iter = children.begin(); iter != children.end(); ++iter) {
            jsonarray->Append(ToJsonValue(*iter));
        }
        val = static_cast<JSONValue *>(jsonarray);
        }
        break;
    case JsonType_String:
        {
        std::string str;
        JSONString *jsonstr = new JSONString();
        static_cast<JsonValue *>(node)->GetValue(str);
        jsonstr->Set(str);
        val = static_cast<JSONValue *>(jsonstr);
        }
        break;
    case JsonType_Number:
        {
        long lval;
        JSONNumber *jsonnum = new JSONNumber();
        static_cast<JsonValue *>(node)->GetValue(lval);
        jsonnum->Set(lval);
        val = static_cast<JSONValue *>(jsonnum);
        }
        break;
    case JsonType_Double:
        {
        double dval;
        JSONDouble *jsondbl = new JSONDouble();
        static_cast<JsonValue *>(node)->GetValue(dval);
        jsondbl->Set(dval);
        val = static_cast<JSONValue *>(jsondbl);
        }
        break;
    case JsonType_Bool:
        {
        bool bval;
        JSONBoolean *jsonbool = new JSONBoolean();
        static_cast<JsonValue *>(node)->GetValue(bval);
        jsonbool->Set(bval);
        val = static_cast<JSONValue *>(jsonbool);
        }
        break;
    case JsonType_Tuple:
        {
        JsonTuple *tval = static_cast<JsonTuple *>(node);
        JSONTuple *jsontuple = new JSONTuple();
        std::string key;

        key = tval->GetKey();
        jsontuple->SetKey(key);
        jsontuple->SetValue(ToJsonValue(tval->GetKeyValue()));
        val = static_cast<JSONValue *>(jsontuple);
        }
        break;
    case JsonType_Null:
        {
        JSONNull *jsonnull = new JSONNull();
        val = static_cast<JSONValue *>(jsonnull);
        }
        break;
    default:
        break;
    }

    return val;
}