开发者

JSON schema validation

Is there a stable library that can validate JSON against a schema?

json-schema.org provides a list of implementations. Notably 开发者_JS百科C and C++ are missing.

Is there a reason I can't easily find a C++ JSON schema validator?

Doesn't anyone else want a quick way to validate incoming JSON files?


Is there a stable library that can validate JSON against a schema?

I found a couple hits on google:

  • From the Chromium project: http://aaronboodman-com-v1.blogspot.com/2010/11/c-version-of-json-schema.html
  • http://avro.apache.org/docs/1.4.1/api/cpp/html/index.html

You could also plug a Python or Javascript interpreter into your app, and simply run the native version of those validator implementations that you've already found.

Is there a reason I can't easily find a C++ JSON schema validator?

I believe JSON originated as a web technology, and C/C++ has fallen out of favor for web app implementation.


Valijson is a very good library which depends only on Boost (And I'm actually hoping to change that). It doesn't even depend on any particular JSON parser, providing adapters for most commonly-used libraries like JsonCpp, rapidjson and json11.

The code may seem verbose, but you can always write a helper (example for 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());
  }
}


You can try UniversalContainer (libuc). http://www.greatpanic.com/code.html. You're looking for the container contract/schema checking class in this library. The schema format is clunky, but should handle everything you care about and provide reasonable reporting on why a particular instance fails to meet the schema.


If you can accommodate a polyglot approach, Ajv appears to be a solid JavaScript implementation.

https://ajv.js.org/

Note: There is also an ajv-cli

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


Is there a stable library that can validate JSON against a schema?

Rapidjson

I'm using it for JSON validation against a schema (for the most). It looks like to be tested and stable (v1.1.0 looks like to be latest release, according to the github repo).


On Windows platform you can use JSON Essentials for COM/ActiveX regardless of the language of your choice. If you want to use it from C++ using MSVC compiler you can have compiler generated wrappers using the #import pre-processor directive.

Its documentation contains a number of samples for the most common use cases in C#, VB and JavaScript. The language syntax is the only things that changes, it all can be implemented using the same library calls in pretty much any language that supports COM.

It's also worth to mention that it offers free licensing options.

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


You can have a look to this approach https://epub.jku.at/obvulioa/content/pageview/6390647?query=berardinelli

The in-memory representation of your JSON documents (metaschema, schemas, or schema instances) is provided in EMF (Eclipse Modeling Framework). Once you have this in-memory representation (preserving the usual JSON textual notation, so compliant with any JSON tool you are using now), you can use EMF-based technologies like OCL as well as MDE platforms like Eclipse Epsilon.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