是否有一个稳定的库可以根据模式验证JSON?
json-schema.org提供了一个实现列表。值得注意的是,缺少C和C++。
我找不到一个C ++ JSON模式验证器有什么原因?
没有人想要一种快速验证传入JSON文件的方法吗?
是否有一个稳定的库可以根据模式验证JSON?
json-schema.org提供了一个实现列表。值得注意的是,缺少C和C++。
我找不到一个C ++ JSON模式验证器有什么原因?
没有人想要一种快速验证传入JSON文件的方法吗?
是否有一个稳定的库可以根据架构验证JSON?
我在谷歌上找到了一些结果:
您还可以将Python或Javascript解释器插入应用程序中,并简单地运行您已经找到的那些验证器实现的本机版本。
为什么我无法轻松找到C ++ JSON模式验证器?
我认为JSON起源于Web技术,而C / C ++已经不再适用于Web应用程序实现。
Valijson 是一款非常优秀的库,仅依赖于 Boost(我实际上希望 改变 这一点)。它甚至不依赖于任何特定的 JSON 解析器,提供了适配大多数常用库(如 JsonCpp、rapidjson 和 json11)的适配器。
代码可能看起来有些冗长,但你可以始终编写一个帮助程序(例如 JsonCpp):
#include <json-cpp/json.h>
#include <sstream>
#include <valijson/adapters/jsoncpp_adapter.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
#include <valijson/validation_results.hpp>
#include <valijson/validator.hpp>
void validate_json(Json::Value const& root, std::string const& schema_str)
{
using valijson::Schema;
using valijson::SchemaParser;
using valijson::Validator;
using valijson::ValidationResults;
using valijson::adapters::JsonCppAdapter;
Json::Value schema_js;
{
Json::Reader reader;
std::stringstream schema_stream(schema_str);
if (!reader.parse(schema_stream, schema_js, false))
throw std::runtime_error("Unable to parse the embedded schema: "
+ reader.getFormatedErrorMessages());
}
JsonCppAdapter doc(root);
JsonCppAdapter schema_doc(schema_js);
SchemaParser parser(SchemaParser::kDraft4);
Schema schema;
parser.populateSchema(schema_doc, schema);
Validator validator(schema);
validator.setStrict(false);
ValidationResults results;
if (!validator.validate(doc, &results))
{
std::stringstream err_oss;
err_oss << "Validation failed." << std::endl;
ValidationResults::Error error;
int error_num = 1;
while (results.popError(error))
{
std::string context;
std::vector<std::string>::iterator itr = error.context.begin();
for (; itr != error.context.end(); itr++)
context += *itr;
err_oss << "Error #" << error_num << std::endl
<< " context: " << context << std::endl
<< " desc: " << error.description << std::endl;
++error_num;
}
throw std::runtime_error(err_oss.str());
}
}
注意:还有一个ajv-cli
#import
预处理器指令生成编译器包装器。Here is a sample of creating a schema, loading and validating a JSON document using the newly created schema for Windows Script Host (WSH) using JSON Essentials evaluation version:
// Create a new JsonEssentials class instance.
var jess = new ActiveXObject('JsonEssentials_0.JsonEssentials.1');
jess.unlock('jesscomeval');
// Create schema JSON document object.
var schemaDoc = jess.createDocument();
// Load JSON schema.
schemaDoc.load({
'type': 'array',
'items': {
'type': 'integer'
}
});
// Create schema collection object.
var schemas = jess.createSchemas();
// Add the test schema to the collection.
var schema = schemas.add(schemaDoc, 'uri:test');
// Create JSON document object.
var instanceDoc = jess.createDocument();
// Load JSON, an array of numeric values.
instanceDoc.load([ 0, 1, 2 ]);
// Validate the test JSON against the added schema.
var status = schema.validate(instanceDoc);
WScript.Echo(status.success ? 'validated' : 'validation failed');
你可以查看这个方法https://epub.jku.at/obvulioa/content/pageview/6390647?query=berardinelli
你的JSON文档(元模式、模式或模式实例)的内存表示是使用EMF(Eclipse建模框架)提供的。一旦你有了这个内存表示(保留通常的JSON文本表示法,因此与你现在使用的任何JSON工具兼容),你就可以使用基于EMF的技术,如OCL,以及基于MDE的平台,如Eclipse Epsilon。