31

是否有可以根据模式验证 JSON 的稳定库?

json-schema.org提供了一个实现列表。值得注意的是,缺少 C 和 C++。

是否有我无法轻松找到 C++ JSON 模式验证器的原因?
没有其他人想要一种快速的方法来验证传入的 JSON 文件吗?

4

6 回答 6

18

是否有可以根据模式验证 JSON 的稳定库?

我在谷歌上找到了几个点击:

您还可以将 Python 或 Javascript 解释器插入您的应用程序,并简单地运行您已经找到的那些验证器实现的本机版本。

是否有我无法轻松找到 C++ JSON 模式验证器的原因?

我相信 JSON 起源于一种 Web 技术,而 C/C++ 在 Web 应用程序实现中已经失宠。

于 2011-01-13T03:02:32.223 回答
7

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());
  }
}
于 2016-01-30T11:37:00.327 回答
2

你可以试试 UniversalContainer (libuc)。http://www.greatpanic.com/code.html。您正在此库中寻找容器合同/模式检查类。模式格式很笨拙,但应该处理您关心的所有内容,并提供关于特定实例为何无法满足模式的合理报告。

于 2012-12-31T18:43:54.143 回答
0

在 Windows 平台上,无论您选择哪种语言,您都可以使用JSON Essentials for COM/ActiveX 。如果您想使用 MSVC 编译器从 C++ 使用它,您可以使用#import预处理器指令让编译器生成包装器。

它的文档包含许多C#、VB 和 JavaScript 中最常见用例的示例。语言语法是唯一改变的东西,这一切都可以用几乎任何支持 COM 的语言使用相同的库调用来实现。

还值得一提的是,它提供免费的许可选项。

以下是使用 JSON Essentials评估版 使用新创建的 Windows 脚本主机 (WSH) 架构创建架构、加载和验证 JSON 文档的示例:

// 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');

于 2022-01-25T13:08:45.280 回答
0

如果您可以适应多语言方法,Ajv 似乎是一个可靠的 JavaScript 实现。

https://ajv.js.org/

注意:还有一个 ajv-cli

https://github.com/jessedc/ajv-cli

于 2019-04-21T19:47:22.023 回答
0

是否有可以根据模式验证 JSON 的稳定库?

Rapidjson

我将它用于针对模式的 JSON 验证(大多数情况下)。它看起来已经过测试并且很稳定(根据 github repo,v1.1.0 看起来是最新版本)。

于 2020-03-12T17:11:37.757 回答