You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-library/utils/phpjson/phpjson.go

51 lines
962 B

package phpjson
import (
"encoding/json"
"fmt"
"reflect"
"strings"
)
func Encode(v interface{}) (string, error) {
bytes, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(bytes), nil
}
func MarshalToString(msg interface{}) (string, error) {
j, e := json.Marshal(msg)
if e != nil {
return "", e
}
return string(j), nil
}
func Implode(list interface{}, seq string) string {
listValue := reflect.Indirect(reflect.ValueOf(list))
if listValue.Kind() != reflect.Slice {
return ""
}
count := listValue.Len()
listStr := make([]string, 0, count)
for i := 0; i < count; i++ {
v := listValue.Index(i)
if str, err := getValue(v); err == nil {
listStr = append(listStr, str)
}
}
return strings.Join(listStr, seq)
}
func getValue(value reflect.Value) (res string, err error) {
switch value.Kind() {
case reflect.Ptr:
res, err = getValue(value.Elem())
default:
res = fmt.Sprint(value.Interface())
}
return
}