decode.go 797 字节
Newer Older
Y
Your Name 已提交
1 2 3 4 5
package response

import (
	"encoding/json"
	"strings"
Y
Your Name 已提交
6 7

	"github.com/eolinker/goku-api-gateway/utils"
Y
Your Name 已提交
8
)
Y
Your Name 已提交
9 10 11 12 13

const (
	//JSON json
	JSON = "json"
	//XML xml
Y
Your Name 已提交
14
	XML = "xml"
Y
Your Name 已提交
15
	//String string
Y
Your Name 已提交
16
	String = "string"
Y
Your Name 已提交
17 18
	//JSONNoQuote 非标准json(key不带双引号)
	JSONNoQuote = "json-noquote"
Y
Your Name 已提交
19
)
Y
Your Name 已提交
20

Y
Your Name 已提交
21
var (
Y
Your Name 已提交
22 23 24 25 26 27 28 29 30 31
	jsonDecoder = func(data []byte, v interface{}) error {
		err := json.Unmarshal(data, v)
		return err
	}
	jsonNoQuoteDecoder = func(data []byte, v interface{}) error {
		d, err := utils.JSObjectToJSON(string(data))
		if err != nil {
			return err
		}
		err = json.Unmarshal(d, v)
Y
Your Name 已提交
32 33 34
		return err
	}
)
Y
Your Name 已提交
35 36

//GetDecoder getDecoder
Y
Your Name 已提交
37 38 39 40 41
func GetDecoder(decoder string) DecodeHandle {

	switch strings.ToLower(decoder) {
	case JSON:
		return jsonDecoder
Y
Your Name 已提交
42 43
	case JSONNoQuote:
		return jsonNoQuoteDecoder
Y
Your Name 已提交
44 45 46 47
	}

	return nil
}