Skip to content

Commit

Permalink
format json by jq
Browse files Browse the repository at this point in the history
  • Loading branch information
LintaoAmons committed Jun 26, 2024
1 parent 546de25 commit 4eb5e8c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lua/bookmarks/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
local write_json_file = function(tbl, path)
local content = vim.fn.json_encode(tbl) -- Encoding table to JSON string

local formatter = "jq"
if not (0 == vim.fn.executable(formatter)) then
local fmt_cmd = { formatter, "--sort-keys", "--monochrome-output" }
local result = vim.system(fmt_cmd, { stdin = content }):wait()
if result.code ~= 0 then
error("Failed to format JSON with " .. formatter .. ": " .. result.stderr)
return nil
end
content = result.stdout
end

local file, err = io.open(path, "w")
if not file then
error("Could not open file: " .. err)
Expand Down

0 comments on commit 4eb5e8c

Please sign in to comment.