github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/perf/json/json.go (about)

     1  // Package json provides a on-the-fly change-able API for JSON serialization.
     2  package json
     3  
     4  import (
     5  	"bytes"
     6  	"encoding/json"
     7  )
     8  
     9  // Marshaler is an alias name of encoding/json.Marshaler.
    10  // See encoding/json.Marshaler for detailed document.
    11  type Marshaler = json.Marshaler
    12  
    13  // Unmarshaler is an alias name of encoding/json.Unmarshaler.
    14  // See encoding/json.Unmarshaler for detailed document.
    15  type Unmarshaler = json.Unmarshaler
    16  
    17  // RawMessage is a raw encoded JSON value.
    18  // It implements Marshaler and Unmarshaler and can
    19  // be used to delay JSON decoding or precompute a JSON encoding.
    20  type RawMessage = json.RawMessage
    21  
    22  // Marshal returns the JSON encoding of v.
    23  //
    24  // See encoding/json.Marshal for detailed document.
    25  func Marshal(v any) ([]byte, error) {
    26  	return getImpl().Marshal(v)
    27  }
    28  
    29  // MarshalToString returns the JSON encoding of v as string.
    30  //
    31  // See encoding/json.Marshal for detailed document.
    32  func MarshalToString(v any) (string, error) {
    33  	return getImpl().MarshalToString(v)
    34  }
    35  
    36  // MarshalIndent is like Marshal but applies Indent to format the output.
    37  //
    38  // See encoding/json.MarshalIndent for detailed document.
    39  func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
    40  	return getImpl().MarshalIndent(v, prefix, indent)
    41  }
    42  
    43  // Unmarshal parses the JSON-encoded data and stores the result
    44  // in the value pointed to by v.
    45  //
    46  // See encoding/json.Unmarshal for detailed document.
    47  func Unmarshal(data []byte, v any) error {
    48  	return getImpl().Unmarshal(data, v)
    49  }
    50  
    51  // UnmarshalFromString parses the JSON-encoded string data and stores
    52  // the result in the value pointed to by v.
    53  //
    54  // See encoding/json.Unmarshal for detailed document.
    55  func UnmarshalFromString(data string, v any) error {
    56  	return getImpl().UnmarshalFromString(data, v)
    57  }
    58  
    59  // Valid reports whether data is a valid JSON encoding.
    60  func Valid(data []byte) bool {
    61  	return getImpl().Valid(data)
    62  }
    63  
    64  // Compact appends to dst the JSON-encoded src with
    65  // insignificant space characters elided.
    66  func Compact(dst *bytes.Buffer, src []byte) error {
    67  	return getImpl().Compact(dst, src)
    68  }
    69  
    70  // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
    71  // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
    72  // so that the JSON will be safe to embed inside HTML <script> tags.
    73  // For historical reasons, web browsers don't honor standard HTML
    74  // escaping within <script> tags, so an alternative JSON encoding must
    75  // be used.
    76  func HTMLEscape(dst *bytes.Buffer, src []byte) {
    77  	getImpl().HTMLEscape(dst, src)
    78  }
    79  
    80  // Indent appends to dst an indented form of the JSON-encoded src.
    81  // See encoding/json.Indent for detailed document.
    82  func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
    83  	return getImpl().Indent(dst, src, prefix, indent)
    84  }
    85  
    86  // MarshalFastest uses the fastest config if the underlying implementation
    87  // supports it, e.g. jsoniter and sonic.
    88  // The result may be incompatible with std [encoding/json] in some ways,
    89  // especially that map keys may be not sorted.
    90  func MarshalFastest(v any) ([]byte, error) {
    91  	return getImpl().MarshalFastest(v)
    92  }
    93  
    94  // MarshalNoHTMLEscape is like Marshal but does not escape HTML characters.
    95  // Optionally indent can be applied to the output,
    96  // empty prefix and indent disables indentation.
    97  // The output is more friendly to read for log messages.
    98  func MarshalNoHTMLEscape(v any, prefix, indent string) ([]byte, error) {
    99  	return getImpl().MarshalNoHTMLEscape(v, prefix, indent)
   100  }