Skip to content

Commit

Permalink
Address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Grant committed Feb 22, 2016
1 parent f87157a commit 25be454
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ func (list *List) UnmarshalJSON(rawData []byte) error {

/*
MarshalJSON returns a top level object for the "data" attribute if a single object. In
all other cases returns a JSON encoded list for "data".
all other cases returns a JSON encoded list for "data". We use a pointer receiver here
so we are able to distinguish between nil (don't serialize) and empty (serialize as []).
*/
func (list *List) MarshalJSON() ([]byte, error) {
// avoid stack overflow by using this subtype for marshaling
type MarshalList List
marshalList := (*MarshalList)(list)
isnil := marshalList == nil
count := 0
if !isnil {
count = len(*marshalList)

if list == nil {
return nil, nil
}

marshalList := MarshalList(*list)
count := len(marshalList)

switch {
case isnil:
return nil, nil
case count == 0:
return []byte("[]"), nil
case count == 1:
return json.Marshal((*marshalList)[0])
return json.Marshal(marshalList[0])
default:
return json.Marshal(marshalList)
}
Expand Down

0 comments on commit 25be454

Please sign in to comment.