2020-09-16 12:23:43 +03:00
package json
2021-05-31 16:25:33 +03:00
import (
2022-03-24 23:09:22 +03:00
"io"
2021-06-04 18:27:02 +03:00
"github.com/goccy/go-json/internal/decoder"
2021-05-31 16:25:33 +03:00
"github.com/goccy/go-json/internal/encoder"
)
2020-09-16 12:23:43 +03:00
2021-05-31 16:25:33 +03:00
type EncodeOption = encoder . Option
type EncodeOptionFunc func ( * EncodeOption )
2021-06-06 11:57:30 +03:00
// UnorderedMap doesn't sort when encoding map type.
2021-05-31 16:25:33 +03:00
func UnorderedMap ( ) EncodeOptionFunc {
return func ( opt * EncodeOption ) {
2021-06-01 07:40:15 +03:00
opt . Flag |= encoder . UnorderedMapOption
2020-09-16 12:23:43 +03:00
}
}
2021-03-27 06:18:19 +03:00
2021-12-30 05:54:29 +03:00
// DisableHTMLEscape disables escaping of HTML characters ( '&', '<', '>' ) when encoding string.
func DisableHTMLEscape ( ) EncodeOptionFunc {
return func ( opt * EncodeOption ) {
opt . Flag &= ^ encoder . HTMLEscapeOption
}
}
// DisableNormalizeUTF8
// By default, when encoding string, UTF8 characters in the range of 0x80 - 0xFF are processed by applying \ufffd for invalid code and escaping for \u2028 and \u2029.
// This option disables this behaviour. You can expect faster speeds by applying this option, but be careful.
// encoding/json implements here: https://github.com/golang/go/blob/6178d25fc0b28724b1b5aec2b1b74fc06d9294c7/src/encoding/json/encode.go#L1067-L1093.
func DisableNormalizeUTF8 ( ) EncodeOptionFunc {
return func ( opt * EncodeOption ) {
opt . Flag &= ^ encoder . NormalizeUTF8Option
}
}
2021-06-06 11:57:30 +03:00
// Debug outputs debug information when panic occurs during encoding.
2021-05-31 16:25:33 +03:00
func Debug ( ) EncodeOptionFunc {
return func ( opt * EncodeOption ) {
2021-06-01 07:40:15 +03:00
opt . Flag |= encoder . DebugOption
2021-03-27 06:18:19 +03:00
}
}
2021-05-31 18:46:42 +03:00
2022-03-24 23:09:22 +03:00
// DebugWith sets the destination to write debug messages.
func DebugWith ( w io . Writer ) EncodeOptionFunc {
return func ( opt * EncodeOption ) {
opt . DebugOut = w
}
}
2023-03-19 22:53:43 +03:00
// DebugDOT sets the destination to write opcodes graph.
func DebugDOT ( w io . WriteCloser ) EncodeOptionFunc {
return func ( opt * EncodeOption ) {
opt . DebugDOTOut = w
}
}
2021-06-06 11:57:30 +03:00
// Colorize add an identifier for coloring to the string of the encoded result.
2021-05-31 18:46:42 +03:00
func Colorize ( scheme * ColorScheme ) EncodeOptionFunc {
return func ( opt * EncodeOption ) {
2021-06-01 07:40:15 +03:00
opt . Flag |= encoder . ColorizeOption
2021-05-31 18:46:42 +03:00
opt . ColorScheme = scheme
}
}
2021-06-04 18:27:02 +03:00
type DecodeOption = decoder . Option
type DecodeOptionFunc func ( * DecodeOption )
2021-06-06 11:57:30 +03:00
// DecodeFieldPriorityFirstWin
// in the default behavior, go-json, like encoding/json,
// will reflect the result of the last evaluation when a field with the same name exists.
// This option allow you to change this behavior.
// this option reflects the result of the first evaluation if a field with the same name exists.
// This behavior has a performance advantage as it allows the subsequent strings to be skipped if all fields have been evaluated.
2021-06-04 18:27:02 +03:00
func DecodeFieldPriorityFirstWin ( ) DecodeOptionFunc {
return func ( opt * DecodeOption ) {
2021-06-12 11:06:26 +03:00
opt . Flags |= decoder . FirstWinOption
2021-06-04 18:27:02 +03:00
}
}