Skip to content

FileFormatInfo/go-fractured-json

Repository files navigation

go-fractured-json

Go implementation of FracturedJson-style JSON formatting.

Source references used for this port:

  • C# implementation: ../FracturedJson
  • Project wiki: ../FracturedJson.wiki

Current Status

Implemented in this repo:

  • Ordered JSON parser (preserves object property order from input)
  • FracturedJson-style options with v5 default values
  • Inline formatting for arrays/objects based on complexity and line length
  • Expanded formatting with object key alignment
  • Compact multi-line array formatting (multiple items per row)
  • Table formatting for similar array rows (array-of-arrays and array-of-objects)
  • Number list alignment modes (Left, Right, Decimal, Normalize) for table-formatted numeric columns
  • JSON-with-comments input handling (TreatAsError, Remove, Preserve)
  • Trailing-comma policy handling via AllowTrailingCommas
  • Blank-line preservation of leading/trailing blank runs via PreserveBlankLines
  • Convenience APIs for Reformat, Serialize, and Minify

Install / Use

go test ./...

Regenerate golden files in testdata using the C# formatter:

./scripts/regenerate-golden.sh

Optional flags:

# Regenerate one case by name
./scripts/regenerate-golden.sh --only wiki_always_expand_depth_1

# Use a non-default FracturedJson checkout
./scripts/regenerate-golden.sh --fracturedjson ../FracturedJson

Requirements for regeneration:

  • .NET SDK (dotnet) on PATH
  • C# FracturedJson repo at ../FracturedJson or passed via --fracturedjson
package main

import (
	"fmt"

	fj "github.com/FileFormatInfo/go-fractured-json"
)

func main() {
	input := `{"items":[{"id":1,"name":"alpha"},{"id":2,"name":"beta"}],"ok":true}`

	f := fj.NewFormatter()
	f.Options.MaxTotalLineLength = 80

	out, err := f.Reformat(input, 0)
	if err != nil {
		panic(err)
	}
	fmt.Println(out)
}

API

  • type Formatter struct { Options Options }
  • func NewFormatter() *Formatter
  • func (f *Formatter) Reformat(input string, startingDepth int) (string, error)
  • func (f *Formatter) Serialize(obj any, startingDepth int) (string, error)
  • func (f *Formatter) Minify(input string) (string, error)
  • Convenience wrappers:
    • Reformat(input string) (string, error)
    • Serialize(obj any) (string, error)
    • Minify(input string) (string, error)

About

Go port of j-brooke/FracturedJson

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors