github.com/gogf/gf/v2@v2.7.4/encoding/gjson/gjson_stdlib_json_util.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gjson
     8  
     9  import (
    10  	"bytes"
    11  
    12  	"github.com/gogf/gf/v2/errors/gerror"
    13  	"github.com/gogf/gf/v2/internal/json"
    14  	"github.com/gogf/gf/v2/util/gconv"
    15  )
    16  
    17  // Valid checks whether `data` is a valid JSON data type.
    18  // The parameter `data` specifies the json format data, which can be either
    19  // bytes or string type.
    20  func Valid(data interface{}) bool {
    21  	return json.Valid(gconv.Bytes(data))
    22  }
    23  
    24  // Marshal is alias of Encode in order to fit the habit of json.Marshal/Unmarshal functions.
    25  func Marshal(v interface{}) (marshaledBytes []byte, err error) {
    26  	return Encode(v)
    27  }
    28  
    29  // MarshalIndent is alias of json.MarshalIndent in order to fit the habit of json.MarshalIndent function.
    30  func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error) {
    31  	return json.MarshalIndent(v, prefix, indent)
    32  }
    33  
    34  // Unmarshal is alias of DecodeTo in order to fit the habit of json.Marshal/Unmarshal functions.
    35  func Unmarshal(data []byte, v interface{}) (err error) {
    36  	return DecodeTo(data, v)
    37  }
    38  
    39  // Encode encodes any golang variable `value` to JSON bytes.
    40  func Encode(value interface{}) ([]byte, error) {
    41  	return json.Marshal(value)
    42  }
    43  
    44  // MustEncode performs as Encode, but it panics if any error occurs.
    45  func MustEncode(value interface{}) []byte {
    46  	b, err := Encode(value)
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  	return b
    51  }
    52  
    53  // EncodeString encodes any golang variable `value` to JSON string.
    54  func EncodeString(value interface{}) (string, error) {
    55  	b, err := json.Marshal(value)
    56  	return string(b), err
    57  }
    58  
    59  // MustEncodeString encodes any golang variable `value` to JSON string.
    60  // It panics if any error occurs.
    61  func MustEncodeString(value interface{}) string {
    62  	return string(MustEncode(value))
    63  }
    64  
    65  // Decode decodes json format `data` to golang variable.
    66  // The parameter `data` can be either bytes or string type.
    67  func Decode(data interface{}, options ...Options) (interface{}, error) {
    68  	var value interface{}
    69  	if err := DecodeTo(gconv.Bytes(data), &value, options...); err != nil {
    70  		return nil, err
    71  	} else {
    72  		return value, nil
    73  	}
    74  }
    75  
    76  // DecodeTo decodes json format `data` to specified golang variable `v`.
    77  // The parameter `data` can be either bytes or string type.
    78  // The parameter `v` should be a pointer type.
    79  func DecodeTo(data interface{}, v interface{}, options ...Options) (err error) {
    80  	decoder := json.NewDecoder(bytes.NewReader(gconv.Bytes(data)))
    81  	if len(options) > 0 {
    82  		// The StrNumber option is for certain situations, not for all.
    83  		// For example, it causes converting issue for other data formats, for example: yaml.
    84  		if options[0].StrNumber {
    85  			decoder.UseNumber()
    86  		}
    87  	}
    88  	if err = decoder.Decode(v); err != nil {
    89  		err = gerror.Wrap(err, `json Decode failed`)
    90  	}
    91  	return
    92  }
    93  
    94  // DecodeToJson codes json format `data` to a Json object.
    95  // The parameter `data` can be either bytes or string type.
    96  func DecodeToJson(data interface{}, options ...Options) (*Json, error) {
    97  	if v, err := Decode(gconv.Bytes(data), options...); err != nil {
    98  		return nil, err
    99  	} else {
   100  		if len(options) > 0 {
   101  			return New(v, options[0].Safe), nil
   102  		}
   103  		return New(v), nil
   104  	}
   105  }