#include <iostream>
#include <string>
#include <vector>
#include "json/value.h"
#include "json/json.h"
#include "json/reader.h"
#include "json/writer.h"
struct JsonValidatorDef {
const char *name;
const Json::ValueType type;
const bool required;
};
bool JsonValidator(const Json::Value &value, const std::vector<JsonValidatorDef>&defs) {
for (auto &def : defs) {
if (false == value.isMember(def.name)) {
if (true == def.required) {
std::cerr << def.name << " missing." << std::endl;
}
return (true == def.required) ? false : true;
}
if (value[def.name].type() != def.type) {
std::cerr << "error type = " << value[def.name].type() << " expected type = " << def.type << std::endl;
return false;
}
}
return true;
}
struct check_value_struct {
check_value_struct(const std::string _json_name,
bool _required,
unsigned _value_type,
const std::string _others)
: json_name(_json_name)
, required(_required)
, value_type(_value_type)
, others_check(_others) {
}
check_value_struct(const std::string _json_name,
bool _required,
unsigned _value_type)
: json_name(_json_name)
, required(_required)
, value_type(_value_type) {
}
std::string json_name;
bool required;
unsigned value_type;
std::string others_check;
};
enum {
OPTIONAL = 0,
REQUIRED = 1,
};
enum {
STRING = 1,
INT = 2,
ARRAY = 3,
DATE = 4,
DATETIME = 5
};
bool CheckEmptyValue(const Json::Value &value, const check_value_struct &def) {
return (REQUIRED == def.required) ? false : true;
}
bool CheckStringValue(const Json::Value &value, const check_value_struct &def) {
if (Json::stringValue != value[def.json_name].type()) {
return false;
}
if (0 == def.others_check.compare("NOT_EMPTY")) {
const std::string &str = value[def.json_name].asString();
return (true == str.empty()) ? false : true;
}
return true;
}
bool GetValueFromFormatString(const std::string &str, const std::string &format, int &val) {
auto index = str.find(format);
if (std::string::npos == index) {
return false;
}
std::string sub_str = str.substr(index);
return 1 == sscanf(sub_str.c_str(), format.c_str(), &val);
}
bool CheckIntValue(const Json::Value &value, const check_value_struct &def) {
if (value[def.json_name].type() != Json::intValue) {
return false;
}
int val = 0;
int m = value[def.json_name].asInt();
std::string format = "min%d";
if (true == GetValueFromFormatString(def.others_check, format, val)) {
if (val > m) {
return false;
}
}
format = "max%d";
if (true == GetValueFromFormatString(def.others_check, format, val)) {
if (val < m) {
return false;
}
}
return true;
}
bool JsonValidator(const Json::Value &value, const std::vector<check_value_struct>&defs) {
for (auto &def : defs) {
if (value[def.json_name].isNull()) {
std::cerr << def.json_name << " is null." << std::endl;
if (false == CheckEmptyValue(value, def)) {
return false;
}
continue;
}
switch (def.value_type) {
case STRING:
if (false == CheckStringValue(value, def)) {
return false;
}
break;
case INT:
if (false == CheckIntValue(value, def)) {
return false;
}
break;
}
}
return true;
}
int main() {
Json::Value root;
Json::Reader reader;
std::string s = "{\"uploadid\": \"UP000000\"}";
if (!reader.parse(s, root)) {
std::cerr << "parse fail" << std::endl;
return -1;
}
std::cout << root["uploadid"].asString() << std::endl;
std::cout << root.isMember("uploadid") << std::endl;
std::vector<JsonValidatorDef>defs{{"11", Json::intValue, true}};
std::cout << JsonValidator(root, defs) << std::endl;
return 0;
}