代码之家  ›  专栏  ›  技术社区  ›  Marek R

使用jsoncpp时从JSon中剥离私有数据的最佳方法

  •  1
  • Marek R  · 技术社区  · 8 年前

    JSon 数据与服务器交换。

    • 服务器发送代码中省略的新值
    • 发送了带有拼写错误的JSon
    • ans等等

    但同时,任何私有数据都应该被虚拟数据遮蔽。

    因此,请参阅日志:

    {
        "secuityToken" : "asdasdgas234fsdfsaD",
        "message" : "user private message"
    }
    

    {
        "secuityToken" : "********",
        "message" : "*******"
    }
    

    我的代码是C++,所以 jsoncpp 我能看到的最好的arproach是:

    bool ProcessServerMessage(const std::string& message)
    {
        Json::Value jsonValue;
        Json::Reader reader;
        if (reader.parse(sMessage, jsonValue, false))
        {
            auto logValue =  ShadowPrivateData(jsonValue, listOfKeysWithPrivateData);
            LOG() << " JSOn recived: " << logValue;
            …
        }
    

    质疑如何 ShadowPrivateData

    2 回复  |  直到 8 年前
        1
  •  3
  •   Sergey    8 年前

    对我来说,一个简单的方法就足够了。只要打电话 ShadowPrivateData jsonValue .在每个递归步骤上,您应该确定 jsonValue公司 是数组、对象或两者都不是,并正确地遍历它。使用 isArray isObject 为此。

    listOfKeysWithPrivateData 。如果在列表中找到字段名称,请确定字段的类型(使用 isString isDouble , isIntegral 以此类推)并用适当的值替换字段:用星号替换字符串,用零替换数字,等等。

    声明 std::set<std::string> 或者类似的方法来执行对数搜索而不是线性搜索。

    如何遍历聚合对象?使用 getMemberNames 对于对象和 size 对于阵列。换句话说,jsoncpp为json对象内省提供了完整的方法集合。

    如果正确实施,这种方法应该审查 无视它的复杂性。

        2
  •  0
  •   Marek R    8 年前

    // in header
    class CJSONUtils
    {
    public:
        static Json::Value HidePrivateData(const Json::Value& value,
                                           const std::vector<std::string>& pathSufixes);
    
    private:
        static void ShadowPrivateData(Json::Value& value,
                                      const std::vector<std::string>& pathSufixes,
                                      const std::string& sPath);
    
        static bool IsPrivatePath(const std::string& sPath,
                                  const std::vector<std::string>& pathSufixes);
    
        static std::string MaskPrivateText(const std::string& sPrivateText);
    };
    
    // in cpp
    #include "JSONUtils.h"
    #include "StringUtils.h"
    #include <algorithm>
    #include <json/json.h>
    
    using namespace std;
    using namespace Json;
    
    Value CJSONUtils::HidePrivateData(const Value& value,
                                      const vector<string>& pathSufixes)
    {
        Value result { value };
        ShadowPrivateData(result, pathSufixes, {});
        return result;
    }
    
    void CJSONUtils::ShadowPrivateData(Value& value,
                                       const vector<string>& pathSufixes,
                                       const string& sPath)
    {
        switch (value.type())
        {
            case nullValue:
            case intValue:
            case uintValue:
            case realValue:
            case booleanValue:
                break;
    
            case stringValue:
                if (IsPrivatePath(sPath, pathSufixes))
                {
                    value = Value { MaskPrivateText(value.asString()) };
                }
                break;
    
            case arrayValue:
                for (auto& arrayValue : value)
                {
                    ShadowPrivateData(arrayValue, pathSufixes, sPath + "[]");
                }
                break;
    
            case objectValue:
                for (auto it = value.begin(); it != value.end(); ++it)
                {
                    ShadowPrivateData(*it, pathSufixes, sPath + "." + it.key().asString());
                }
                break;
        }
    }
    
    bool CJSONUtils::IsPrivatePath(const string& sPath,
                                   const vector<string>& pathSufixes)
    {
        return std::any_of(pathSufixes.begin(),
                           pathSufixes.end(),
                           [&](const string& sSufix)
                           {
                               return EndsWith(sPath, sSufix);
                           });
    }
    
    std::string CJSONUtils::MaskPrivateText(const std::string& sPrivateText)
    {
        if (sPrivateText.length() < 15)
        {
            return std::string(sPrivateText.length(), '*');
        }
        ostringstream result;
        result << "< *" << sPrivateText.length() << " characters * >";
        return result.str();
    }
    

    从现在起 Json::Value

    LOG() << " JSON received: " << CJSONUtils::HidePrivateData(jsonRoot, listOfPrivateItemsPaths);
    

    我已经为它编写了测试(gtest),它工作起来很有魅力。

    推荐文章