decode

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// JsonNodeTypeValue :  普通值类型,对应 int, float, string, bool 等,
	// 该类型不具有子类型,存储在 Value 字段;
	JsonNodeTypeValue = iota + 1

	// JsonNodeTypeSlice :  切片类型,对应 [],该类型是有序的,
	// 存储在 Children 字段,使用下标唯一表示;
	JsonNodeTypeSlice

	// JsonNodeTypeObject : 对象类型,对应 {},该类型是无序的,
	// 存储在 ChildrenMap, 使用 key 唯一表示。
	JsonNodeTypeObject
)
View Source
const (
	STRING     jsonTokenType = iota + 1
	NUMBER                   // string
	NULL                     // null
	StartArray               // [
	EndArray                 // ]
	StartObj                 // {
	EndObj                   // }
	Comma                    // ,
	Colon                    // :
	Boolean                  // true false
	EndDoc
)

Variables

View Source
var BadDiffsError = errors.New("diffs format error")

BadDiffsError 在输入不合法的 diffs 串时被返回

Functions

func ATestPath

func ATestPath(srcNode *JsonNode, path string, value *JsonNode) error

func AddPath

func AddPath(node *JsonNode, path string, value *JsonNode) error

AddPath 为 node 的 path 路径处的对象添加一个子节点 path 路径表示的是子节点加入后的路径, 以 "/" 开头

func CopyPath

func CopyPath(node *JsonNode, from, path string) error

CopyPath 将 node from 处的节点复制到 path 处

func GetJsonNodeError

func GetJsonNodeError(op, msg string) error

func KeyReplace

func KeyReplace(key string) string

KeyReplace 转义 key 中的特殊字符 "/" 会被替换成 "~1" "~1" 会被替换成 "~01" "~01" 会被替换为 "~001" "~001" 会被替换为 "~0001" 依此类推

func KeyRestore

func KeyRestore(key string) string

func Marshal

func Marshal(node *JsonNode) ([]byte, error)

Marshal 将一个 JsonNode 对象序列化为 Json 字符。

func WrapJsonNodeError

func WrapJsonNodeError(op string, err error) error

Types

type JsonNode

type JsonNode struct {
	Type        JsonNodeType         `json:"type"`
	Hash        string               `json:"hash"`
	Key         string               `json:"key"`
	Value       interface{}          `json:"value"`        // 保存 JsonNodeTypeValue 类型对象的值
	Children    []*JsonNode          `json:"children"`     // 保存 JsonNodeTypeSlice 类型对象的值
	ChildrenMap map[string]*JsonNode `json:"children_map"` // 保存 JsonNodeTypeObject 类型对象的值
	Level       int64                `json:"level"`        // 该 node 所处的层级
	// contains filtered or unexported fields
}

JsonNode 以树的形式组织 Json 中的每一项数据。 根据 Json 的特点,可以将 Json 存储的数据分为三种不同类型: JsonNodeTypeValue,JsonNodeTypeSlice,JsonNodeTypeObject 如:

{
  "a": 1,
  "b": [1],
}

就可以看作两个 JsonNodeTypeObject 类型节点,key 分别是 a 和 b, 其中 a 的值是一个值为 1 的 JsonNodeTypeValue, b 的值是一个长度为 1 的 JsonNodeTypeSlice 类型节点, 而他第 0 个元素也是一个值为 1 的 JsonNodeTypeValue 节点。 最外层的大括号是一个 JsonNodeTypeObject 节点,他作为根节点,Key 为空。

一个 Json 字节数组可以使用 Unmarshal 反序列化为 JsonNode 对象, JsonNode 对象也可以使用 Marshal 序列化为 Json 字节数组

func MovePath

func MovePath(node *JsonNode, from, path string) (*JsonNode, error)

MovePath 将 node 中 from 处的节点移动到 path 处

func NewObjectNode

func NewObjectNode(key string, childrenMap map[string]*JsonNode, level int) *JsonNode

func NewSliceNode

func NewSliceNode(children []*JsonNode, level int) *JsonNode

func NewValueNode

func NewValueNode(value interface{}, level int) *JsonNode

func RemovePath

func RemovePath(node *JsonNode, path string) (*JsonNode, error)

RemovePath 删除并返回 node 中根据 path 找到的节点。

func ReplacePath

func ReplacePath(node *JsonNode, path string, value *JsonNode) (*JsonNode, error)

ReplacePath 替换 node 中 path 处的对象为 value, 并返回旧值

func Unmarshal

func Unmarshal(input []byte) (*JsonNode, error)

Unmarshal 将一个 json 序列格式化为 JsonNode 对象

func (*JsonNode) ADD

func (jn *JsonNode) ADD(key interface{}, value *JsonNode) error

ADD 为当前的 JsonNode 节点添加子对象。 当当前节点为 JsonNodeTypeObject 类型时,key 必须是 string 类型; 当当前节点为 JsonNodeTypeSlice 类型时,key 表示新加入节点的位置下标,必须能转换为 int 类型; 不能对 JsonNodeTypeValue 类型的节点执行 ADD 操作; 不符合上述要求该方法返回一个由 BadDiffsError 装饰的 error

func (*JsonNode) Append

func (jn *JsonNode) Append(v *JsonNode) error

Append 为当前 JsonNodeTypeSlice 节点追加子对象。 只能用于 JsonNodeTypeSlice 类型的节点。

func (*JsonNode) Equal

func (jn *JsonNode) Equal(patch *JsonNode) bool

Equal 比较当前节点和 patch 是否相等 对于两个 JsonNodeTypeObject 类型,不关心顺序,每个 key 对应的 value 都相等才认为相等; 对于两个 JsonNodeTypeSlice 类型,Children 中每个位置对应的元素都相等才认为相等; 对于两个 JsonNodeTypeValue 类型,Value 相等即认为相等。

func (*JsonNode) Find

func (jn *JsonNode) Find(path string) (*JsonNode, bool)

Find 根据路径返回对应的 node 节点 如完整的 json 文件为:

{
 "article_list": [
   {
     "id": 1,
     "article_info": {
       "name": "瓦尔登湖",
       "type": "文学"
     },
     "author_info": {
       "name": "梭罗",
       "country": "US"
     }
   },
 ]
}

使用 `/article_list/0/author_info` 可以得到

{
   "name": "梭罗",
   "country": "US",
}

func (*JsonNode) Marshal

func (jn *JsonNode) Marshal() ([]byte, error)

Marshal 将一个 JsonNode 对象序列化为 Json 字符。

func (*JsonNode) Remove

func (jn *JsonNode) Remove(key interface{}) (*JsonNode, error)

Remove 删除当前 JsonNode 中 key 对应的节点并返回被删除的值。 Remove 只能删除父节点的某个子节点,节点不能删除它自己,因此, JsonNodeTypeValue 类型的节点不能使用 Remove 方法。

func (*JsonNode) Replace

func (jn *JsonNode) Replace(key interface{}, value *JsonNode) (*JsonNode, error)

Replace 使用 value 替换当前节点的 key 的值, 并返回旧值。 当当前节点为 JsonNodeTypeObject 类型时,key 必须是 string 类型; 当当前节点为 JsonNodeTypeSlice 类型时,key 表示新加入节点的位置下标,必须能转换为 int 类型; 不符合上述要求该方法返回一个由 BadDiffsError 装饰的 error

func (*JsonNode) String

func (jn *JsonNode) String() string

type JsonNodeError

type JsonNodeError struct {
	Op string
}

func (*JsonNodeError) Error

func (e *JsonNodeError) Error() string

type JsonNodeType

type JsonNodeType uint8

func (JsonNodeType) String

func (jt JsonNodeType) String() string

Jump to

Keyboard shortcuts

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