Documentation
¶
Index ¶
- Constants
- Variables
- func ATestPath(srcNode *JsonNode, path string, value *JsonNode) error
- func AddPath(node *JsonNode, path string, value *JsonNode) error
- func CopyPath(node *JsonNode, from, path string) error
- func GetJsonNodeError(op, msg string) error
- func KeyReplace(key string) string
- func KeyRestore(key string) string
- func Marshal(node *JsonNode) ([]byte, error)
- func WrapJsonNodeError(op string, err error) error
- type JsonNode
- func MovePath(node *JsonNode, from, path string) (*JsonNode, error)
- func NewObjectNode(key string, childrenMap map[string]*JsonNode, level int) *JsonNode
- func NewSliceNode(children []*JsonNode, level int) *JsonNode
- func NewValueNode(value interface{}, level int) *JsonNode
- func RemovePath(node *JsonNode, path string) (*JsonNode, error)
- func ReplacePath(node *JsonNode, path string, value *JsonNode) (*JsonNode, error)
- func Unmarshal(input []byte) (*JsonNode, error)
- func (jn *JsonNode) ADD(key interface{}, value *JsonNode) error
- func (jn *JsonNode) Append(v *JsonNode) error
- func (jn *JsonNode) Equal(patch *JsonNode) bool
- func (jn *JsonNode) Find(path string) (*JsonNode, bool)
- func (jn *JsonNode) Marshal() ([]byte, error)
- func (jn *JsonNode) Remove(key interface{}) (*JsonNode, error)
- func (jn *JsonNode) Replace(key interface{}, value *JsonNode) (*JsonNode, error)
- func (jn *JsonNode) String() string
- type JsonNodeError
- type JsonNodeType
Constants ¶
const ( // JsonNodeTypeValue : 普通值类型,对应 int, float, string, bool 等, // 该类型不具有子类型,存储在 Value 字段; JsonNodeTypeValue = iota + 1 // JsonNodeTypeSlice : 切片类型,对应 [],该类型是有序的, // 存储在 Children 字段,使用下标唯一表示; JsonNodeTypeSlice // JsonNodeTypeObject : 对象类型,对应 {},该类型是无序的, // 存储在 ChildrenMap, 使用 key 唯一表示。 JsonNodeTypeObject )
const ( STRING jsonTokenType = iota + 1 NUMBER // string NULL // null StartArray // [ EndArray // ] StartObj // { EndObj // } Comma // , Colon // : Boolean // true false EndDoc )
Variables ¶
var BadDiffsError = errors.New("diffs format error")
BadDiffsError 在输入不合法的 diffs 串时被返回
Functions ¶
func GetJsonNodeError ¶
func KeyReplace ¶
KeyReplace 转义 key 中的特殊字符 "/" 会被替换成 "~1" "~1" 会被替换成 "~01" "~01" 会被替换为 "~001" "~001" 会被替换为 "~0001" 依此类推
func KeyRestore ¶
func WrapJsonNodeError ¶
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 NewObjectNode ¶
func NewSliceNode ¶
func NewValueNode ¶
func RemovePath ¶
RemovePath 删除并返回 node 中根据 path 找到的节点。
func ReplacePath ¶
ReplacePath 替换 node 中 path 处的对象为 value, 并返回旧值
func (*JsonNode) ADD ¶
ADD 为当前的 JsonNode 节点添加子对象。 当当前节点为 JsonNodeTypeObject 类型时,key 必须是 string 类型; 当当前节点为 JsonNodeTypeSlice 类型时,key 表示新加入节点的位置下标,必须能转换为 int 类型; 不能对 JsonNodeTypeValue 类型的节点执行 ADD 操作; 不符合上述要求该方法返回一个由 BadDiffsError 装饰的 error
func (*JsonNode) Equal ¶
Equal 比较当前节点和 patch 是否相等 对于两个 JsonNodeTypeObject 类型,不关心顺序,每个 key 对应的 value 都相等才认为相等; 对于两个 JsonNodeTypeSlice 类型,Children 中每个位置对应的元素都相等才认为相等; 对于两个 JsonNodeTypeValue 类型,Value 相等即认为相等。
func (*JsonNode) Find ¶
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) Remove ¶
Remove 删除当前 JsonNode 中 key 对应的节点并返回被删除的值。 Remove 只能删除父节点的某个子节点,节点不能删除它自己,因此, JsonNodeTypeValue 类型的节点不能使用 Remove 方法。
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