json文件解析代码 jsoncpp

#include <iostream>
#include <fstream>
#include <json/json.h>

bool isTypeValid(const std::string &type)
{
        return (type == "uint8" || type == "int8" || type == "int32");
}

bool isDataTypesValid(const Json::Value &root)
{
        bool hasUint8 = false;
        bool hasInt8 = false;
        bool hasInt32 = false;
        for (const auto &key : root.getMemberNames())
        {
                const Json::Value &field = root[key];
                std::string type = field["type"].asString();

                if (type == "uint8")
                {
                        hasUint8 = true;
                }
                else if (type == "int8")
                {
                        hasInt8 = true;
                }
                else if (type == "int32")
                {
                        hasInt32 = true;
                }
        }
        if ((hasUint8 && hasInt8) || (hasUint8 && hasInt32) || (hasInt8 && hasInt32))
        {
                return false;
        }
        return true;
}

bool isScaleValid(double scale)
{
        return scale > 0;
}

bool isZeropointValid(int zeropoint, const std::string &type)
{
        if (type == "uint8")
        {
                return (zeropoint >= 0 && zeropoint <= UINT8_MAX);
        }
        else if (type == "int8")
        {
                return (zeropoint >= INT8_MIN && zeropoint <= INT8_MAX);
        }
        else if (type == "int32")
        {
                return (zeropoint >= INT32_MIN && zeropoint <= INT32_MAX);
        }
        return false;
}

int main()
{
        std::ifstream inputFile("config.json");
        if (!inputFile.is_open())
        {
                std::cout << "Unable to open JSON file, please check if the file exists.!" << std::endl;
                return 1;
        }
        // 创建 Json::Value 对象来存储解析后的 JSON 数据
        Json::Value root;
        // 使用 Json::Reader 对象解析 JSON 文件并将其存储在 root 中
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(inputFile, root);
        inputFile.close();
        if (!parsingSuccessful)
        {
                std::cout << "JSON file parsing failed, please check if the JSON file format is correct.!" << std::endl;
                std::cout << reader.getFormattedErrorMessages() << std::endl;
                return 1;
        }

        // 检查并读取字段值
        for (const auto &key : root.getMemberNames())
        {
                std::cout << "Field: " << key << std::endl;
                const Json::Value &field = root[key];

                std::string type;
                double scale;
                int zeropoint;

                std::vector<double> scales;
                std::vector<int> zeropoints;

                // 尝试获取 "type" 字段的值  // 检查 type
                if (field.isMember("type") && field["type"].isString())
                {
                        type = field["type"].asString();
                        if (!isTypeValid(type))
                        {
                                std::cout << "The quantization type parameter is invalid. Only uint8, int8, and int32 are supported.  " << type << std::endl;
                                return 1;
                        }
                }
                else
                {
                        std::cout << "The " type " parameter is missing. Please make sure to include the " type " field in the JSON file and set it to a valid quantization type (such as uint8, int8, or int32). " << std::endl;
                        return 1;
                }

                // 尝试获取 "scale" 字段的值
                if (field.isMember("scale") && field["scale"].isDouble())
                {
                        scale = field["scale"].asDouble();
                        // 检查 zeropoint
                        if (!isScaleValid(scale))
                        {
                                std::cout << "Invalid scale: " << scale << std::endl;
                                return 1;
                        }
                }
                else if (field.isMember("scales") && field["scales"].isArray())
                {
                        for (const auto &element : field["scales"])
                        {
                                if (element.isDouble())
                                {
                                        scales.push_back(element.asDouble());
                                }
                                else
                                {
                                        std::cout << "Invalid scale11: " << scale << std::endl;
                                        return 1;
                                }
                        }
                }
                else
                {
                        std::cout << "error2" << std::endl;
                        return 1;
                }

                // 尝试获取 "scale" 字段的值
                if (field.isMember("zeropoint") && field["zeropoint"].isInt())
                {
                        zeropoint = field["zeropoint"].asInt();
                        // 检查 zeropoint
                        if (!isZeropointValid(zeropoint, type))
                        {
                                std::cout << "Invalid zeropoint: " << zeropoint << std::endl;
                                return 1;
                        }
                }
                else if (field.isMember("zeropoints") && field["zeropoints"].isArray())
                {
                        for (const auto &element : field["zeropoints"])
                        {
                                if (element.asInt())
                                {
                                        zeropoints.push_back(element.asInt());
                                }
                        }

                        if (zeropoints.size() != scales.size())
                        {
                                std::cout << "In per-channel quantization parameters, the number of elements in the 'scales' and 'zeropoints' parameters is different." << std::endl;
                                return 1;
                        }
                }
                else
                {
                        std::cout << "error3" << std::endl;
                        return 1;
                }
        }
        return 0;
}



©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容