New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

zgo.at/jfmt

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zgo.at/jfmt

  • v0.0.0-20240726113937-e6436421fade
  • Go
  • Socket score

Version published
Created
Source

jfmt is my JSON formatter. There's a few things this does different than most JSON formatters I could find, which in my opinion makes the output much nicer.

Install with go install zgo.at/jfmt/cmd/jfmt@latest.

Or import zgo.at/jfmt to use in Go code.


It tries to be a little smarter about what should be on a single line. Things like this:

{
    "nums": [
        12,
        4,
        901,
        12,
        742,
    ],
    "strings": [
        "k",
        "p",
        "bas",
        "pqz",
        "q21",
    ],
    "obj" {
        "k": "v"
    }
}

Are just silly, and splitting it out over many lines like that isn't helpful. So that becomes:

{
    "nums":    [12, 4, 901, 12, 742],
    "strings": ["k", "p", "bas", "pqz", "q21"],
    "obj"      {"k": "v"}
}

It will still write to multiple lines if it's too long.

It will also align the keys; which I rather like.

Like many JSON formatting tools keys are sorted alphabetically, but it puts values that fit on a single line first, to avoid avoid things like this:

{
  "additional": false,
  "definitions": {
      [.. very long object ..]
   }
  "title": "JSON schema for Python project metadata and configuration",
  "type": "object",
}

I don't like these kind of "dangling primitives", so that that becomes:

{
  "additional": false,
  "title":      "JSON schema for Python project metadata and configuration",
  "type":       "object",
  "definitions": {
      [.. very long object ..]
   }
}

And things like this:

{
    "arr5": [
        [
            [
                [
                    [
                        {"type": "string", "value": "#"}
                    ]
                ]
            ]
        ]
    ]
}

Can just be written on a single line, too:

{
    "arr5": [[[[[{"type": "string", "value": "#"}]]]]],
}

There's a bunch of cases like this; in general it tries to be as concise as possible as long as it doesn't sacrifice readability.


The downside of this is that it's a bit slower and uses more memory. It's still plenty fast enough for almost all use cases; the largest file in schemastore.org (kubernetes-definitions.json, 17,500 lines/950K) takes about 120ms on my laptop (vs. 30ms with Go's default MarshalIndent). I spent almost zero effort optimising anything, so there's probably a lot to win here.

FAQs

Package last updated on 26 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc