Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to validate the structure of a json object ? #604

Closed
Oberonc opened this issue Jun 3, 2017 · 2 comments
Closed

Is there a way to validate the structure of a json object ? #604

Oberonc opened this issue Jun 3, 2017 · 2 comments

Comments

@Oberonc
Copy link

Oberonc commented Jun 3, 2017

Suppose I parsed a string from an unreliable source.
How can I validate the result json object ? (make sure it has certain structure / contents. Such as a list of 3 numbers and then a string ?)
Other than using a long boilerplate code with size() and get<>() and is_* ?

@pboettch
Copy link
Contributor

pboettch commented Jun 3, 2017

To validate a json instance or document, the json-schema.org has defined "a vocabulary that allows you to annotate and validate JSON documents" (citation from their site).

To my knowledge, there are two libraries written in C++ on github. One written by me, which supports draft4-version of json-schema.org only and is entirely based on this (nlohmann/json) library for json-parsing.

And there is valijson by Tristan Penman which does support other C++ json-parsers as well.

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2017

@pboettch is right - JSON Schema is the tool to use here. It lets you specify your requirements with a JSON document and avoids manual checks.

@nlohmann nlohmann closed this as completed Jun 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants