Skip to content

mitranim/jsonfmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

Flexible JSON formatter. Features:

  • Preserves order.
  • Supports comments (configurable).
  • Supports trailing commas (configurable).
  • Supports max width (configurable).
    • For dicts and lists: single-line until given width, multi-line after exceeding said width.
  • Fixes missing or broken punctuation.
  • Tiny Go library.
  • Optional tiny CLI.
  • No dependencies.

See API documentation at https://godoc.org/github.com/mitranim/jsonfmt.

Current limitations:

  • Always permissive. Unrecognized non-whitespace is treated as arbitrary content on par with strings, numbers, etc.
  • Slower than json.Indent from the Go standard library.
  • Input must be UTF-8.
  • Input and output are []byte, without streaming.
    • Streaming support could be added on demand.

Installation

Library

To use this as a library, simply import it:

import "github.com/mitranim/jsonfmt"

var formatted string = jsonfmt.Format[string](jsonfmt.Default, `{}`)
var formatted string = jsonfmt.FormatString(jsonfmt.Default, `{}`)
var formatted []byte = jsonfmt.FormatBytes(jsonfmt.Default, `{}`)

CLI

First, install Go: https://golang.org. Then run this:

go install github.com/mitranim/jsonfmt/jsonfmt@latest

This will compile the executable into $GOPATH/bin/jsonfmt. Make sure $GOPATH/bin is in your $PATH so the shell can discover the jsonfmt command. For example, my ~/.profile contains this:

export GOPATH=~/go
export PATH=$PATH:$GOPATH/bin

Alternatively, you can run the executable using the full path. At the time of writing, ~/go is the default $GOPATH for Go installations. Some systems may have a different one.

~/go/bin/jsonfmt

Usage

See the library documentation on https://godoc.org/github.com/mitranim/jsonfmt.

For CLI usage, run jsonfmt -h.

Examples

Supports comments and trailing commas (all configurable):

{// Line comment
"one": "two", /* Block comment */ "three": 40}

Output:

{
  // Line comment
  "one": "two",
  /* Block comment */
  "three": 40,
}

Single-line until width limit (configurable):

{
  "one": {"two": ["three"], "four": ["five"]},
  "six": {"seven": ["eight"], "nine": ["ten"], "eleven": ["twelve"], "thirteen": ["fourteen"]}
}

Output:

{
  "one": {"two": ["three"], "four": ["five"]},
  "six": {
    "seven": ["eight"],
    "nine": ["ten"],
    "eleven": ["twelve"],
    "thirteen": ["fourteen"],
  },
}

Fix missing or broken punctuation:

{"one" "two" "three" {"four" "five"} "six" ["seven": "eight"]},,,

Output:

{"one": "two", "three": {"four": "five"}, "six": ["seven", "eight"]}

License

https://unlicense.org

Misc

I'm receptive to suggestions. If this library almost satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts

About

Flexible JSON formatter. Supports comments, multi-line with single-line chunks within width limit, fixes punctuation. Library and optional CLI.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages