json2

package
v6.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package json2 is the supplement of the standard library of `json`.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeNowTime

func EncodeNowTime(layout string, utc ...bool) []byte

EncodeNowTime is the same as EncodeTime, but encodes the now time.

func EncodeTime

func EncodeTime(t time.Time, layout string, utc ...bool) []byte

EncodeTime encodes the time t to []byte, which will convrt it to UTC if utc is true.

func MarshalJSON

func MarshalJSON(w io.Writer, v interface{}) (n int, err error)

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

func MarshalKvJSON(w io.Writer, args ...interface{}) (n int, err error)

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 ToBytes

func ToBytes(i interface{}, fmtSprintf ...bool) []byte

ToBytes is the same as ToBytesErr, but ignoring the error.

func ToBytesErr

func ToBytesErr(i interface{}, fmtSprintf ...bool) ([]byte, error)

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 types.ErrUnknownType.

func ToString

func ToString(i interface{}, fmtSprintf ...bool) string

ToString is the same as ToBytesErr, but returns string and ignores the error.

func ToStringErr

func ToStringErr(i interface{}, fmtSprintf ...bool) (string, error)

ToStringErr is the same as ToBytesErr, but returns string.

func Write

func Write(w io.Writer, i interface{}, fmtSprintf ...bool) error

Write is the same as ToBytesErr, but writes the result into w, and do some optimizations.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL