提交 3340d3db 编写于 作者: 王能's avatar 王能

空数据功能增加

上级 13b7f1bb
......@@ -2,7 +2,12 @@
1.增加结构体首字母默认小写功能
StructFirstLower=true表示首字母小写(tag的优先级最高,有tag则根据tag)
示例:
type Test struct {
Data int
Data2 int `json:"D2"`
}
赋值并解析成json:{"data":10,"D2":20}
2.增加map、list、slide转结构体的功能
Map2Struct(map[string]interface{}类型转结构体)
......@@ -18,11 +23,17 @@
Data interface{} `json:"data,keepEmpty"` //空也返回
}
空的定义(bool一定会返回):
Array、Map、Slice、String的长度是0
Int、Uint、Float是0
Interface、Ptr是nil
全局增加空数据返回:StructKeepType
示例,想全局让boolean、string、整形这三个空都返回
type TestKeepStruct struct {
Bo bool
It uint
St string
Ar []string
}
json.StructKeepType = json.KeepEmptyBool | json.KeepEmptyNumber | json.KeepEmptyString
结构体数据均为空,调用以上代码后返回的json:{"bo":false,"it":0,"st":""}
## 导入方式
导入:go get github.com/weimingjue/json
......
......@@ -154,8 +154,20 @@ import (
// handle them. Passing cyclic structures to Marshal will result in
// an infinite recursion.
//
// 首字母默认小写
var StructFirstLower = true
var (
// 全局配置:首字母默认大、小写,也可以tag单独设置(`json:"data"`)
StructFirstLower = true
//全局配置:保留那些空类型,也可以tag单独保留(`json:"data,keepEmpty"`)
StructKeepType = KeepEmptyBool
)
const (
KeepEmptyBool, KeepEmptyNumber, KeepEmptyString, KeepEmptyArray emptyType = 0x1, 0x2, 0x4, 0x8
)
type emptyType int8
func Marshal(v interface{}) ([]byte, error) {
e := newEncodeState()
......@@ -317,16 +329,42 @@ func (e *encodeState) error(err error) {
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
//case reflect.Bool:
// return !v.Bool()
case reflect.Bool:
if StructKeepType&KeepEmptyBool > 0 {
return false
} else {
return !v.Bool()
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
if StructKeepType&KeepEmptyNumber > 0 {
return false
} else {
return v.Int() == 0
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
if StructKeepType&KeepEmptyNumber > 0 {
return false
} else {
return v.Uint() == 0
}
case reflect.Float32, reflect.Float64:
return v.Float() == 0
if StructKeepType&KeepEmptyNumber > 0 {
return false
} else {
return v.Float() == 0
}
case reflect.String:
if StructKeepType&KeepEmptyString > 0 {
return false
} else {
return v.Len() == 0
}
case reflect.Array, reflect.Map, reflect.Slice:
if StructKeepType&KeepEmptyArray > 0 {
return false
} else {
return v.Len() == 0
}
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
......
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package json_test
import (
"fmt"
"json"
"testing"
)
func TestKeep(t *testing.T) {
eks := TestKeepStruct{}
json.StructKeepType = json.KeepEmptyNumber | json.KeepEmptyArray
bytes, _ := json.Marshal(&eks)
fmt.Println(string(bytes))
}
type TestKeepStruct struct {
Bo bool
It uint
St string
Ar []string
St2 string `json:"Data,keepEmpty"`
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册