Go implementation of FracturedJson-style JSON formatting.
Source references used for this port:
- C# implementation:
../FracturedJson - Project wiki:
../FracturedJson.wiki
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, andMinify
go test ./...Regenerate golden files in testdata using the C# formatter:
./scripts/regenerate-golden.shOptional 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 ../FracturedJsonRequirements for regeneration:
- .NET SDK (
dotnet) on PATH - C# FracturedJson repo at
../FracturedJsonor 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)
}type Formatter struct { Options Options }func NewFormatter() *Formatterfunc (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)