Skip to content

I want to achieve an upgraded function. #1566

Description

@3038922

I want to achieve an upgraded function.
According to the target key, add and delete obj. without changing value.
It's OK to add obj in this way. But once erase, it's wrong.
if i ues json::diff(), if the target value is different, he will update the value. I don't want him to update the value.

image

void SysBase::upDateJson(json &source, const json &target)
{
    if (source.type() == target.type())
        switch (source.type())
        {
            case json::value_t::object:
            {
                //第一遍:遍历机器人文件的的元素
                for (auto it = source.begin(); it != source.end(); ++it)
                {
                    if (target.find(it.key()) != target.end())
                    {
                        //递归调用以比较对象的对象值
                        upDateJson(it.value(), target[it.key()]);
                    }
                    else
                    {
                        //找到一个不在o中的键 ->删除它
                        source.erase(it);
                        logger->debug({"删除: ", it.key()});
                    }
                }
                //第二遍:遍历用户数据里的元素
                for (auto it = target.begin(); it != target.cend(); ++it)
                {
                    if (source.find(it.key()) == source.end()) //如果机器人文件没找到和目标文件里同名的元素
                    {
                        //添加他
                        source[it.key()] = it.value();
                        logger->debug({"添加: ", it.key()});
                    }
                }
                break;
            }
            default:
                break;
        }
    return;
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions