Skip to content

How to cache a json object's pointer into a map? #1180

Description

@yulinwei9

Hi, I have JSON file

{
    "type":"design",
    "guid":"86a71c67-dd7b-4bdd-9104-fca9ab12f953",
    "children":[
        {
            "type":"folder",
            "guid":"2c2af70d-8b05-4453-a0e1-8c233b0f7de1",
            "children":[
                {
                    "type":"resource",
                    "guid":"b92fda27-53a8-43aa-a240-fce65762462f"
                }
            ]
        },
        {
            "type":"resource",
            "guid":"b18aa915-d268-48b7-83c9-75ee35ab6e37"
        },
        {
            "type":"resource",
            "guid":"98f8626e-964d-4ca1-b3b6-45eff9c600c8"
        }
    ]
}

It is generated by CJSON by the following code

bool BubbleWriter::AppendNode(const UTF8String& parentId)
{
        std::map<UTF8String, cJSON*>::iterator it = m_content.find(parentId);
        if (it == m_content.end())
            return false;

        cJSON* parent = it->second;
        cJSON* pChildren = cJSON_GetObjectItem(parent, "children");
        if (NULL == pChildren)
        {
            pChildren = cJSON_CreateArray();
            cJSON_AddItemToObject(parent, "children", pChildren);
        }

        cJSON* pChild = cJSON_CreateObject();
        cJSON_AddItemToArray(pChildren, pChild);
        ...
 }

m_content is a map to cache the GUID and its corresponding json object, the function AppendNode() could append an object into a json array but the function does not know how deep is the current object in the json file.

I would like to replace CJSON to json.hpp, but I don't know how to cache a json object into a map as value by json.hpp. Any ideas?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions