.NET JSON Check, dnjc
, is a command line tool to check for JSON syntax errors using the System.Text.Json
parsers.
PS> Get-Content "good.json" | dnjc
# Nothing wrong: no output
PS> Get-Content "bad.json" | dnjc
6 2 '}' is an invalid start of a value.
The source code is hosted on GitHub.
dnjc
can be installed as a .NET global tool:
If this is the first .NET global tool you've installed, you may need to restart your shell/console for it to pick up the changes to your PATH.
The current versions of the tool and the library that powers it are shown below.
Package | Version |
---|---|
DotNetJsonCheck.Tool | |
DotNetJsonCheck |
After installing, invoke it, giving it the JSON you want to check on standard input. Using PowerShell, this is something like:
Other shells, like Bash, CMD, and Zsh can use redirection:
Any errors will be written to standard out.
With no switches, dnjc
will allow /* */
and //
comments as well as trailing commas. This JSON document will be successfully parsed:
These defaults were chosen because this is how the Microsoft.Extensions.Configuration.Json
library invokes JsonDocument.Parse()
, and those are the JSON files I'm most often editing.
This behavior can be controlled:
--allow-comments
: allows /* */
and //
style comments (defaults to enabled)--allow-trailing-commas
: allows trailing commas in arrays and objects (defaults to enabled)--strict
: parses JSON strictly (no comments, no trailing commas)--help
: prints help and exits--version
: prints version information and exitsAny number of options may be passed. They are processed in order. For example, to allow comments but not trailing commas, pass --strict --allow-comments
in that order.
The Emacs package flycheck-dnjc.el
can be used to configure a Flycheck checker that uses dnjc
.
To install it, download it and then install it with M-x package-install-file
RET /path/to/where/you/downloaded/flycheck-dnjc.el
If you use use-package
to manage your packages, add this to your .emacs
file:
If you aren't using any package managers, make sure that flycheck-dnjc
in on your load-path and add:
Any errors encountered will be written to standard out, one per line. Each line has the format
LEVEL<TAB>LINE<TAB>COLUMN<TAB>MESSAGE
If LINE or COLUMN cannot be determined, they will be empty.
Additional columns may be added in the future at the end. Ensure your parsing can handle this.
The exit code of dnjc
will be
Things that I want to do, in rough priority order
package-upload-file
to create a hostable package archive?Copyright 2020, G. Christopher Warrington
dnjc is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 as published by the Free Software Foundation.
dnjc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
A copy of the GNU Affero General Public License Version 3 is included in the file LICENSE at the root of the repository.