Skip to main content

Crate jsonvet

Crate jsonvet 

Source
Expand description

Checks files in the JSON family for syntax errors, duplicate keys and the rules of each dialect: JSON, I-JSON, JSONC, JWCC (HuJSON), JSON5 and JSON Lines.

use jsonvet::{check, Config, Dialect, Rule};

let text = "{\n  // port\n  \"port\": 80,\n  \"port\": 8080,\n}\n";
let report = check(text, &Config::new(Dialect::Json5));
let problem = &report.problems()[0];
assert_eq!(problem.rule(), Rule::DuplicateKey);
assert_eq!(problem.to_string(), "duplicate key $.port");
let at = report.location(problem.offset());
assert_eq!((at.line, at.column), (4, 3));

Printing every problem with its position, as in the README:

use jsonvet::{check, Config, Dialect};

let report = check("{\"a\": 1, \"a\": 2}", &Config::new(Dialect::Json));
for problem in report.problems() {
    let at = report.location(problem.offset());
    println!("{}:{}: {}[{}] {problem}", at.line, at.column, problem.severity(), problem.rule());
}

check never panics and has no error mode: the problems in the text are its output.

Structs§

Config
What to check: a dialect, and any rules whose severity differs from the dialect’s default.
Location
A 1-based line and column. Lines end at LF, CR and CRLF, and in JSON5 also at U+2028 and U+2029, which JSON5 counts as line terminators and the other dialects as ordinary characters; columns count Unicode code points from the start of the line.
Problem
One problem in a file: what is wrong, where, and how much it matters.
Related
A second place a Problem refers to, and what it is.
Report
The problems found in one text, in document order.
UnknownDialect
The error for a dialect name jsonvet does not know.
UnknownRule
The error for a rule id jsonvet does not know.

Enums§

Dialect
A member of the JSON family. In the order JSON, JSONC, JWCC, JSON5, each one adds syntax to the one before it. The one thing that does not carry over is where a // comment ends: JWCC ends it at LF only, JSONC also at a lone CR, and JSON5 also at U+2028 and U+2029. I-JSON is JSON with stricter content rules, and JSON Lines is one JSON value per line.
RelatedKind
What a Related offset points at.
Rule
What jsonvet checks. Every problem belongs to one rule.
Severity
How much a problem matters. An ignored (Off) rule is not checked at all. Severities are ordered from Off to Error.

Functions§

check
Check one text and return every problem in it, in document order. Checking stops at the first syntax error; problems found before it stay in the report.