api.go
This file
Path
codec/json/api.go
57 lines · 1.9 KiB · Go
Last changed by
feat: support custom json codec at runtime (#3391)Timbefore adoption688a429
History of this fileTake it
1// Copyright 2025 Gin Core Team. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package json67import "io"89// API the json codec in use.10var API Core1112// Core the api for json codec.13type Core interface {14 Marshal(v any) ([]byte, error)15 Unmarshal(data []byte, v any) error16 MarshalIndent(v any, prefix, indent string) ([]byte, error)17 NewEncoder(writer io.Writer) Encoder18 NewDecoder(reader io.Reader) Decoder19}2021// Encoder an interface writes JSON values to an output stream.22type Encoder interface {23 // SetEscapeHTML specifies whether problematic HTML characters24 // should be escaped inside JSON quoted strings.25 // The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e26 // to avoid certain safety problems that can arise when embedding JSON in HTML.27 //28 // In non-HTML settings where the escaping interferes with the readability29 // of the output, SetEscapeHTML(false) disables this behavior.30 SetEscapeHTML(on bool)3132 // Encode writes the JSON encoding of v to the stream,33 // followed by a newline character.34 //35 // See the documentation for Marshal for details about the36 // conversion of Go values to JSON.37 Encode(v any) error38}3940// Decoder an interface reads and decodes JSON values from an input stream.41type Decoder interface {42 // UseNumber causes the Decoder to unmarshal a number into an any as a43 // Number instead of as a float64.44 UseNumber()4546 // DisallowUnknownFields causes the Decoder to return an error when the destination47 // is a struct and the input contains object keys which do not match any48 // non-ignored, exported fields in the destination.49 DisallowUnknownFields()5051 // Decode reads the next JSON-encoded value from its52 // input and stores it in the value pointed to by v.53 //54 // See the documentation for Unmarshal for details about55 // the conversion of JSON into a Go value.56 Decode(v any) error57}