1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
24 func (a
*Animal
) UnmarshalJSON(b
[]byte) error
{
26 if err
:= json
.Unmarshal(b
, &s
); err
!= nil {
29 switch strings
.ToLower(s
) {
41 func (a Animal
) MarshalJSON() ([]byte, error
) {
52 return json
.Marshal(s
)
55 func Example_customMarshalJSON() {
56 blob
:= `["gopher","armadillo","zebra","unknown","gopher","bee","gopher","zebra"]`
58 if err
:= json
.Unmarshal([]byte(blob
), &zoo
); err
!= nil {
62 census
:= make(map[Animal
]int)
63 for _
, animal
:= range zoo
{
67 fmt
.Printf("Zoo Census:\n* Gophers: %d\n* Zebras: %d\n* Unknown: %d\n",
68 census
[Gopher
], census
[Zebra
], census
[Unknown
])