Esempio n. 1
0
std::string 
JsonValue::ToJson(std::string &str)
{
    char buf[128];
    JsonTuple *tup;

    switch(GetType()) {
    case JsonType_Bool:
        if (m_boolVal == true) {
            str += "true";
        } else {
            str += "false";
        }
        break;
    case JsonType_Null:
        str += "null";
        break;
    case JsonType_Double:
        sprintf(buf, "%g", m_dblVal);
        str += buf;
        break;
    case JsonType_Number:
        sprintf(buf, "%ld", m_longVal);
        str += buf;
        break;
    case JsonType_String:
        snprintf(buf, sizeof(buf), "%s", m_strVal.c_str());
        str += buf;
        break;
    case JsonType_Object:
        str += "{"; 
        str = DumpChildren(str);
        str += "}";
        break;
    case JsonType_Array:
        str += "["; 
        str = DumpChildren(str);
        str += "]";
        break;
    case JsonType_Tuple:
        tup = static_cast<JsonTuple *>(this);
        str += tup->GetKey();
        str += " : ";
        str = tup->GetKeyValue()->ToJson(str);
        break;
    case JsonType_Root:
        str = DumpChildren(str);
        break;
    } 
    return str;
}
Esempio n. 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;
}