jsonast

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2025 License: MIT Imports: 7 Imported by: 1

README

jsonast

❯ jsonast --help
jsonast - build json AST

Usage:

  jsonast [flags] [FILE]

This command converts json to AST but does not convert the value type of the node.
Node values ​​are always strings.

Flags:

Examples

cat - <<EOS | jsonast | jq
{
  "hello": "world!"
}
EOS
{
  "path": ".",
  "pos": {
    "line": 1,
    "column": 1,
    "offset": 0
  },
  "object": {
    "path": ".",
    "pos": {
      "line": 1,
      "column": 1,
      "offset": 0
    },
    "pairs": [
      {
        "path": ".[\"hello\"]",
        "pos": {
          "line": 2,
          "column": 3,
          "offset": 4
        },
        "key": "hello",
        "value": {
          "path": ".[\"hello\"]",
          "pos": {
            "line": 2,
            "column": 12,
            "offset": 13
          },
          "string": "world!"
        }
      }
    ]
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

type Array struct {
	Pos   lexer.Position
	Items []*Value `parser:"'[' @@ (',' @@)* ']'" json:"items,omitempty"`
}

func (*Array) IsNode

func (*Array) IsNode()

func (*Array) MarshalJSON

func (v *Array) MarshalJSON() ([]byte, error)

type IndexPath

type IndexPath struct {
	IndexInt    int
	IndexString string
}

func NewIndexPath

func NewIndexPath[I StringOrIntLike](index I) *IndexPath

func (IndexPath) AsPath

func (p IndexPath) AsPath() string

type IntLike

type IntLike interface {
	~int
}

type JArray

type JArray struct {
	Path  Path      `json:"path"`
	Pos   *JPos     `json:"pos"`
	Items []*JValue `json:"items,omitempty"`
}

func (*JArray) IsJNode

func (*JArray) IsJNode()

type JNode

type JNode interface {
	IsJNode()
}

JNode is Node with additional information added. The result of json.Marshal is more organized than Node.

type JObject

type JObject struct {
	Path  Path     `json:"path"`
	Pos   *JPos    `json:"pos"`
	Pairs []*JPair `json:"pairs,omitempty"`
}

func (*JObject) IsJNode

func (*JObject) IsJNode()

type JPair

type JPair struct {
	Path  Path    `json:"path"`
	Pos   *JPos   `json:"pos"`
	Key   string  `json:"key,omitempty"`
	Value *JValue `json:"value,omitempty"`
}

func (*JPair) IsJNode

func (*JPair) IsJNode()

type JPos

type JPos struct {
	Line   int `json:"line"`
	Column int `json:"column"`
	Offset int `json:"offset"`
}

type JSON

type JSON struct {
	Value *Value `parser:"@@" json:"value"`
}

func Parse

func Parse(r io.Reader) (*JSON, error)

func (*JSON) MarshalJSON

func (j *JSON) MarshalJSON() ([]byte, error)

type JValue

type JValue struct {
	Path   Path     `json:"path"`
	Pos    *JPos    `json:"pos"`
	Object *JObject `json:"object,omitempty"`
	Array  *JArray  `json:"array,omitempty"`
	String *string  `json:"string,omitempty"`
	Number *string  `json:"number,omitempty"`
	True   *string  `json:"true,omitempty"`
	False  *string  `json:"false,omitempty"`
	Null   *string  `json:"null,omitempty"`
}

func (*JValue) IsJNode

func (*JValue) IsJNode()

type Marshaler

type Marshaler struct{}

func (Marshaler) Build

func (m Marshaler) Build(node Node, initialPath Path) JNode

func (Marshaler) Marshal

func (m Marshaler) Marshal(node Node) ([]byte, error)

type Node

type Node interface {
	IsNode()
}

type Object

type Object struct {
	Pos   lexer.Position
	Pairs []*Pair `parser:"'{' @@ (',' @@)* '}'" json:"pairs,omitempty"`
}

func (*Object) IsNode

func (*Object) IsNode()

func (*Object) MarshalJSON

func (v *Object) MarshalJSON() ([]byte, error)

type Pair

type Pair struct {
	Pos   lexer.Position
	Key   string `parser:"@String ':'" json:"key,omitempty"`
	Value *Value `parser:"@@" json:"value,omitempty"`
}

func (*Pair) IsNode

func (*Pair) IsNode()

func (*Pair) MarshalJSON

func (v *Pair) MarshalJSON() ([]byte, error)

type Path

type Path []PathElement

func NewPath

func NewPath(elems ...PathElement) Path

func (Path) Add

func (p Path) Add(elem PathElement) Path

func (Path) AsPath

func (p Path) AsPath() string

func (Path) Clone

func (p Path) Clone() Path

func (Path) MarshalJSON

func (p Path) MarshalJSON() ([]byte, error)

func (Path) WithoutLast

func (p Path) WithoutLast() Path

type PathElement

type PathElement interface {
	AsPath() string
}

type SimplePath

type SimplePath string

func NewSimplePath

func NewSimplePath[T StringLike](s T) SimplePath

func (SimplePath) AsPath

func (p SimplePath) AsPath() string

type StringLike

type StringLike interface {
	~string
}

type StringOrIntLike

type StringOrIntLike interface {
	StringLike | IntLike
}

type Value

type Value struct {
	Pos    lexer.Position
	Object *Object `parser:"@@ |" json:"object,omitempty"`
	Array  *Array  `parser:"@@ |" json:"array,omitempty"`
	String *string `parser:"@String |" json:"string,omitempty"`
	Number *string `parser:"@Number |" json:"number,omitempty"`
	True   *string `parser:"@True |" json:"true,omitempty"`
	False  *string `parser:"@False |" json:"false,omitempty"`
	Null   *string `parser:"@Null" json:"null,omitempty"`
}

func (*Value) IsNode

func (*Value) IsNode()

func (*Value) MarshalJSON

func (v *Value) MarshalJSON() ([]byte, error)

Directories

Path Synopsis
cmd
jsonast command

Jump to

Keyboard shortcuts

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