BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News JSON Validation Roundup

JSON Validation Roundup

This item in japanese

Lire ce contenu en français

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.

JSON Schema

JSON Schema is a proposed standard supported by 17 different projects. It is available for JavaScript, Java, Python, Ruby, Perl, PHP, .NET, ActionScript, C, Haskell, and Erlang. The format is more complicated than VeriJSON with the schema bearing little resemblance to the actual data. It is also more comprehensive, with the ability to range-check numbers, limit lists, and prevent duplicate entries in lists. It also supports references to other schemas, allowing you to break down large schemas into smaller components.

Atdgen

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.

DataContractJsonSerializer

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

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

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.

Rate this Article

Adoption
Style

BT