| 1 |
package json |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"runtime" |
| 8 |
"unsafe" |
| 9 |
|
| 10 |
"github.com/goccy/go-json/internal/encoder" |
| 11 |
"github.com/goccy/go-json/internal/encoder/vm" |
| 12 |
"github.com/goccy/go-json/internal/encoder/vm_color" |
| 13 |
"github.com/goccy/go-json/internal/encoder/vm_color_indent" |
| 14 |
"github.com/goccy/go-json/internal/encoder/vm_indent" |
| 15 |
) |
| 16 |
|
| 17 |
// An Encoder writes JSON values to an output stream. |
| 18 |
type Encoder struct { |
| 19 |
w io.Writer |
| 20 |
enabledIndent bool |
| 21 |
enabledHTMLEscape bool |
| 22 |
prefix string |
| 23 |
indentStr string |
| 24 |
} |
| 25 |
|
| 26 |
// NewEncoder returns a new encoder that writes to w. |
| 27 |
func NewEncoder(w io.Writer) *Encoder { |
| 28 |
return &Encoder{w: w, enabledHTMLEscape: true} |
| 29 |
} |
| 30 |
|
| 31 |
// Encode writes the JSON encoding of v to the stream, followed by a newline character. |
| 32 |
// |
| 33 |
// See the documentation for Marshal for details about the conversion of Go values to JSON. |
| 34 |
func (e *Encoder) Encode(v any) error { |
| 35 |
return e.EncodeWithOption(v) |
| 36 |
} |
| 37 |
|
| 38 |
// EncodeWithOption call Encode with EncodeOption. |
| 39 |
func (e *Encoder) EncodeWithOption(v any, optFuncs ...EncodeOptionFunc) error { |
| 40 |
ctx := encoder.TakeRuntimeContext() |
| 41 |
ctx.Option.Flag = 0 |
| 42 |
|
| 43 |
err := e.encodeWithOption(ctx, v, optFuncs...) |
| 44 |
|
| 45 |
encoder.ReleaseRuntimeContext(ctx) |
| 46 |
return err |
| 47 |
} |
| 48 |
|
| 49 |
// EncodeContext call Encode with context.Context and EncodeOption. |
| 50 |
func (e *Encoder) EncodeContext(ctx context.Context, v any, optFuncs ...EncodeOptionFunc) error { |
| 51 |
rctx := encoder.TakeRuntimeContext() |
| 52 |
rctx.Option.Flag = 0 |
| 53 |
rctx.Option.Flag |= encoder.ContextOption |
| 54 |
rctx.Option.Context = ctx |
| 55 |
|
| 56 |
err := e.encodeWithOption(rctx, v, optFuncs...) |
| 57 |
|
| 58 |
encoder.ReleaseRuntimeContext(rctx) |
| 59 |
return err |
| 60 |
} |
| 61 |
|
| 62 |
func (e *Encoder) encodeWithOption(ctx *encoder.RuntimeContext, v any, optFuncs ...EncodeOptionFunc) error { |
| 63 |
if e.enabledHTMLEscape { |
| 64 |
ctx.Option.Flag |= encoder.HTMLEscapeOption |
| 65 |
} |
| 66 |
ctx.Option.Flag |= encoder.NormalizeUTF8Option |
| 67 |
ctx.Option.DebugOut = os.Stdout |
| 68 |
for _, optFunc := range optFuncs { |
| 69 |
optFunc(ctx.Option) |
| 70 |
} |
| 71 |
var ( |
| 72 |
buf []byte |
| 73 |
err error |
| 74 |
) |
| 75 |
if e.enabledIndent { |
| 76 |
buf, err = encodeIndent(ctx, v, e.prefix, e.indentStr) |
| 77 |
} else { |
| 78 |
buf, err = encode(ctx, v) |
| 79 |
} |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
if e.enabledIndent { |
| 84 |
buf = buf[:len(buf)-2] |
| 85 |
} else { |
| 86 |
buf = buf[:len(buf)-1] |
| 87 |
} |
| 88 |
buf = append(buf, '\n') |
| 89 |
if _, err := e.w.Write(buf); err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
return nil |
| 93 |
} |
| 94 |
|
| 95 |
// SetEscapeHTML specifies whether problematic HTML characters should be escaped inside JSON quoted strings. |
| 96 |
// The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e to avoid certain safety problems that can arise when embedding JSON in HTML. |
| 97 |
// |
| 98 |
// In non-HTML settings where the escaping interferes with the readability of the output, SetEscapeHTML(false) disables this behavior. |
| 99 |
func (e *Encoder) SetEscapeHTML(on bool) { |
| 100 |
e.enabledHTMLEscape = on |
| 101 |
} |
| 102 |
|
| 103 |
// SetIndent instructs the encoder to format each subsequent encoded value as if indented by the package-level function Indent(dst, src, prefix, indent). |
| 104 |
// Calling SetIndent("", "") disables indentation. |
| 105 |
func (e *Encoder) SetIndent(prefix, indent string) { |
| 106 |
if prefix == "" && indent == "" { |
| 107 |
e.enabledIndent = false |
| 108 |
return |
| 109 |
} |
| 110 |
e.prefix = prefix |
| 111 |
e.indentStr = indent |
| 112 |
e.enabledIndent = true |
| 113 |
} |
| 114 |
|
| 115 |
func marshalContext(ctx context.Context, v any, optFuncs ...EncodeOptionFunc) ([]byte, error) { |
| 116 |
rctx := encoder.TakeRuntimeContext() |
| 117 |
rctx.Option.Flag = 0 |
| 118 |
rctx.Option.Flag = encoder.HTMLEscapeOption | encoder.NormalizeUTF8Option | encoder.ContextOption |
| 119 |
rctx.Option.Context = ctx |
| 120 |
for _, optFunc := range optFuncs { |
| 121 |
optFunc(rctx.Option) |
| 122 |
} |
| 123 |
|
| 124 |
buf, err := encode(rctx, v) |
| 125 |
if err != nil { |
| 126 |
encoder.ReleaseRuntimeContext(rctx) |
| 127 |
return nil, err |
| 128 |
} |
| 129 |
|
| 130 |
// this line exists to escape call of `runtime.makeslicecopy` . |
| 131 |
// if use `make([]byte, len(buf)-1)` and `copy(copied, buf)`, |
| 132 |
// dst buffer size and src buffer size are differrent. |
| 133 |
// in this case, compiler uses `runtime.makeslicecopy`, but it is slow. |
| 134 |
buf = buf[:len(buf)-1] |
| 135 |
copied := make([]byte, len(buf)) |
| 136 |
copy(copied, buf) |
| 137 |
|
| 138 |
encoder.ReleaseRuntimeContext(rctx) |
| 139 |
return copied, nil |
| 140 |
} |
| 141 |
|
| 142 |
func marshal(v any, optFuncs ...EncodeOptionFunc) ([]byte, error) { |
| 143 |
ctx := encoder.TakeRuntimeContext() |
| 144 |
|
| 145 |
ctx.Option.Flag = 0 |
| 146 |
ctx.Option.Flag |= (encoder.HTMLEscapeOption | encoder.NormalizeUTF8Option) |
| 147 |
for _, optFunc := range optFuncs { |
| 148 |
optFunc(ctx.Option) |
| 149 |
} |
| 150 |
|
| 151 |
buf, err := encode(ctx, v) |
| 152 |
if err != nil { |
| 153 |
encoder.ReleaseRuntimeContext(ctx) |
| 154 |
return nil, err |
| 155 |
} |
| 156 |
|
| 157 |
// this line exists to escape call of `runtime.makeslicecopy` . |
| 158 |
// if use `make([]byte, len(buf)-1)` and `copy(copied, buf)`, |
| 159 |
// dst buffer size and src buffer size are differrent. |
| 160 |
// in this case, compiler uses `runtime.makeslicecopy`, but it is slow. |
| 161 |
buf = buf[:len(buf)-1] |
| 162 |
copied := make([]byte, len(buf)) |
| 163 |
copy(copied, buf) |
| 164 |
|
| 165 |
encoder.ReleaseRuntimeContext(ctx) |
| 166 |
return copied, nil |
| 167 |
} |
| 168 |
|
| 169 |
func marshalIndent(v any, prefix, indent string, optFuncs ...EncodeOptionFunc) ([]byte, error) { |
| 170 |
ctx := encoder.TakeRuntimeContext() |
| 171 |
|
| 172 |
ctx.Option.Flag = 0 |
| 173 |
ctx.Option.Flag |= (encoder.HTMLEscapeOption | encoder.NormalizeUTF8Option | encoder.IndentOption) |
| 174 |
for _, optFunc := range optFuncs { |
| 175 |
optFunc(ctx.Option) |
| 176 |
} |
| 177 |
|
| 178 |
buf, err := encodeIndent(ctx, v, prefix, indent) |
| 179 |
if err != nil { |
| 180 |
encoder.ReleaseRuntimeContext(ctx) |
| 181 |
return nil, err |
| 182 |
} |
| 183 |
|
| 184 |
buf = buf[:len(buf)-2] |
| 185 |
copied := make([]byte, len(buf)) |
| 186 |
copy(copied, buf) |
| 187 |
|
| 188 |
encoder.ReleaseRuntimeContext(ctx) |
| 189 |
return copied, nil |
| 190 |
} |
| 191 |
|
| 192 |
func encode(ctx *encoder.RuntimeContext, v any) ([]byte, error) { |
| 193 |
b := ctx.Buf[:0] |
| 194 |
if v == nil { |
| 195 |
b = encoder.AppendNull(ctx, b) |
| 196 |
b = encoder.AppendComma(ctx, b) |
| 197 |
return b, nil |
| 198 |
} |
| 199 |
header := (*emptyInterface)(unsafe.Pointer(&v)) |
| 200 |
typ := header.typ |
| 201 |
|
| 202 |
typeptr := uintptr(typ) |
| 203 |
codeSet, err := encoder.CompileToGetCodeSet(ctx, typeptr) |
| 204 |
if err != nil { |
| 205 |
return nil, err |
| 206 |
} |
| 207 |
|
| 208 |
p := ctx.ValueAddr(codeSet, header.ptr) |
| 209 |
if p == nil { |
| 210 |
// only a nil pointer has no address of the value. |
| 211 |
b = encoder.AppendNull(ctx, b) |
| 212 |
b = encoder.AppendComma(ctx, b) |
| 213 |
return b, nil |
| 214 |
} |
| 215 |
ctx.Init(p, codeSet.CodeLength) |
| 216 |
|
| 217 |
buf, err := encodeRunCode(ctx, b, codeSet) |
| 218 |
// the VM refers to the value by uintptr. |
| 219 |
runtime.KeepAlive(v) |
| 220 |
if err != nil { |
| 221 |
return nil, err |
| 222 |
} |
| 223 |
ctx.Buf = buf |
| 224 |
return buf, nil |
| 225 |
} |
| 226 |
|
| 227 |
func encodeIndent(ctx *encoder.RuntimeContext, v any, prefix, indent string) ([]byte, error) { |
| 228 |
b := ctx.Buf[:0] |
| 229 |
if v == nil { |
| 230 |
b = encoder.AppendNull(ctx, b) |
| 231 |
b = encoder.AppendCommaIndent(ctx, b) |
| 232 |
return b, nil |
| 233 |
} |
| 234 |
header := (*emptyInterface)(unsafe.Pointer(&v)) |
| 235 |
typ := header.typ |
| 236 |
|
| 237 |
typeptr := uintptr(typ) |
| 238 |
codeSet, err := encoder.CompileToGetCodeSet(ctx, typeptr) |
| 239 |
if err != nil { |
| 240 |
return nil, err |
| 241 |
} |
| 242 |
|
| 243 |
p := ctx.ValueAddr(codeSet, header.ptr) |
| 244 |
if p == nil { |
| 245 |
// only a nil pointer has no address of the value. |
| 246 |
b = encoder.AppendNull(ctx, b) |
| 247 |
b = encoder.AppendCommaIndent(ctx, b) |
| 248 |
return b, nil |
| 249 |
} |
| 250 |
ctx.Init(p, codeSet.CodeLength) |
| 251 |
buf, err := encodeRunIndentCode(ctx, b, codeSet, prefix, indent) |
| 252 |
// the VM refers to the value by uintptr. |
| 253 |
runtime.KeepAlive(v) |
| 254 |
|
| 255 |
if err != nil { |
| 256 |
return nil, err |
| 257 |
} |
| 258 |
|
| 259 |
ctx.Buf = buf |
| 260 |
return buf, nil |
| 261 |
} |
| 262 |
|
| 263 |
func encodeRunCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { |
| 264 |
if (ctx.Option.Flag & encoder.DebugOption) != 0 { |
| 265 |
if (ctx.Option.Flag & encoder.ColorizeOption) != 0 { |
| 266 |
return vm_color.DebugRun(ctx, b, codeSet) |
| 267 |
} |
| 268 |
return vm.DebugRun(ctx, b, codeSet) |
| 269 |
} |
| 270 |
if (ctx.Option.Flag & encoder.ColorizeOption) != 0 { |
| 271 |
return vm_color.Run(ctx, b, codeSet) |
| 272 |
} |
| 273 |
return vm.Run(ctx, b, codeSet) |
| 274 |
} |
| 275 |
|
| 276 |
func encodeRunIndentCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, prefix, indent string) ([]byte, error) { |
| 277 |
ctx.Prefix = []byte(prefix) |
| 278 |
ctx.IndentStr = []byte(indent) |
| 279 |
if (ctx.Option.Flag & encoder.DebugOption) != 0 { |
| 280 |
if (ctx.Option.Flag & encoder.ColorizeOption) != 0 { |
| 281 |
return vm_color_indent.DebugRun(ctx, b, codeSet) |
| 282 |
} |
| 283 |
return vm_indent.DebugRun(ctx, b, codeSet) |
| 284 |
} |
| 285 |
if (ctx.Option.Flag & encoder.ColorizeOption) != 0 { |
| 286 |
return vm_color_indent.Run(ctx, b, codeSet) |
| 287 |
} |
| 288 |
return vm_indent.Run(ctx, b, codeSet) |
| 289 |
} |