Exemplo n.º 1
0
void JsonObject::buildObject(JsonObject &parent, json_object *obj)
{
    enum json_type type;

    json_object_object_foreach(obj, key, val)
    {
        type = json_object_get_type(val);

        switch (type)
        {
            case json_type_object:
            {
                JsonObject child;

                JsonObject::buildObject(child, val);

                parent.push_back(key, child);
            }
            break;

            case json_type_array:
            {
                json_object *jArray = val;

                std::vector<JsonObject>  objArray;
                std::vector<std::string> strArray;
                std::vector<bool>        boolArray;
                std::vector<int>         intArray;

                int arraylen = json_object_array_length(jArray);
                json_object *jValue;

                for (int i = 0; i < arraylen; i++)
                {
                    jValue = json_object_array_get_idx(jArray, i);
                    type = json_object_get_type(jValue);

                    switch (type)
                    {
                        case json_type_string:
                        {
                            std::string value = json_object_get_string(jValue);
                            strArray.push_back(value);
                        }
                        break;

                        case json_type_boolean:
                        {
                            bool value = (bool)json_object_get_boolean(jValue);

                            boolArray.push_back(value);
                        }
                        break;

                        case json_type_int:
                        {
                            int value = json_object_get_int(jValue);

                            intArray.push_back(value);
                        }
                        break;

                        case json_type_object:
                        {
                            JsonObject child;

                            JsonObject::buildObject(child, jValue);

                            objArray.push_back(child);
                        }
                        break;

                        default:break;
                    }
                }

                JsonObject child;

                if (!strArray.empty())
                {
                    child = JsonObject(strArray);
                }
                else if (!boolArray.empty())
                {
                    child = JsonObject(boolArray);
                }
                else if (!intArray.empty())
                {
                    child = JsonObject(intArray);
                }
                else if (!objArray.empty())
                {
                    child = JsonObject(objArray);
                }

                parent.push_back(key, child);
            }
            break;

            case json_type_boolean:
            {
                bool value = (bool)json_object_get_boolean(val);

                parent.push_back(key, value);
            }
            break;

            case json_type_int:
            {
                int value = json_object_get_int(val);

                parent.push_back(key, value);
            }
            break;

            case json_type_string:
            {
                std::string value = json_object_get_string(val);

                parent.push_back(key, value.c_str());
            }
            break;

            default: break;
        };
    }