json

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: GPL-3.0 Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

JSON parsing library

Библиотека для разбора JSON в дерево объектов. Так же позволяет выполнять поисковые запросы над ними.

Использование

import "go.neonxp.dev/json"

jsonString := `{
    "string key": "string value",
    "number key": 123.321,
    "bool key": true,
    "object": {
        "one": "two",
        "object 2": {
            "three": "four"
        }
    },
    "array": [
        "one",
        2,
        true,
        null,
        {
            "five": "six"
        }
    ]
}`

j := json.New(std.Factory) // в качестве фабрики можно передавать имплементацию интерфейса NodeFactory
rootNode, err := j.Unmarshal(jsonString)

// Запрос по получившемуся дереву узлов
found := json.MustQuery(rootNode, []string{ "array", "4", "five" }) // == six

В результате rootNode будет содержать:

std.ObjectNode{
    "string key": &std.StringNode{ "string value" },
    "number key": &std.NumberNode{ 123.321 },
    "bool key":   &std.BoolNode{ true },
    "object": std.ObjectNode{
        "one": &std.StringNode{ "two" },
        "object 2": std.ObjectNode{
            "three": &std.StringNode{ "four" },
        },
    },
    "array": &std.ArrayNode{
        &std.StringNode{ "one" },
        &std.NumberNode{ 2 },
        &std.BoolNode{ true },
        &std.NullNode{},
        std.ObjectNode{
            "five": &std.StringNode{ "six" },
        },
    },
},

Своя фабрика

type Factory interface {
    // Непосредственно фабрика возвращающая заготовки нужного типа
	Produce(typ NodeType) (Node, error)
    // Заполнение заготовки определенного типа соответствующим ей значением
	Fill(n Node, value any)
}

type Node interface {
    // Возвращает json представление узла
	ToJSON() string
}

type ObjectNode interface {
	Node
	Set(k string, v Node)
	Get(k string) (Node, bool)
}

type ArrayNode interface {
	Node
	Append(v Node)
	Index(i int) Node
	Len() int
}

type StringNode interface {
	Node
}

type NumberNode interface {
	Node
}

type BooleanNode interface {
	Node
}

type NullNode interface {
	Node
}

// Если узел имплементирует этот интерфейс то вызывается метод Parent передающий родительский узел
type AcceptParent interface {
	SetParent(n Node)
}

Пример реализации самой простой фабрики и типов

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AcceptParent added in v0.1.0

type AcceptParent interface {
	SetParent(n Node)
}

type ArrayNode added in v0.1.0

type ArrayNode interface {
	Node
	Append(v Node)
	Index(i int) Node
	Len() int
}

type BooleanNode added in v0.1.0

type BooleanNode interface {
	Node
}

type Factory added in v0.1.2

type Factory interface {
	Produce(typ NodeType) (Node, error)
	Fill(n Node, value any)
}

type JSON added in v0.1.0

type JSON struct {
	Factory Factory
}

func New added in v0.1.0

func New(factory Factory) *JSON

func (*JSON) Marshal added in v0.1.0

func (j *JSON) Marshal(n Node) string

func (*JSON) MustNode added in v0.1.0

func (j *JSON) MustNode(value any) Node

func (*JSON) MustUnmarshal added in v0.1.0

func (j *JSON) MustUnmarshal(input string) Node

func (*JSON) Node added in v0.1.0

func (j *JSON) Node(value any) (Node, error)

func (*JSON) Unmarshal added in v0.1.0

func (j *JSON) Unmarshal(input string) (Node, error)

type Node added in v0.1.0

type Node interface {
	ToJSON() string
}

func MustQuery added in v0.1.0

func MustQuery(parent Node, path []string) Node

func Query

func Query(parent Node, path []string) (Node, error)

type NodeType added in v0.1.0

type NodeType string
const (
	StringType  NodeType = "string"
	NumberType  NodeType = "number"
	ObjectType  NodeType = "object"
	ArrayType   NodeType = "array"
	BooleanType NodeType = "boolean"
	NullType    NodeType = "null"
)

type NullNode added in v0.1.0

type NullNode interface {
	Node
}

type NumberNode added in v0.1.0

type NumberNode interface {
	Node
}

type ObjectNode added in v0.1.0

type ObjectNode interface {
	Node
	Set(k string, v Node)
	Get(k string) (Node, bool)
}

type StringNode added in v0.1.0

type StringNode interface {
	Node
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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