jsongen

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 4 Imported by: 1 module, 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.

Documentation

Overview

Package jsongen forked from darjun/json-gen

Package jsongen Copyright 2024 Joshua J Baker. All rights reserved. Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.

https://github.com/tidwall/gjson

Index

Constants

This section is empty.

Variables

View Source
var DisableEscapeHTML = false

DisableEscapeHTML 禁用 JSON 编码时对 HTML 敏感字符的自动转义. 默认将 '>', '<' 和 '&' 分别转义为 \u003e, \u003c 和 \u0026. 这是 jsongen 的全局编码开关, 应在其他 jsongen 函数调用前一次性设置.

Functions

func AppendJSONString

func AppendJSONString(dst []byte, s string) []byte

AppendJSONString 将 s 编码为带双引号的合法 JSON 字符串并追加到 dst.

func EscapeString

func EscapeString(s string) string

EscapeString 转义 JSON 字符串, 并在前后加上双引号.

Types

type Array

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

Array 数组类的 JSON 数据

func NewArray

func NewArray() *Array

NewArray 创建 JSON 数组, 无添加值时, 结果中至少附加一个空数组: []

func (*Array) AppendArray

func (a *Array) AppendArray(vv ...*Array)

AppendArray 追加单个或多个 array 到数组: [1,2] => [1,2,[{"A":1}],[true]]

func (*Array) AppendArrayArray

func (a *Array) AppendArrayArray(vv []*Array)

AppendArrayArray 追加 array 数组: [1,2] => [1,2,[[3],[4],[{"b":true}]]]

func (*Array) AppendBool

func (a *Array) AppendBool(vv ...bool)

AppendBool 追加单个或多个 bool 到数组: [1,2] => [1,2,true,false]

func (*Array) AppendBoolArray

func (a *Array) AppendBoolArray(vv []bool)

AppendBoolArray 追加 bool 数组: [1,2] => [1,2,[true,false]]

func (*Array) AppendFloat

func (a *Array) AppendFloat(vv ...float64)

AppendFloat 追加单个或多个 float64 到数组: [1,2] => [1,2,3.1,4]

func (*Array) AppendFloatArray

func (a *Array) AppendFloatArray(vv []float64)

AppendFloatArray 追加 float64 数组: [1,2] => [1,2,[3,4.1,5]]

func (*Array) AppendInt

func (a *Array) AppendInt(vv ...int64)

AppendInt 追加单个或多个 int64 到数组: [1,2] => [1,2,3,4]

func (*Array) AppendIntArray

func (a *Array) AppendIntArray(vv []int64)

AppendIntArray 追加 int64 数组: [1,2] => [1,2,[3,4,5]]

func (*Array) AppendMap

func (a *Array) AppendMap(vv ...*Map)

AppendMap 追加单个或多个 map 到数组: [1,2] => [1,2,{"A":1},{"b":true}]

func (*Array) AppendMapArray

func (a *Array) AppendMapArray(vv []*Map)

AppendMapArray 追加 map 数组: [1,2] => [1,2,[{"A":1},{"b":true}]]

func (*Array) AppendRawBytes

func (a *Array) AppendRawBytes(bb ...[]byte)

AppendRawBytes 追加单个或多个原生 JSON 数据

func (*Array) AppendRawBytesArray

func (a *Array) AppendRawBytesArray(vv [][]byte)

AppendRawBytesArray 追加原生 JSON 数据数组

func (*Array) AppendRawString

func (a *Array) AppendRawString(ss ...string)

AppendRawString 追加单个或多个原生 JSON 字符串, 如: [1,2] => [1,2,[2,{"A":1}]] a.AppendRawString(`[2,{"A":1}]`)

func (*Array) AppendRawStringArray

func (a *Array) AppendRawStringArray(ss []string)

AppendRawStringArray 追加原生 JSON 字符串数组: [1,2] => [1,2,["x",[4],[{"b":true}]]] a.AppendRawStringArray([]string{`"x"`, `[4]`, `[{"b":true}]`})

func (*Array) AppendString

func (a *Array) AppendString(vv ...string)

AppendString 追加单个或多个 string 到数组: [1,2] => [1,2,"A","b"] a.AppendString("A", "b")

func (*Array) AppendStringArray

func (a *Array) AppendStringArray(vv []string)

AppendStringArray 追加 string 数组: [1,2] => [1,2,["A","b"]] a.AppendStringArray([]string{"A","b"})

