|
- package response
-
- import (
- "bytes"
- "encoding/json"
- )
-
- const (
- JsonMsgDefaultOk = "ok"
- JsonMsgDefaultFailed = "unknown"
- )
-
- type Json struct {
- Code int `json:"code"`
- Data interface{} `json:"data"`
- Msg string `json:"msg"`
- Status bool `json:"status"`
- }
-
- func (j *Json) String() string {
- s, _ := j.ToString()
- return s
- }
-
- func (j *Json) ToString() (str string, err error) {
- var b []byte
- b, err = json.Marshal(j)
- return bytes.NewBuffer(b).String(), err
- }
-
- func NewJsonByDefaultSuccess(data ...interface{}) *Json {
- return NewJson(true, 0, JsonMsgDefaultOk, data...)
- }
-
- func NewJsonByDefaultFailed(data ...interface{}) *Json {
- return NewJson(false, 1, JsonMsgDefaultFailed, data...)
- }
-
- func NewJson(status bool, code int, msg string, data ...interface{}) *Json {
- if len(data) > 0 {
- return &Json{
- Code: code,
- Data: data[0],
- Msg: msg,
- Status: status,
- }
- } else {
- return &Json{
- Code: code,
- Data: nil,
- Msg: msg,
- Status: status,
- }
- }
- }
|