Skip to content

phinze/jsonfmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonfmt

accept loosely formatted json, output strictly formatted json

Install / Update

go get -f -u -v github.com/phinze/jsonfmt

Usage

jsonfmt <json_file>

Reformats the file in place.

Using with VIM

In your .vimrc:

function! JsonFormat()
  " save cursor position and many other things
  let l:curw=winsaveview()

  " Write current unsaved buffer to a temp file
  let l:tmpname = tempname()
  call writefile(getline(1, '$'), l:tmpname)

  " populate the final command with user based fmt options
  let command = "jsonfmt"

  " execute our command...
  let out = system(command . " " . l:tmpname)

  if v:shell_error == 0
    " remove undo point caused via BufWritePre
    try | silent undojoin | catch | endtry

    " Replace current file with temp file, then reload buffer
    call rename(l:tmpname, expand('%'))
    silent edit!
    let &syntax = &syntax
  endif

  " restore our cursor/windows positions
  call winrestview(l:curw)
endfunction

autocmd BufWritePre *.json call JsonFormat()

Acknowledgements

  • Uses @daviddengcn's ljson for the loose input parsing a.k.a. the actually difficult part ;D
  • Vim code stolen from @fatih's delightful vim-go project.

About

accept loosely formatted json, output strictly formatted json

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published