#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;
}
json文件解析代码 jsoncpp
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- JSON 简介 JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的...
- 中文乱码,之所以出现这种现象,根本原因是解析和编码所按照的字符集不 同,而字符集是什么呢? ``` 字符集(Cha...
- // StringRequest request = new StringRequest(url, ...
- json文件的地址url="http://www.lagou.com/lbs/getAllCitySearchLa...
- 本文,我们编写JAVA程序来解析class文件,读者注意,阅读本文前先详细了解Class文件结构,可参考笔者前一篇...