func (*Array) AppendUint

func (a *Array) AppendUint(vv ...uint64)

AppendUint 追加单个或多个 uint64 到数组: [1,2] => [1,2,3,4]

func (*Array) AppendUintArray

func (a *Array) AppendUintArray(vv []uint64)

AppendUintArray 追加 uint64 数组: [1,2] => [1,2,[3,4,5]]

func (*Array) Serialize

func (a *Array) Serialize(buf []byte) []byte

Serialize 将数组序列化并追加到 buf 后返回. 只在 buf 已满 (含 nil 和非空满前缀) 时按 Size 一次性扩容, 嵌套调用时外层已预留容量, 不重复遍历计算 Size. buf 为 nil 时结果按 Size 由运行时一次分配 (容量按尺寸类取整) 而非取自 bytespool: 现有调用方均不归还结果, 池未命中反而更慢且按 2 的幂次多占内存. 结果不应 bytespool.Release (不来自池); 需要复用缓冲时请传入自有 buf 并自行管理生命周期.

func (*Array) Size

func (a *Array) Size() int

type Map

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

Map 对象类(字典) JSON 数据. 内联前 16 个键值槽位且无自引用指针, 常规热路径(如 IP 查询、Web 日志、告警构造)可直接在栈上完成组装.

func NewMap

func NewMap() *Map

NewMap 创建对象类(字典) JSON 数据集, 无添加值时, 结果中至少附加一个空对象: {}

func (*Map) PutArray

func (m *Map) PutArray(key string, oa *Array)

PutArray 添加值为数组的数据项: {"A":[1,true,"x"]}

func (*Map) PutBool

func (m *Map) PutBool(key string, b bool)

func (*Map) PutBoolArray

func (m *Map) PutBoolArray(key string, b []bool)

func (*Map) PutFloat

func (m *Map) PutFloat(key string, f float64)

func (*Map) PutFloatArray

func (m *Map) PutFloatArray(key string, f []float64)

func (*Map) PutInt

func (m *Map) PutInt(key string, i int64)

func (*Map) PutIntArray

func (m *Map) PutIntArray(key string, i []int64)

func (*Map) PutMap

func (m *Map) PutMap(key string, om *Map)

PutMap 添加值为对象(字典)的数据项, map 嵌套: {"A":{"sub":1}}

func (*Map) PutRawBytes

func (m *Map) PutRawBytes(key string, b []byte)

func (*Map) PutRawBytesArray

func (m *Map) PutRawBytesArray(key string, bs [][]byte)

func (*Map) PutRawString

func (m *Map) PutRawString(key, s string)

func (*Map) PutRawStringArray

func (m *Map) PutRawStringArray(key string, ss []string)

func (*Map) PutString

func (m *Map) PutString(key, s string)

func (*Map) PutStringArray

func (m *Map) PutStringArray(key string, s []string)

func (*Map) PutUint

func (m *Map) PutUint(key string, u uint64)

func (*Map) PutUintArray

func (m *Map) PutUintArray(key string, u []uint64)

PutUintArray 添加 uint64 数组数据项: {"A":[1,2]}

func (*Map) Serialize

func (m *Map) Serialize(buf []byte) []byte

Serialize 将对象序列化并追加到 buf 后返回, buf 容量与结果归属规则同 Array.Serialize.

func (*Map) Size

func (m *Map) Size() int

type RawBytes

type RawBytes []byte

RawBytes 附加原生 JSON 数据

func (RawBytes) Serialize

func (b RawBytes) Serialize(buf []byte) []byte

func (RawBytes) Size

func (b RawBytes) Size() int

type RawString

type RawString string

RawString 附加原生 JSON 数据字符串形式, 如直接附加: `[1,{"A":1}]`

func (RawString) Serialize

func (s RawString) Serialize(buf []byte) []byte

func (RawString) Size

func (s RawString) Size() int

type V

type V string

V 表示标准的 JSON 值,例如: 123,1.23,true 等 字符串值是以双引号包裹的字符串, 如: "abc"

func (V) Serialize

func (v V) Serialize(buf []byte) []byte

func (V) Size

func (v V) Size() int

type Value

type Value interface {
	// Serialize 将值序列化为字符串, 追加到 buf 并返回
	Serialize(buf []byte) []byte
	// Size 返回值最终字节数
	Size() int
}

Jump to

Keyboard shortcuts

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