mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 11:48:09 +08:00
63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
// Package json provides json operations wrapping ignoring stdlib or third-party lib json.
|
|
package json
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
)
|
|
|
|
// Marshal adapts to json/encoding Marshal API.
|
|
//
|
|
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
|
|
// Refer to https://godoc.org/encoding/json#Marshal for more information.
|
|
func Marshal(v interface{}) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
// MarshalIndent same as json.MarshalIndent. Prefix is not supported.
|
|
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
|
return json.MarshalIndent(v, prefix, indent)
|
|
}
|
|
|
|
// Unmarshal adapts to json/encoding Unmarshal API
|
|
//
|
|
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
|
|
// Refer to https://godoc.org/encoding/json#Unmarshal for more information.
|
|
func Unmarshal(data []byte, v interface{}) error {
|
|
return json.Unmarshal(data, v)
|
|
}
|
|
|
|
// UnmarshalUseNumber decodes the json data bytes to target interface using number option.
|
|
func UnmarshalUseNumber(data []byte, v interface{}) error {
|
|
decoder := NewDecoder(bytes.NewReader(data))
|
|
decoder.UseNumber()
|
|
return decoder.Decode(v)
|
|
}
|
|
|
|
// NewEncoder same as json.NewEncoder
|
|
func NewEncoder(writer io.Writer) *json.Encoder {
|
|
return json.NewEncoder(writer)
|
|
}
|
|
|
|
// NewDecoder adapts to json/stream NewDecoder API.
|
|
//
|
|
// NewDecoder returns a new decoder that reads from r.
|
|
//
|
|
// Instead of a json/encoding Decoder, an Decoder is returned
|
|
// Refer to https://godoc.org/encoding/json#NewDecoder for more information.
|
|
func NewDecoder(reader io.Reader) *json.Decoder {
|
|
return json.NewDecoder(reader)
|
|
}
|
|
|
|
// Valid reports whether data is a valid JSON encoding.
|
|
func Valid(data []byte) bool {
|
|
return json.Valid(data)
|
|
}
|