Documentation ¶
Overview ¶
Package json2 is the supplement of the standard library of `json`.
Index ¶
- func EncodeNowTime(layout string, utc ...bool) []byte
- func EncodeTime(t time.Time, layout string, utc ...bool) []byte
- func MarshalJSON(w io.Writer, v interface{}) (n int, err error)
- func MarshalKvJSON(w io.Writer, args ...interface{}) (n int, err error)
- func ToBytes(i interface{}, fmtSprintf ...bool) []byte
- func ToBytesErr(i interface{}, fmtSprintf ...bool) ([]byte, error)
- func ToString(i interface{}, fmtSprintf ...bool) string
- func ToStringErr(i interface{}, fmtSprintf ...bool) (string, error)
- func Write(w io.Writer, i interface{}, fmtSprintf ...bool) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EncodeNowTime ¶
EncodeNowTime is the same as EncodeTime, but encodes the now time.
func EncodeTime ¶
EncodeTime encodes the time t to []byte, which will convrt it to UTC if utc is true.
func MarshalJSON ¶
MarshalJSON marshals a value v as JSON into w.
Support the types:
nil bool string | error float32 float64 int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 time.Time // The layout is time.RFC3339Nano. map[string]interface{} or map[string]string for json object json.Marshaler fmt.Stringer Array or Slice of the type above
For other types, it will use json.Marshal() to marshal it.
Example ¶
buf := bytes.NewBuffer(nil) MarshalJSON(buf, 123) buf.WriteByte('\n') MarshalJSON(buf, 1.23) buf.WriteByte('\n') MarshalJSON(buf, "123") buf.WriteByte('\n') MarshalJSON(buf, `double"quotation`) buf.WriteByte('\n') MarshalJSON(buf, time.Time{}) buf.WriteByte('\n') MarshalJSON(buf, []int{1, 2, 3}) buf.WriteByte('\n') MarshalJSON(buf, []string{"a", "b", "c"}) buf.WriteByte('\n') MarshalJSON(buf, []float64{1.2, 1.4, 1.6}) buf.WriteByte('\n') // MarshalJSON(buf, map[string]interface{}{"number": 123, "name": "abc"}) // {"number":123,"name":"abc"} buf.WriteByte('\n') fmt.Printf("%s", buf.String())
Output: 123 1.23 "123" "double\"quotation" "0001-01-01T00:00:00Z" [1,2,3] ["a","b","c"] [1.2,1.4,1.6]
func MarshalKvJSON ¶
MarshalKvJSON marshals some key-value pairs as JSON into w.
Notice: the key must be string, and the value may be one of the following:
nil bool string | error float32 float64 int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 time.Time // The layout is time.RFC3339Nano. map[string]interface{} or map[string]string for json object json.Marshaler fmt.Stringer Array or Slice of the type above
Example ¶
buf := bytes.NewBuffer(nil) MarshalKvJSON(buf, "nil", nil, "bool", true, "string", "abc", "int", 123, "double_quotation", `a"b`, "float", 1.23, "slice", []interface{}{"abc", 123}, "sslice", []string{"a", "b", "c"}, "map", map[string]interface{}{"key": "xyz"}) fmt.Println(buf.String()) // Check whether the json string is valid. data := make(map[string]interface{}) if err := json.Unmarshal(buf.Bytes(), &data); err != nil { fmt.Println(err) }
Output: {"nil":null,"bool":true,"string":"abc","int":123,"double_quotation":"a\"b","float":1.23,"slice":["abc",123],"sslice":["a","b","c"],"map":{"key":"xyz"}}
func ToBytesErr ¶
ToBytesErr encodes a value to []byte.
For the time.Time, it uses time.RFC3339Nano to format it.
Support the types:
nil bool []byte string float32 float64 int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 time.Time interface error interface fmt.Stringer interface Byter interface MarshalByter interface encoding.TextMarshaler
For other types, use fmt.Sprintf("%v") to format it if fmtSprintf is true, or return the error cast.ErrUnknownType.
func ToStringErr ¶
ToStringErr is the same as ToBytesErr, but returns string.
Types ¶
This section is empty.