JSON Validation Roundup
Create any “flexible” or “extensible” file format and sooner or later a group of developers will start complaining about validation. There are many this can be handles, but in general they can be categorized as:
- Assume the data is correctly formatted.
- Manually check for mal-formed data and attempt to correct.
- Manually check for mal-formed data and reject where appropriate.
- Automatically check for mal-formed data.
The purpose of this article isn’t to debate these options, but rather to summarize the toolkits available for automatic validation.
Atdgen offers JSON serialization and deserialization support to OCaml. JSON schemas are created using what are known as atd files. These files are run through a code generator to create OCaml class files and matching object/JSON converters. In Atdgen, validation occurs during the deserialization process.
Validation rules in atd files are open-ended, developers can inject whatever OCaml code they want directly into the atd file. References to secondary schema files are supported.
Atdgen is available under an open source license.
Based on WCF’s DataContractSerializer, the DataContractJsonSerializer uses class definitions and data contract attributes to determine how a JSON file is validated and ultimately deserialized. This is suitable for basic tasks, but tends to exhibit performance problems and has difficulty with dictionaries. Validation is pretty much limited to basic structure.
Json.NET by James Newton-King is by far the most comprehensive JSON serializer/deserializer available today. Like the DataContractJsonSerializer, Json.NET can use classes to define its schema. You can either use the standard WCF data contract attributes or JSON.NET’s serialization attributes and supports a wide variety of types including DataTable and nHibernate entities. And as of this month, JSON.NET 5.0 supports unlimited size integers (via BigInteger) and read-only collections that expose an IEnumerable constructor.
JSON.NET also supports validation via JSON Schema.
VeriJSON is an Objective-C library that relies on pattern-based matching. The patterns themselves are written in JSON. Supported types are “number”, “string”, “bool”, and “url”. Strings can be restricted using a regular expression and urls by schema (e.g. http, ftp). Arrays and objects are represented structurally and optional properties are supported.
VeriJSON is available via an open source license.
CAMV with JSON validation engine
And then the main project page is here: www.cameditor.org
We have more examples on our VerifyXML.org site and the associated GitHub repository.
To validate json online
Ralph Winzinger Nov 25, 2014