Skip to content

Commit f520034

Browse files
committed
json: use reflect.IsZero on arrays instead of len check
2 parents e9e1044 + 0ae1a77 commit f520034

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2009 The Go Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google Inc. nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# json
2+
A fork of encoding/json that fixes omitempty behavior on structs
3+
4+
https://play.golang.org/p/M8AOBJgWMJy
5+
6+
# Example
7+
```
8+
T := struct {
9+
B struct {} `json:",omitempty"`
10+
}{}
11+
12+
json.NewEncoder(os.Stdout).Encode(T) // stdlib
13+
NewEncoder(os.Stdout).Encode(T) // this package
14+
```
15+
16+
# Output
17+
```
18+
{"B":{}} // stdlib
19+
{} // this package
20+
```

encode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ func (e *encodeState) error(err error) {
338338

339339
func isEmptyValue(v reflect.Value) bool {
340340
switch v.Kind() {
341-
case reflect.Struct:
341+
case reflect.Struct, reflect.Array:
342342
return v.IsZero()
343-
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
343+
case reflect.Map, reflect.Slice, reflect.String:
344344
return v.Len() == 0
345345
case reflect.Bool:
346346
return !v.Bool()

0 commit comments

Comments
 (0)