flatjson

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 9 Imported by: 0

README

flatjson

PkgGoDev

Overview

flatjson converts JSON files to a "flat" representation with one value per line. For example, given the input:

{
  "menu": {
    "id": "file",
    "value": "File",
    "popup": {
      "menuitem": [
        {
          "value": "New",
          "onclick": "CreateNewDoc()"
        },
        {
          "value": "Open",
          "onclick": "OpenDoc()"
        },
        {
          "value": "Close",
          "onclick": "CloseDoc()"
        }
      ]
    }
  }
}

flatjson outputs:

root = {};
root.menu = {};
root.menu.id = "file";
root.menu.popup = {};
root.menu.popup.menuitem = [];
root.menu.popup.menuitem[0] = {};
root.menu.popup.menuitem[0].onclick = "CreateNewDoc()";
root.menu.popup.menuitem[0].value = "New";
root.menu.popup.menuitem[1] = {};
root.menu.popup.menuitem[1].onclick = "OpenDoc()";
root.menu.popup.menuitem[1].value = "Open";
root.menu.popup.menuitem[2] = {};
root.menu.popup.menuitem[2].onclick = "CloseDoc()";
root.menu.popup.menuitem[2].value = "Close";
root.menu.value = "File";

This format, although verbose, makes it much easier to see the nesting of values. It also happens to be valid JavaScript that can be used to recreate the original JSON object.

This "flat" format is very handy for visualizing diffs. For example, comparing the above JSON object with a second JSON object:

{
  "menu": {
    "id": "file",
    "disabled": true,
    "value": "File menu",
    "popup": {
      "menuitem": [
        {
          "value": "New",
          "onclick": "CreateNewDoc()"
        },
        {
          "value": "Open",
          "onclick": "OpenDoc()"
        }
      ]
    }
  }
}

yields the diff:

--- testdata/a.json
+++ testdata/b.json
@@ -1,5 +1,6 @@
 root = {};
 root.menu = {};
+root.menu.disabled = true;
 root.menu.id = "file";
 root.menu.popup = {};
 root.menu.popup.menuitem = [];
@@ -9,8 +10,5 @@
 root.menu.popup.menuitem[1] = {};
 root.menu.popup.menuitem[1].onclick = "OpenDoc()";
 root.menu.popup.menuitem[1].value = "Open";
-root.menu.popup.menuitem[2] = {};
-root.menu.popup.menuitem[2].onclick = "CloseDoc()";
-root.menu.popup.menuitem[2].value = "Close";
-root.menu.value = "File";
+root.menu.value = "File menu";

Installation

go install github.com/twpayne/flatjson/cmd/flatjson

Generating flat JSON

To convert a file to flat JSON, specify it on the command line, for example:

flatjson vendor/vendor.json

If no filenames are specified, flatjson will read JSON from the standard input.

Generated a unified diff

To generate a unified diff between two JSON files, specify the -diff option and the filenames on the command line, for example:

flatjson -diff ./testdata/a.json ./testdata/b.json

An additional -context option specifies how many lines of context to show. The default is three.

License

MIT

Documentation

Overview

Package flatjson converts JSON structures to a flat list.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}, options ...EncoderOption) ([]byte, error)

Marshal returns the flatjson encoding of v.

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal parses the flatjson-encoded data and stores the result in the value pointed to by v.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

A Decoder decodes flatjson.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new Decoder that reads from r.

func (*Decoder) Decode

func (d *Decoder) Decode(root interface{}) (interface{}, error)

Decode decodes a value, merging it into root.

type Deepener

type Deepener struct{}

A Deepener converts flat JSON into a JSON object.

func NewDeepener

func NewDeepener() *Deepener

NewDeepener returns a new Deepener.

func (*Deepener) MergeValues

func (d *Deepener) MergeValues(root interface{}, r io.Reader) (interface{}, error)

MergeValues merges values read from r into root.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

A Encoder flattens JSON.

func NewEncoder

func NewEncoder(w io.Writer, options ...EncoderOption) *Encoder

NewEncoder returns a new Encoder.

func (*Encoder) Encode

func (e *Encoder) Encode(value interface{}) error

Encode encodes value.

func (*Encoder) Transcode

func (e *Encoder) Transcode(d *json.Decoder) error

Transcode reads JSON tokens from d and writes their flatjson representation.

type EncoderOption

type EncoderOption func(*Encoder)

A EncoderOption modifies a Encoder.

func EncoderPrefix

func EncoderPrefix(prefix string) EncoderOption

EncoderPrefix sets the prefix.

func EncoderSuffix

func EncoderSuffix(suffix string) EncoderOption

EncoderSuffix sets the suffix.

type Flattener

type Flattener struct {
	// contains filtered or unexported fields
}

A Flattener converts JSON into flat JSON.

func NewFlattener

func NewFlattener(w io.Writer, options ...FlattenerOption) *Flattener

NewFlattener returns a new Flattener that writes to w.

func (*Flattener) WriteValues

func (f *Flattener) WriteValues(data []byte) error

WriteValues decodes JSON from data and writes it.

type FlattenerOption

type FlattenerOption func(*Flattener)

A FlattenerOption sets an option on a Flattener.

func FlattenerPrefix

func FlattenerPrefix(prefix string) FlattenerOption

FlattenerPrefix sets the prefix on a Flattener.

func FlattenerSuffix

func FlattenerSuffix(suffix string) FlattenerOption

FlattenerSuffix sets the suffix on a Flattener.

Directories

Path Synopsis
cmd
flatjson command
flatjson flattens JSON files and generates diffs of flattened JSON files.
flatjson flattens JSON files and generates diffs of flattened JSON files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL