github.com/jxskiss/gopkg@v0.17.3/json/json.go (about)

     1  package json
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io"
     7  	"os"
     8  
     9  	"github.com/jxskiss/gopkg/internal/unsafeheader"
    10  )
    11  
    12  // Marshaler is an alias name of encoding/json.Marshaler.
    13  // See encoding/json.Marshaler for detailed document.
    14  type Marshaler = json.Marshaler
    15  
    16  // Unmarshaler is an alias name of encoding/json.Unmarshaler.
    17  // See encoding/json.Unmarshaler for detailed document.
    18  type Unmarshaler = json.Unmarshaler
    19  
    20  // Encoder is a wrapper of encoding/json.Encoder.
    21  // It provides same methods as encoding/json.Encoder but with method
    22  // chaining capabilities.
    23  //
    24  // See encoding/json.Encoder for detailed document.
    25  type Encoder struct {
    26  	*aliasEncoder
    27  }
    28  
    29  // NewEncoder returns a new Encoder that writes to w.
    30  func NewEncoder(w io.Writer) *Encoder {
    31  	return &Encoder{_NewEncoder(w)}
    32  }
    33  
    34  func (enc *Encoder) SetEscapeHTML(on bool) *Encoder {
    35  	enc.aliasEncoder.SetEscapeHTML(on)
    36  	return enc
    37  }
    38  
    39  func (enc *Encoder) SetIndent(prefix, indent string) *Encoder {
    40  	enc.aliasEncoder.SetIndent(prefix, indent)
    41  	return enc
    42  }
    43  
    44  // Decoder is a wrapper of encoding/json.Decoder.
    45  // It provides same methods as encoding/json.Decoder but with method
    46  // chaining capabilities.
    47  //
    48  // See encoding/json.Decoder for detailed document.
    49  type Decoder struct {
    50  	*aliasDecoder
    51  }
    52  
    53  // NewDecoder returns a new Decoder that reads from r.
    54  func NewDecoder(r io.Reader) *Decoder {
    55  	return &Decoder{_NewDecoder(r)}
    56  }
    57  
    58  func (dec *Decoder) UseNumber() *Decoder {
    59  	dec.aliasDecoder.UseNumber()
    60  	return dec
    61  }
    62  
    63  func (dec *Decoder) DisallowUnknownFields() *Decoder {
    64  	dec.aliasDecoder.DisallowUnknownFields()
    65  	return dec
    66  }
    67  
    68  // Marshal returns the JSON encoding of v.
    69  //
    70  // See encoding/json.Marshal for detailed document.
    71  func Marshal(v interface{}) ([]byte, error) {
    72  	return _Marshal(v)
    73  }
    74  
    75  // MarshalToString returns the JSON encoding of v as string.
    76  //
    77  // See encoding/json.Marshal for detailed document.
    78  func MarshalToString(v interface{}) (string, error) {
    79  	buf, err := _Marshal(v)
    80  	if err != nil {
    81  		return "", err
    82  	}
    83  	return unsafeheader.BytesToString(buf), nil
    84  }
    85  
    86  // MarshalIndent is like Marshal but applies Indent to format the output.
    87  //
    88  // See encoding/json.MarshalIndent for detailed document.
    89  func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
    90  	return _MarshalIndent(v, prefix, indent)
    91  }
    92  
    93  // MarshalMapUnordered is like Marshal but does not sort map keys.
    94  // It's useful to optimize performance where map key ordering is not needed.
    95  func MarshalMapUnordered(v interface{}) ([]byte, error) {
    96  	return _MarshalMapUnordered(v)
    97  }
    98  
    99  // MarshalNoHTMLEscape is like Marshal but does not escape HTML characters.
   100  // Optionally indent can be applied to the output,
   101  // empty indentPrefix and indent disables indentation.
   102  // The output is more friendly to read for log messages.
   103  func MarshalNoHTMLEscape(v interface{}, indentPrefix, indent string) ([]byte, error) {
   104  	var buf bytes.Buffer
   105  	err := NewEncoder(&buf).
   106  		SetEscapeHTML(false).
   107  		SetIndent(indentPrefix, indent).
   108  		Encode(v)
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	return buf.Bytes(), nil
   113  }
   114  
   115  // Unmarshal parses the JSON-encoded data and stores the result
   116  // in the value pointed to by v.
   117  //
   118  // See encoding/json.Unmarshal for detailed document.
   119  func Unmarshal(data []byte, v interface{}) error {
   120  	return _Unmarshal(data, v)
   121  }
   122  
   123  // UnmarshalFromString parses the JSON-encoded string data and stores
   124  // the result in the value pointed to by v.
   125  //
   126  // See encoding/json.Unmarshal for detailed document.
   127  func UnmarshalFromString(data string, v interface{}) error {
   128  	buf := unsafeheader.StringToBytes(data)
   129  	return _Unmarshal(buf, v)
   130  }
   131  
   132  // Load reads JSON-encoded data from the named file at path and stores
   133  // the result in the value pointed to by v.
   134  func Load(path string, v interface{}) error {
   135  	file, err := os.Open(path)
   136  	if err != nil {
   137  		return err
   138  	}
   139  	defer file.Close()
   140  	err = NewDecoder(file).Decode(v)
   141  	return err
   142  }
   143  
   144  // Dump writes v to the named file at path using JSON encoding.
   145  // It disables HTMLEscape.
   146  // Optionally indent can be applied to the output,
   147  // empty indentPrefix and indent disables indentation.
   148  func Dump(path string, v interface{}, indentPrefix, indent string) error {
   149  	file, err := os.Create(path)
   150  	if err != nil {
   151  		return err
   152  	}
   153  	defer file.Close()
   154  	err = NewEncoder(file).
   155  		SetEscapeHTML(false).
   156  		SetIndent(indentPrefix, indent).
   157  		Encode(v)
   158  	return err
   159  }